Bindings in Azure Functions
Bindings provide a declarative way to connect your Azure Functions to other services without having to write complex integration code. They define how your function is triggered and how it interacts with data.
What are Bindings?
Bindings in Azure Functions can be categorized into two main types:
- Triggers: These define how a function is invoked. Each function must have exactly one trigger. For example, an HTTP trigger starts a function when an HTTP request is received.
- Inputs & Outputs: These define how your function accesses data from other services (inputs) and how it writes data to other services (outputs).
Key Concepts
- Declarative Configuration: Bindings are configured in the
function.jsonfile (for JavaScript, C#, Python) or via attributes (for C#, Java, F#). This configuration tells the Functions runtime how to connect to services. - Code Abstraction: You don't need to write explicit code to connect to services like Azure Cosmos DB, Azure Blob Storage, or message queues. The bindings handle the underlying communication.
- Flexibility: A single function can have multiple input and output bindings, allowing for complex data flows.
Common Binding Types
Input Bindings
- Azure Blob Storage: Read blobs from a storage container.
- Azure Queue Storage: Read messages from a queue.
- Azure Table Storage: Read or query entities from a table.
- Azure Cosmos DB: Read documents from a Cosmos DB collection.
- Service Bus: Read messages from a Service Bus queue or topic.
- Event Hubs: Read events from an Event Hub.
Output Bindings
- Azure Blob Storage: Write blobs to a storage container.
- Azure Queue Storage: Send messages to a queue.
- Azure Table Storage: Insert or update entities in a table.
- Azure Cosmos DB: Write documents to a Cosmos DB collection.
- Service Bus: Send messages to a Service Bus queue or topic.
- Application Insights: Send telemetry data.
Example: Azure Blob Storage Input and Output Binding (function.json)
This example shows a function that reads an image from Blob Storage, processes it (conceptually), and writes a new image back.
{
"scriptFile": "index.js",
"bindings": [
{
"name": "myBlob",
"type": "blobTrigger",
"direction": "in",
"path": "samples-workitems/{name}.png",
"connection": "AzureWebJobsStorage"
},
{
"name": "inputImage",
"type": "blob",
"direction": "in",
"path": "samples-workitems/{name}.png",
"connection": "AzureWebJobsStorage"
},
{
"name": "outputImage",
"type": "blob",
"direction": "out",
"path": "samples-output/{name}-processed.png",
"connection": "AzureWebJobsStorage"
}
]
}
In this configuration:
myBlobis the trigger that starts the function when a new PNG file arrives in thesamples-workitemscontainer.inputImageis an input binding that makes the content of the trigger blob available to the function.outputImageis an output binding that allows the function to write data to a blob in thesamples-outputcontainer.