Introduction to Azure Functions Bindings
Azure Functions bindings provide a declarative way to connect your functions to Azure and other services. Instead of writing integration code yourself, you declare the bindings in your function's configuration, and the Functions runtime handles the connection and data management.
Bindings simplify development by abstracting away the complexities of service-specific SDKs and protocols. They allow you to focus on your business logic while the runtime manages the interactions with external resources.
There are two main categories of bindings:
- Trigger Bindings: Define how your function is invoked. Each function must have exactly one trigger.
- Input/Output Bindings: Define how your function accesses data from other services (input) or writes data to them (output).
Types of Bindings
Bindings are configured in the function.json file (for Node.js, Python, Java, and .NET Framework) or through attributes (for .NET). Each binding has a type, a direction (in for input, out for output, or implied for triggers), and other properties specific to the service it connects to.
Trigger Bindings
A trigger is a specific type of binding that controls when your function code executes. Triggers listen for events and initiate function execution when those events occur. An Azure Function must have exactly one trigger.
Common trigger types include:
- HTTP Trigger: Invokes a function via an HTTP request.
- Timer Trigger: Invokes a function on a schedule.
- Blob Trigger: Invokes a function when a blob is created or updated in Azure Blob Storage.
- Queue Trigger: Invokes a function when a message is added to an Azure Storage Queue.
- Event Grid Trigger: Invokes a function in response to an Event Grid event.
- Service Bus Trigger: Invokes a function when a message arrives in a Service Bus queue or topic.
Input & Output Bindings
Input and output bindings enable your function to read from and write to various Azure services and external data sources without explicit SDK code.
- Input Bindings: Load data from a service and make it available as a parameter in your function.
- Output Bindings: Allow your function to send data to a service, often by returning a value or assigning it to a specific parameter.
For example, a function triggered by an HTTP request might use an input binding to read data from a Cosmos DB document and an output binding to write a result to an Azure Queue.
Common Bindings in Detail
HTTP Trigger
The HTTP trigger allows your function to be invoked via an HTTP request. It also provides input bindings for accessing request details (like query parameters, headers, and body) and an output binding for sending an HTTP response.
Example function.json:
{
"scriptFile": "index.js",
"bindings": [
{
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "res"
}
]
}
Blob Storage
Bindings for Azure Blob Storage allow you to read from and write to blobs. You can configure input bindings to load blob content into your function parameters or output bindings to upload data as a new blob.
Example: Input binding for reading a blob
{
"scriptFile": "index.js",
"bindings": [
{
"name": "myBlob",
"type": "blob",
"direction": "in",
"path": "samples-workitems/{name}.txt",
"connection": "AzureWebJobsStorage"
},
{
"name": "outputBlob",
"type": "blob",
"direction": "out",
"path": "samples-workitems-output/{name}.txt",
"connection": "AzureWebJobsStorage"
},
{
"name": "req",
"type": "httpTrigger",
"direction": "in",
"methods": ["get"],
"authLevel": "function"
},
{
"name": "res",
"type": "http",
"direction": "out"
}
]
}
In JavaScript:
module.exports = async function (context, myBlob) {
context.log("JavaScript blob trigger function processed blob");
context.log("Content:", myBlob);
context.bindings.outputBlob = `Processed content: ${myBlob}`;
context.res = {
body: "Blob processed successfully!"
};
};
Queue Storage
Queue bindings enable functions to react to messages in an Azure Storage Queue (trigger) and to send messages to a queue (output).
Example: Queue trigger and output binding
{
"scriptFile": "index.js",
"bindings": [
{
"name": "message",
"type": "queueTrigger",
"direction": "in",
"queueName": "myqueue-items",
"connection": "AzureWebJobsStorage"
},
{
"name": "outputQueue",
"type": "queue",
"direction": "out",
"queueName": "myoutputqueue",
"connection": "AzureWebJobsStorage"
}
]
}
In JavaScript:
module.exports = async function (context, message) {
context.log('JavaScript queue trigger function');
context.log('Message:', message);
context.bindings.outputQueue = `Processed: ${message}`;
};
Cosmos DB
Cosmos DB bindings allow you to read documents from a Cosmos DB collection (input) or write documents to it (output).
Example: Input and output bindings for Cosmos DB
{
"scriptFile": "index.js",
"bindings": [
{
"name": "inputDocument",
"type": "cosmosDB",
"direction": "in",
"databaseName": "Tasks",
"collectionName": "Items",
"id": "{id}",
"partitionKey": "{id}",
"connectionStringSetting": "CosmosDBConnection"
},
{
"name": "outputDocument",
"type": "cosmosDB",
"direction": "out",
"databaseName": "Tasks",
"collectionName": "Items",
"connectionStringSetting": "CosmosDBConnection"
},
{
"name": "req",
"type": "httpTrigger",
"direction": "in",
"methods": ["post"],
"authLevel": "function"
},
{
"name": "res",
"type": "http",
"direction": "out"
}
]
}
Event Hubs
Bindings for Azure Event Hubs enable your functions to process events from an Event Hub (trigger) and send events to an Event Hub (output).
Service Bus
Service Bus bindings allow functions to consume messages from Service Bus queues or topics (trigger) and send messages to them (output).
Binding Expressions
Binding expressions are powerful placeholders that allow you to dynamically configure your bindings based on the input to your function. These expressions can reference properties of the trigger payload, other binding inputs, or even system variables.
Common examples include:
{queueTrigger}: The content of the queue message.{name}: A parameter from an HTTP trigger's URL path or query string.{sys.randguid}: A random GUID generated by the runtime.
These expressions are used in properties like path, id, and collectionName to make bindings context-aware.
Custom Bindings
If the built-in bindings don't meet your needs, you can create your own custom bindings. This allows you to integrate with virtually any service or protocol. Custom bindings can be developed in .NET, Java, or other languages that support the Azure Functions extensibility model.
API Reference Snippet (Illustrative)
| Type | Direction | Common Properties | Description |
|---|---|---|---|
httpTrigger |
in |
methods, authLevel |
Triggers function execution via HTTP requests. |
http |
out |
N/A | Represents the HTTP response. |
blob |
in |
path, connection |
Reads a blob from Azure Blob Storage. |
blob |
out |
path, connection |
Writes a blob to Azure Blob Storage. |
queueTrigger |
in |
queueName, connection |
Triggers function execution when a message arrives in an Azure Storage Queue. |
queue |
out |
queueName, connection |
Sends a message to an Azure Storage Queue. |
cosmosDB |
in |
databaseName, collectionName, id, connectionStringSetting |
Reads a document from Azure Cosmos DB. |
cosmosDB |
out |
databaseName, collectionName, connectionStringSetting |
Writes a document to Azure Cosmos DB. |