Azure Functions enables you to build serverless applications by writing code that responds to events. Bindings are a declarative way to connect to Azure services and other resources without having to write boilerplate code to interact with them. This allows you to focus on your core business logic.
Bindings provide a way to declare how your function connects to data. They simplify your code by abstracting away the complexities of connecting to and interacting with various services like Azure Cosmos DB, Azure Storage, Azure Service Bus, and more. You declare bindings in your function's configuration file (e.g., function.json for JavaScript, C#, and F#, or attributes in C# code) and then use the bound data directly within your function code.
Key Concepts
Declarative Configuration: Bindings are defined in your function's configuration, making it clear how your function interacts with external services.
Simplified Code: You don't need to write explicit SDK code for connecting to, reading from, or writing to data sources.
Input and Output Bindings: Bindings can be used to provide data to your function (input) or to send data out from your function (output).
Trigger Bindings: A special type of input binding that defines the event that triggers your function to run.
Common Binding Types
Azure Functions supports a wide range of binding types for various services. Here are some of the most commonly used ones:
HTTP Trigger and Output Binding
The HTTP trigger allows your function to be invoked by an HTTP request. The HTTP output binding allows you to return an HTTP response.
module.exports = async function (context, req) {
context.log('HTTP trigger function processed a request.');
const name = (req.query.name || (req.body && req.body.name));
const responseMessage = name
? 'Hello, ' + name + '!'
: "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.";
context.res = {
status: 200,
body: responseMessage
};
};
Blob Storage Input and Output Bindings
These bindings allow you to read from or write to Azure Blob Storage containers.
using System;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
public static class CosmosDbProcessor
{
[FunctionName("CosmosDbProcessor")]
public static void Run(
[CosmosDBTrigger("Tasks", "Items", ConnectionStringSetting = "CosmosDBConnection",
DatabaseName = "Tasks", CollectionName = "Items", CreateLeaseCollectionIfNotExists = true)] IReadOnlyList inputDocuments,
[CosmosDB(
databaseName: "Tasks",
collectionName: "ProcessedItems",
ConnectionStringSetting = "CosmosDBConnection")] out dynamic outputDocument,
ILogger log)
{
log.LogInformation($"C# Script trigger function processed {inputDocuments.Count} documents.");
foreach (var doc in inputDocuments)
{
log.LogInformation($"Processing document ID: {doc.id}");
// Create a new document for the output collection
outputDocument = new {
id = Guid.NewGuid().ToString(),
originalId = doc.id,
processedData = $"Data from {doc.name} processed at {DateTime.UtcNow}"
};
}
}
}
public class MyDocument
{
public string id { get; set; }
public string name { get; set; }
}
Queue Storage Trigger and Output Bindings
Functions can be triggered by messages arriving in an Azure Queue Storage queue, and can also send messages to a queue.
import logging
import azure.functions as func
def main(myQueueItem: func.QueueMessage, outputQueue: func.Out[str]) -> None:
logging.info('Python queue trigger function processed a queue item: %s',
myQueueItem.get_body().decode('utf-8'))
message_body = myQueueItem.get_body().decode('utf-8')
outputQueue.set(f"Processed: {message_body}")
Benefits of Using Bindings
Increased Productivity: Focus on writing business logic rather than plumbing code.
Improved Readability: Function code becomes cleaner and easier to understand.
Reduced Complexity: Abstract away low-level service interactions.
Flexibility: Easily swap out data sources or triggers without significant code changes.
Tip: Explore the full list of supported bindings in the official Azure Functions documentation to understand how you can integrate with a wide array of Azure services.