Input Bindings
Simplify data access and integration in your Azure Functions.
What are Input Bindings?
Input bindings in Azure Functions allow you to declaratively connect your function to other Azure services and external data sources. Instead of writing boilerplate code to retrieve data, you define an input binding in your function's configuration (function.json for C#, JavaScript, and Python, or attributes in C#). The Functions runtime then handles the data retrieval, making your function code cleaner and more focused on business logic.
When a function executes, the runtime automatically provides the data specified by the input bindings as parameters to your function. This abstraction significantly reduces the amount of code you need to write for common data access patterns.
Common Input Binding Scenarios
Input bindings are used to:
- Retrieve configuration settings from Azure App Configuration.
- Fetch documents from Azure Cosmos DB.
- Read messages from Azure Service Bus queues or topics.
- Access files in Azure Blob Storage.
- Get data from SQL databases.
- And many more integrations!
How Input Bindings Work
You define bindings in your function.json file (or using attributes in code). Each binding has a type, a direction (which is in for input bindings), a name (which corresponds to the parameter name in your function), and other properties specific to the service being integrated.
Example: Azure Cosmos DB Input Binding (function.json)
{
"scriptFile": "run.csx",
"bindings": [
{
"name": "req",
"type": "httpTrigger",
"direction": "in",
"methods": [
"get",
"post"
],
"authLevel": "function"
},
{
"name": "id",
"type": "httpTrigger",
"direction": "in",
"route": "items/{id}"
},
{
"name": "item",
"type": "cosmosDB",
"direction": "in",
"databaseName": "ToDoList",
"collectionName": "Items",
"connectionStringSetting": "CosmosDBConnection",
"id": "{id}"
},
{
"name": "$return",
"type": "http",
"direction": "out"
}
]
}
Corresponding Function Code (C# Script - run.csx)
#r "Newtonsoft.Json"
using System.Net;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Newtonsoft.Json;
public static class GetItem
{
public static async Task Run(HttpRequestMessage req, dynamic item, string id, TraceWriter log)
{
log.Info($"C# HTTP trigger function processed a request for item ID: {id}");
if (item == null)
{
log.Warning($"Item with ID '{id}' not found.");
return req.CreateResponse(HttpStatusCode.NotFound, $"Item with ID '{id}' not found.");
}
log.Info($"Retrieved item: {JsonConvert.SerializeObject(item)}");
return req.CreateResponse(HttpStatusCode.OK, item);
}
}
In this example:
- The
itemparameter in the function directly receives the document retrieved from Azure Cosmos DB based on theidextracted from the HTTP route. - The
cosmosDBbinding is configured to connect to the specified database and collection using a connection string setting.
Supported Input Binding Types
Azure Functions supports a wide range of input bindings. Here are some of the most commonly used:
| Binding Type | Description | Example Service |
|---|---|---|
blob |
Reads data from an Azure Blob Storage container. | Azure Blob Storage |
queue |
Retrieves a message from an Azure Storage queue. | Azure Storage Queues |
serviceBus |
Retrieves a message from an Azure Service Bus queue or topic. | Azure Service Bus |
cosmosDB |
Retrieves a document or collection from Azure Cosmos DB. | Azure Cosmos DB |
documentdb |
Legacy binding for Azure Cosmos DB. | Azure Cosmos DB |
table |
Reads an entity from an Azure Table Storage table. | Azure Table Storage |
appSettings |
Reads application settings. | Azure Functions Host |
secret |
Reads secrets from Azure Key Vault. | Azure Key Vault |
eventGrid |
Receives events from Azure Event Grid. | Azure Event Grid |
For a comprehensive list and detailed configuration, please refer to the official Azure Functions binding documentation.
Advantages of Using Input Bindings
- Reduced Boilerplate Code: Eliminates manual data fetching and client setup.
- Improved Readability: Function code focuses on core logic, not infrastructure.
- Declarative Configuration: Bindings are defined externally, making them easy to modify.
- Platform Abstraction: The runtime handles the complexities of interacting with various services.
- Enhanced Productivity: Faster development cycles and easier maintenance.