Azure Functions Bindings Overview
Bindings enable you to declaratively connect your function to other services without having to explicitly write plumbing code. Bindings simplify your code by separating the concerns of the business logic from the concerns of connecting to other services.
This overview explains the concept of bindings in Azure Functions and provides a high-level look at the different types of bindings available. For detailed information on specific bindings, refer to the individual binding reference documentation.
What are Bindings?
In Azure Functions, bindings provide a way to easily integrate your function with Azure services and other external services. Think of them as declarative connectors that define how your function input and output data are mapped to and from these services.
Bindings operate in two directions:
- Input Bindings: Data flows into your function from an external service.
- Output Bindings: Data flows out of your function to an external service.
By using bindings, you can:
- Reduce boilerplate code: You don't need to write code for connection management, serialization, or deserialization for many services.
- Improve code readability: The function code focuses purely on the business logic.
- Enhance maintainability: Changes to service integrations are often managed through binding configurations rather than extensive code refactoring.
Types of Bindings
Azure Functions supports a wide variety of bindings, categorized by the services they connect to. Here are some of the most common categories:
Trigger Bindings
A trigger is a special type of input binding that determines when your function should execute. When the trigger condition is met, the function runtime is invoked.
Trigger Type | Description | Example |
---|---|---|
HTTP Trigger | Executes your function when an HTTP request is received. | @HttpTrigger(methods=["get", "post"], authLevel="anonymous") |
Timer Trigger | Executes your function on a schedule defined by a CRON expression. | @TimerTrigger(schedule = "0 */5 * * * *") |
Blob Trigger | Executes your function when a new or updated blob is detected in Azure Blob Storage. | @BlobTrigger(path = "samples-workitems/{name}") |
Queue Trigger | Executes your function when a new message is added to an Azure Storage Queue. | @QueueTrigger(name = "myQueueItem", queueName = "myqueue-items") |
Event Grid Trigger | Executes your function in response to events published to Azure Event Grid. | @EventGridTrigger |
Cosmos DB Trigger | Executes your function when documents are added or modified in a Cosmos DB collection. | @CosmosDBTrigger(name = "documents", databaseName = "ToDoList", collectionName = "Items") |
Input and Output Bindings
These bindings allow your function to read data from or write data to various services.
Binding Type | Description | Example (Input) | Example (Output) |
---|---|---|---|
Blob Storage | Read from or write to Azure Blob Storage. | @BlobInput(path = "samples-data/{filename}") |
@BlobOutput(path = "samples-output/{name}.txt") |
Table Storage | Read from or write to Azure Table Storage. | @TableInput(tableName = "Products", rowKey = "{row}", partitionKey = "{partition}") |
@TableOutput(tableName = "Logs") |
Document DB / Cosmos DB | Read documents from or write documents to Azure Cosmos DB. | @CosmosDBInput(databaseName = "db", collectionName = "items", id = "{id}") |
@CosmosDBOutput(databaseName = "db", collectionName = "processeditems") |
Service Bus | Send or receive messages from Azure Service Bus queues or topics. | @ServiceBusQueueTrigger(name = "message", queueName = "inputqueue") |
@ServiceBusOutput(queueName = "outputqueue") |
Event Hubs | Read events from or send events to Azure Event Hubs. | @EventHubTrigger(name = "event", eventHubName = "myEventHub") |
@EventHubOutput(eventHubName = "outputEventHub") |
Twilio | Send SMS messages via Twilio. | N/A | @TwilioSmsOutput(to = "+1234567890", message = "Hello from Functions") |
SendGrid | Send emails via SendGrid. | N/A | @SendGridOutput(to = "recipient@example.com", subject = "Function Email") |
How Bindings Work
When you define bindings in your function's configuration (typically in a function.json
file or using attributes in code), the Azure Functions runtime handles the interaction with the connected services.
For example, with an HTTP trigger and a Blob output binding, the process might look like this:
- An HTTP request arrives, triggering the function.
- The HTTP trigger binding extracts relevant data from the request (e.g., query parameters, request body).
- Your function code executes, processes the input data, and perhaps generates some output data.
- The Blob output binding takes the data returned by your function and writes it as a new blob to the specified location in Azure Blob Storage.
Configuration
Bindings are configured in one of two ways:
function.json
file: For most languages, you define bindings in afunction.json
file located in the same directory as your function code.- Attributes: In languages like C#, you can define bindings directly on your function parameters using attributes.
Example using Attributes (C#)
This C# example shows an HTTP trigger with a Blob output binding:
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
public static class HttpBlobOutput
{
[FunctionName("HttpBlobOutput")]
public static void Run(
[HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequest req,
[Blob("output-container/{rand-guid}.txt", FileAccess.Write)] out string outputBlob,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
string requestBody = new StreamReader(req.Body).ReadToEnd();
outputBlob = $"Received: {requestBody}";
log.LogInformation($"Output blob content set to: {outputBlob}");
}
}
Example using function.json
(JavaScript)
This JavaScript example demonstrates the equivalent configuration using function.json
:
index.js
module.exports = async function (context, req) {
context.log('JavaScript HTTP trigger function processed a request.');
const requestBody = req.body;
context.bindings.outputBlob = `Received: ${requestBody}`;
context.res = {
status: 200,
body: "Successfully wrote to blob storage."
};
};
function.json
{
"bindings": [
{
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "res"
},
{
"type": "blob",
"path": "output-container/{rand-guid}.txt",
"connection": "AzureWebJobsStorage",
"direction": "out",
"name": "outputBlob"
}
]
}
Binding Expressions
Binding expressions allow you to dynamically reference values from triggers, other bindings, or application settings. These expressions can be used to construct file paths, queue names, or other dynamic targets.
Common examples include:
{name}
: References a parameter from the trigger payload (e.g., the blob name from a Blob trigger).{rand-guid}
: Generates a unique GUID.%AppName%
: References an application setting.
Conclusion
Azure Functions bindings are a powerful feature that abstracts away the complexities of integrating with various services. By understanding and leveraging bindings, you can build more efficient, maintainable, and scalable serverless applications.
Explore the individual binding documentation for detailed information on configuration, properties, and advanced usage.