Azure Functions Bindings
Bindings are a declarative way to connect your Azure Functions to other Azure services and external data sources. They allow you to trigger your function based on events and to write data to other services without explicitly writing client SDK code.
Input and Output Bindings
Bindings are configured in the function.json file (for JavaScript, C#, F#, Java, and PowerShell) or through attributes (for C# and F#). There are two main categories of bindings:
- Trigger Bindings: These define the event that starts your function. Each function can have only one trigger.
- Input/Output Bindings: These allow your function to read data from or write data to other services. A function can have multiple input and output bindings.
Trigger Bindings
Trigger bindings define what initiates the execution of your function. Common triggers include:
- HTTP Trigger: Invoked by an HTTP request.
- Timer Trigger: Invoked on a schedule defined by a CRON expression.
- Blob Trigger: Invoked when a blob is added or updated in Azure Blob Storage.
- Queue Trigger: Invoked when a message is added to an Azure Storage Queue.
- Event Hub Trigger: Invoked when events are received by an Azure Event Hub.
- Service Bus Trigger: Invoked when a message arrives in an Azure Service Bus Queue or Topic.
Example: HTTP Trigger (function.json)
{
"scriptFile": "__init__.py",
"bindings": [
{
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "res"
}
]
}
This configuration defines an HTTP trigger named req that accepts GET and POST requests, and an HTTP output binding named res to send the response.
Common Input and Output Bindings
These bindings simplify interacting with various Azure services:
- Azure Blob Storage: Read from or write to blobs.
- Azure Cosmos DB: Read documents or write to collections.
- Azure Queue Storage: Read from or write to queues.
- Azure Table Storage: Read from or write to tables.
- Azure Event Hubs: Send events to an Event Hub.
- Azure Service Bus: Send messages to queues or topics.
- Azure SQL Database: Execute SQL queries.
- Twilio: Send SMS messages.
Example: Blob Input and Queue Output (function.json)
{
"scriptFile": "__init__.py",
"bindings": [
{
"name": "inputBlob",
"type": "blob",
"direction": "in",
"path": "samples-workitems/{name}.txt",
"connection": "AzureWebJobsStorage"
},
{
"name": "outputQueue",
"type": "queue",
"direction": "out",
"queueName": "output-queue",
"connection": "AzureWebJobsStorage"
}
]
}
This function has a blob input binding that reads a file named .txt from the samples-workitems container and a queue output binding that writes a message to the output-queue.
Example: Cosmos DB Input and Output (C# Attribute)
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using System.Collections.Generic;
using System.Threading.Tasks;
public static class CosmosDbExample
{
[FunctionName("CosmosDbFunction")]
public static async Task Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
[CosmosDB(
databaseName: "MyDatabase",
collectionName: "MyCollection",
ConnectionStringSetting = "CosmosDbConnection",
Id = "{Query.id}")] Document inputDocument,
[CosmosDB(
databaseName: "MyDatabase",
collectionName: "MyOutputCollection",
ConnectionStringSetting = "CosmosDbConnection")]IAsyncCollector outputDocument,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
if (inputDocument != null)
{
log.LogInformation($"Found document: {inputDocument.Id}");
await outputDocument.AddAsync(inputDocument); // Write the input document to another collection
}
else
{
log.LogWarning("No document found with the provided ID.");
}
}
}
This C# example demonstrates using attributes to define Cosmos DB input and output bindings. The inputDocument reads a document based on a query parameter, and outputDocument writes the retrieved document to a different collection.
Bindings provide a powerful abstraction that reduces boilerplate code and makes your Azure Functions more maintainable and scalable. For detailed information on specific binding types, refer to the official Azure Functions documentation.