Bindings for Azure Functions
Bindings are a declarative way to connect to data sources and services. Bindings reduce the amount of boilerplate code you need to write in your function. They let you declare how functions are connected to external data and events, and how functions execute, without needing to write code to interact with those other services.
Types of Bindings
Bindings can be categorized as either triggers or other bindings:
- Triggers: A trigger is a specific type of input binding that causes a function to execute. Each function must have exactly one trigger.
- Input Bindings: Define data to be passed into your function from an external service.
- Output Bindings: Define data to be passed out of your function to an external service.
Common Bindings
Azure Functions supports a wide variety of bindings for popular Azure services and external systems:
Binding Type | Description | Example Use Cases |
---|---|---|
HTTP Trigger | Executes a function in response to an HTTP request. | Web APIs, webhook handlers. |
Timer Trigger | Executes a function on a schedule defined by a cron expression. | Scheduled tasks, background jobs. |
Blob Storage Input/Output | Reads from or writes to Azure Blob Storage. | Processing uploaded files, saving function output. |
Queue Storage Input/Output | Reads from or writes to Azure Queue Storage. | Decoupling services, message processing. |
Cosmos DB Input/Output | Reads from or writes to Azure Cosmos DB. | Data persistence, document retrieval. |
Service Bus Input/Output | Reads from or writes to Azure Service Bus queues or topics. | Enterprise messaging patterns. |
Event Hubs Input/Output | Reads from or writes to Azure Event Hubs. | Real-time data streaming, IoT data processing. |
Table Storage Input/Output | Reads from or writes to Azure Table Storage. | Storing simple structured data. |
Binding Configuration
Bindings are typically configured in a function.json
file (for Node.js, Python, Java, etc.) or through attributes in code (for .NET).
Example: function.json
for an HTTP Trigger with a Blob Output
{
"scriptFile": "index.js",
"bindings": [
{
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "res"
},
{
"type": "blob",
"direction": "out",
"name": "outputBlob",
"path": "output-container/{rand-guid}.txt",
"connection": "AzureWebJobsStorage"
}
]
}
In this example:
- The first binding defines an
httpTrigger
as an input. - The second binding defines an
http
output for the response. - The third binding defines a
blob
output that writes to a file in theoutput-container
directory, using a random GUID for the filename, and uses the default storage connection string.
name
property in a binding definition corresponds to the parameter name in your function code.
Custom Bindings
For scenarios not covered by built-in bindings, you can create custom bindings to integrate with your own services or specific protocols.