Azure Functions Host and Trigger Bindings
Last updated: October 26, 2023
Azure Functions relies heavily on its binding model to simplify the development of serverless applications. Bindings allow you to declaratively connect your function code to other Azure services and external data sources without requiring you to write boilerplate code for connecting, retrieving, or storing data. This document dives deep into the concepts of Azure Functions host and trigger bindings.
Understanding Host and Trigger Bindings
In Azure Functions, bindings are declared in your function's configuration file (function.json
or using attributes in code). They are categorized into two main types:
- Trigger Bindings: These define how a function is invoked. A function must have exactly one trigger binding. The trigger determines when your function runs, and it can also provide input data to your function.
- Input and Output Bindings: These allow you to interact with other services or data sources. Input bindings read data into your function, and output bindings write data from your function.
The Role of the Host
The Azure Functions host is the runtime environment that manages the execution of your functions. It's responsible for:
- Loading your functions
- Managing triggers and detecting events
- Executing your function code
- Handling input and output bindings
- Managing scaling and resources
The host is an integral part of the Azure Functions platform and works seamlessly with your function code and its defined bindings.
Trigger Bindings Explained
Triggers are the events that cause your function to execute. Common examples include:
- HTTP Trigger: Invokes your function when an HTTP request is received.
- Timer Trigger: Invokes your function on a defined schedule.
- Blob Trigger: Invokes your function when a blob is added or updated in Azure Blob Storage.
- Queue Trigger: Invokes your function when a new message is available in an Azure Storage Queue.
- Cosmos DB Trigger: Invokes your function when documents change in a Cosmos DB collection.
Trigger Configuration Example (function.json
)
Here's an example of an HTTP trigger configuration:
{
"scriptFile": "run.py",
"entryPoint": "main",
"bindings": [
{
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "res"
}
]
}
type
: The type of trigger (e.g.,httpTrigger
).direction
: Alwaysin
for triggers.name
: The name used to access the trigger data in your code.methods
(for HTTP trigger): Specifies allowed HTTP methods.authLevel
(for HTTP trigger): Defines the authentication level.
Input and Output Bindings
Beyond triggers, functions can use input and output bindings to interact with various services. These bindings make it easy to read data from and write data to other resources.
Common Input/Output Bindings:
- Azure Blob Storage: Read and write files.
- Azure Cosmos DB: Read and write documents.
- Azure Service Bus: Send and receive messages.
- Azure Event Hubs: Send and receive event data.
- Azure Table Storage: Read and write table entities.
Input Binding Configuration Example (function.json
)
Reading data from a Cosmos DB collection:
{
"scriptFile": "run.py",
"entryPoint": "main",
"bindings": [
{
"type": "httpTrigger",
"direction": "in",
"name": "req"
},
{
"type": "cosmosDB",
"direction": "in",
"name": "inputDocument",
"databaseName": "MyDatabase",
"collectionName": "MyCollection",
"id": "{id}",
"partitionKey": "{id}",
"connectionStringSetting": "CosmosDBConnection"
},
{
"type": "http",
"direction": "out",
"name": "res"
}
]
}
Output Binding Configuration Example (function.json
)
Writing a message to an Azure Storage Queue:
{
"scriptFile": "run.py",
"entryPoint": "main",
"bindings": [
{
"type": "httpTrigger",
"direction": "in",
"name": "req"
},
{
"type": "queue",
"direction": "out",
"name": "outputQueueItem",
"queueName": "myqueue-items",
"connectionStringSetting": "AzureWebJobsStorage"
}
]
}
In your function code, you access these bindings using the name
property defined in their configuration. For example, in Python, the req
variable would hold the HTTP request, and inputDocument
would hold the data retrieved from Cosmos DB.
Bindings and Development Languages
Azure Functions supports multiple programming languages. The way you configure and use bindings can vary slightly:
- C#: Uses attributes (e.g.,
[HttpTrigger]
,[BlobInput]
) directly on method parameters. - JavaScript/TypeScript: Bindings are defined in
function.json
and accessed through thecontext.bindings
object. - Python: Bindings are defined in
function.json
and parameters are typed in the function signature. - Java: Uses annotations.
- PowerShell: Bindings are defined in
function.json
and accessed via$TriggerMetadata
and$MyBindingName
.
Example: Python Function with Bindings
A Python function that reads from Cosmos DB and returns an HTTP response:
import logging
import azure.functions as func
def main(req: func.HttpRequest, inputDocument: func.Document) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
if inputDocument:
return func.HttpResponse(
body=f"Document found: {inputDocument.id}",
mimetype="text/plain",
status_code=200
)
else:
return func.HttpResponse(
"Please pass an id in the query string or in the request body",
status_code=400
)
Conclusion
Host and trigger bindings are fundamental to the Azure Functions programming model. They abstract away complex integrations, allowing you to focus on writing business logic. By understanding and leveraging bindings, you can build efficient, scalable, and cost-effective serverless applications on Azure.