Introduction to Azure Functions Bindings
Azure Functions bindings provide a declarative way to connect your function code to other Azure services and data sources. Instead of writing boilerplate code to handle authentication, network requests, or serialization, you can define bindings in your function's configuration. This allows your code to focus on business logic, making it more readable, maintainable, and efficient.
Bindings abstract away the complexity of interacting with services like Azure Cosmos DB, Azure Queue Storage, Service Bus, and many more. They define how your function is triggered (e.g., by an HTTP request, a timer, or a message in a queue) and how it interacts with data (as inputs or outputs).
How Bindings Work
Bindings are defined in your function's function.json file (for Node.js, Python, and PowerShell) or through attributes in your code (for C#, F#, and Java).
Each binding has a type, a direction (trigger, in, out), and specific settings (like connection strings, queue names, or container IDs).
When a function runs, the Functions runtime uses the binding definitions to:
- Invoke the function: Based on the trigger binding, the runtime starts your function.
- Provide input data: For input bindings, the runtime fetches data from the configured service and passes it as arguments to your function.
- Send output data: For output bindings, the runtime takes data returned by your function or explicitly bound to an output parameter and sends it to the configured service.
Binding Types
Bindings can be categorized into three main types:
Triggers
A trigger is what starts your function. Every Azure Function must have exactly one trigger. It defines the event that causes your function to execute.
- HTTP Trigger: Executes when an HTTP request is received.
- Timer Trigger: Executes on a schedule defined by a CRON expression.
- Queue Trigger: Executes when a new message is added to an Azure Storage queue.
- Blob Trigger: Executes when a new or updated blob is detected in an Azure Storage container.
- Cosmos DB Trigger: Executes when documents are created or updated in a Cosmos DB collection.
- And many more...
Input Bindings
Input bindings define how your function retrieves data from external services or data sources before its execution. This data is then passed as parameters to your function.
- Cosmos DB Input: Retrieves a document from a Cosmos DB collection.
- Queue Storage Input: Retrieves a message from an Azure Storage queue.
- Table Storage Input: Retrieves an entity from an Azure Table Storage table.
- Service Bus Queue Input: Retrieves a message from a Service Bus queue.
- And many more...
Output Bindings
Output bindings define how your function writes data to external services or data sources after its execution. This is typically done by returning a value from the function or by binding to an output parameter.
- Cosmos DB Output: Writes a document to a Cosmos DB collection.
- Queue Storage Output: Sends a message to an Azure Storage queue.
- Table Storage Output: Inserts or updates an entity in Azure Table Storage.
- Service Bus Queue Output: Sends a message to a Service Bus queue.
- Blob Storage Output: Writes data to a blob in Azure Blob Storage.
- And many more...
Common Bindings and Use Cases
HTTP Trigger & Output
Use Case: Building web APIs and webhooks.
- Trigger: Receives HTTP requests.
- Output: Sends HTTP responses.
Timer Trigger
Use Case: Running scheduled tasks and background jobs.
- Trigger: Executes based on a CRON schedule.
Queue Trigger & Output
Use Case: Implementing background processing, decoupling services.
- Trigger: Processes messages from Azure Queue Storage.
- Output: Sends messages to Azure Queue Storage.
Blob Trigger & Output
Use Case: Processing file uploads, image manipulation, data transformation.
- Trigger: Reacts to new or updated blobs.
- Output: Writes data to blobs.
Cosmos DB Trigger & Input/Output
Use Case: Real-time data processing, IoT data handling.
- Trigger: Responds to changes in Cosmos DB.
- Input: Reads documents from Cosmos DB.
- Output: Writes documents to Cosmos DB.
Binding Configuration
Bindings are configured in a function.json file. Here's an example of an HTTP triggered function that writes to a queue:
{
"scriptFile": "../run.csx",
"bindings": [
{
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "res"
},
{
"queueName": "myqueue-items",
"connection": "AzureWebJobsStorage",
"type": "queue",
"direction": "out",
"name": "outputQueueItem"
}
]
}
The name property in each binding definition corresponds to a parameter name in your function's code.
The connection setting often refers to an app setting that holds the connection string for the service.
Code Examples
C# Example (HTTP Trigger with Queue Output)
function.json
{
"scriptFile": "MyHttpFunction.cs",
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [ "post" ]
},
{
"type": "http",
"direction": "out",
"name": "res"
},
{
"queueName": "tasks",
"connection": "AzureWebJobsStorage",
"type": "queue",
"direction": "out",
"name": "outputQueue"
}
]
}
MyHttpFunction.cs
using System.IO;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
public static class MyHttpFunction
{
[FunctionName("MyHttpFunction")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequest req,
[Queue("tasks", Connection = "AzureWebJobsStorage")] IAsyncCollector<string> outputQueue,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
string message = data?.message;
if (message != null)
{
await outputQueue.AddAsync($"Processing: {message}");
log.LogInformation($"Message added to queue: {message}");
return new OkObjectResult($"Message '{message}' sent to queue.");
}
else
{
return new BadRequestObjectResult("Please pass a message in the request body.");
}
}
}
Node.js Example (Queue Trigger with Table Output)
function.json
{
"bindings": [
{
"name": "myQueueItem",
"type": "queueTrigger",
"direction": "in",
"queueName": "myqueue",
"connection": "AzureWebJobsStorage"
},
{
"name": "outputTable",
"type": "table",
"direction": "out",
"tableName": "MyTable",
"connection": "AzureWebJobsStorage"
}
]
}
index.js
module.exports = async function (context, myQueueItem) {
context.log('JavaScript queue trigger function processed work item', myQueueItem);
const item = {
PartitionKey: "default",
RowKey: context.invocationId,
data: myQueueItem
};
context.bindings.outputTable = item;
context.log('Item written to table.');
};