Azure Functions Bindings Configuration
This document provides a comprehensive guide to configuring bindings in Azure Functions. Bindings offer a declarative way to connect your function to other Azure services and data sources, simplifying your code by abstracting away complex connection logic.
Core Concepts
Bindings are defined in your function's configuration file, typically function.json. Each binding has a specific type, direction (in, out, or inout), and a set of properties that define how it interacts with the service it's connected to.
Binding Properties
Common properties include:
type: The type of binding (e.g.,httpTrigger,blob,queue).direction: Specifies whether the binding is an input (in), output (out), or both (inout).name: The name used in your function code to access the binding.connection: The name of an application setting that contains the connection string or key for the service.path: For file-based bindings (like blobs or queues), this specifies the container or queue name, and optionally a file pattern.
function.json Structure
A typical function.json file looks like this:
{
"bindings": [
{
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "res"
},
{
"type": "blob",
"direction": "in",
"name": "inputBlob",
"path": "samples-workitems/{name}.txt",
"connection": "AzureWebJobsStorage"
},
{
"type": "blob",
"direction": "out",
"name": "outputBlob",
"path": "samples-output/{name}.txt",
"connection": "AzureWebJobsStorage"
}
],
"disabled": false
}
Explanation of the Example
- The first binding is an
httpTrigger, namedreq, which receives HTTP requests. - The second binding is an
httpoutput binding, namedres, used to send HTTP responses. - The third binding is an input
blobbinding, namedinputBlob, configured to read a text file from thesamples-workitemscontainer. The{name}in the path acts as a placeholder that can be resolved from the trigger or other bindings. - The fourth binding is an output
blobbinding, namedoutputBlob, configured to write a text file to thesamples-outputcontainer. connection: "AzureWebJobsStorage"indicates that the connection string for these blob bindings is stored in the application setting namedAzureWebJobsStorage.
Common Configuration Scenarios
HTTP Trigger and Response
The fundamental binding for web APIs. The httpTrigger binding defines the URL path, HTTP methods, and authorization level. The http binding is used for the return value.
Queue Storage Binding
To process messages from a Queue Storage queue:
{
"bindings": [
{
"name": "myQueueItem",
"type": "queueTrigger",
"direction": "in",
"queueName": "myqueue-items",
"connection": "AzureWebJobsStorage"
}
]
}
To send messages to a Queue Storage queue:
{
"bindings": [
{
"name": "outputQueueItem",
"type": "queue",
"direction": "out",
"queueName": "myoutputqueue",
"connection": "AzureWebJobsStorage"
}
]
}
Blob Storage Binding
To read a blob:
{
"bindings": [
{
"name": "inputBlob",
"type": "blob",
"direction": "in",
"path": "input-container/{blobName}",
"connection": "AzureWebJobsStorage"
}
]
}
To write a blob:
{
"bindings": [
{
"name": "outputBlob",
"type": "blob",
"direction": "out",
"path": "output-container/{name}.json",
"connection": "AzureWebJobsStorage"
}
]
}
Cosmos DB Binding
To read a document from Cosmos DB:
{
"bindings": [
{
"name": "inputDocument",
"type": "cosmosDB",
"direction": "in",
"databaseName": "Tasks",
"collectionName": "Items",
"id": "{id}",
"partitionKey": "{partitionKey}",
"connectionStringSetting": "CosmosDBConnection"
}
]
}
To write a document to Cosmos DB:
{
"bindings": [
{
"name": "outputDocument",
"type": "cosmosDB",
"direction": "out",
"databaseName": "Tasks",
"collectionName": "Items",
"createIfNotExists": false,
"connectionStringSetting": "CosmosDBConnection"
}
]
}
Connection Strings and App Settings
Connection information for services is managed through application settings. For example, the connection or connectionStringSetting properties in your function.json refer to names of settings that you configure in your Azure Function App's configuration.
function.json. Always use application settings to securely manage your credentials.
Dynamic Paths and Parameters
Many bindings support dynamic path parameters using curly braces ({}). These parameters can be populated from:
- The trigger's payload (e.g., route parameters from an HTTP trigger, blob name).
- Other input bindings.
- System variables.
For example, in "path": "input-container/{blobName}", {blobName} will be replaced by the actual name of the blob that triggered the function.
Advanced Configuration Options
Depending on the binding type, you can configure additional options such as:
dataType: Specifies the data type of the binding (e.g.,string,binary,stream).collectionQuery: For Cosmos DB, allows for querying a collection.fixedRateRetry,exponentialBackoffRetry: Configure retry policies for certain bindings.
Refer to the specific binding documentation for a full list of configurable properties.
Explore the sidebar for detailed configuration examples of specific binding types.