Triggers and Input Bindings
Azure Functions provides a powerful and extensible programming model that uses bindings to allow you to declaratively connect to data sources and services without writing complex integration code. Bindings make it easy to declare how functions should be programming-entered or exited, and which data should be readily available in function parameters.
There are two main types of bindings:
- Triggers: Define how a function is invoked. Every function must have exactly one trigger.
- Input Bindings: Provide data to your function from a connected service.
- Output Bindings: Send data from your function to a connected service.
Triggers
A trigger is an event that causes a function to execute. For example, a new message arriving in a storage queue, an HTTP request, or a schedule expiration can all be triggers. Each function can have only one trigger.
Common Triggers:
- HTTP Trigger: Invoked by an HTTP request.
- Timer Trigger: Invoked on a schedule (e.g., every 5 minutes).
- Blob Trigger: Invoked when a blob is added or updated in Azure Blob Storage.
- Queue Trigger: Invoked when a message arrives in an Azure Storage Queue.
- Event Grid Trigger: Invoked by events published to Azure Event Grid.
The trigger definition specifies the event that starts your function, and can also define configuration options like polling intervals or schedules.
Input Bindings
Input bindings allow you to easily access data from other Azure services or external data sources within your function. Instead of manually writing code to fetch data, you declare an input binding, and the Azure Functions runtime provides the data to your function as a parameter.
Example: Blob Input Binding
Imagine you have a function that needs to process the content of a text file stored in Azure Blob Storage. With an input binding, you can configure your function to automatically download the blob content into a string variable.
{
"scriptFile": "__init__.py",
"bindings": [
{
"name": "req",
"type": "httpTrigger",
"direction": "in",
"methods": [
"get",
"post"
]
},
{
"name": "outputBlob",
"type": "blob",
"direction": "out",
"path": "output-container/{name}.txt",
"connection": "AzureWebJobsStorage"
},
{
"name": "inputBlob",
"type": "blob",
"direction": "in",
"path": "input-container/{name}.txt",
"connection": "AzureWebJobsStorage"
}
]
}
In the example above:
- The
inputBlobbinding is configured withdirection: "in", indicating it's an input binding. - The
pathspecifies the blob to retrieve (e.g.,input-container/my-file.txt). - The
connectionproperty refers to an app setting that contains the connection string for Azure Storage.
Within your function code, the content of the specified blob will be available in a parameter named inputBlob.
Benefits of Using Input Bindings:
- Simplicity: Reduces boilerplate code for data access.
- Declarative: Configuration is done in the
function.jsonfile, separating concerns. - Performance: The runtime can optimize data retrieval.
- Consistency: Provides a uniform way to interact with various services.
Common Input Bindings:
- Blob Input: Reads a blob from Azure Blob Storage.
- Queue Input: Retrieves a message from an Azure Storage Queue.
- Table Input: Retrieves a table entity from Azure Table Storage.
- Cosmos DB Input: Retrieves documents from Azure Cosmos DB.
- Service Bus Queue/Topic Input: Reads messages from Azure Service Bus.
Binding Expressions
Many binding configurations use binding expressions to dynamically determine values. These expressions can reference properties of the trigger, other bindings, or configuration settings.
For example, in an HTTP trigger, you can use binding expressions like {Query.name} to get a query parameter or {Headers.Content-Type} to access request headers.