Azure Functions triggers and bindings concepts
In this article, you learn the high-level concepts surrounding functions triggers and bindings.
Triggers cause a function to run. A trigger defines how a function is invoked and a function must have exactly one trigger. Triggers can also pass data into your function, as you would with method calls.
Binding to a function is a way of declaratively connecting your functions to other resources; bindings either pass data into your function (an input binding) or enable you to write data out from your function (an output binding) using binding parameters. Your function trigger is essentially a special type of input binding.
You can mix and match different bindings to suit your function's specific scenario. Bindings are optional and a function might have one or multiple input and/or output bindings.
Triggers and bindings let you avoid hardcoding access to other services. Your function receives data (for example, the content of a queue message) in function parameters. You send data (for example, to create a queue message) by using the return value of the function.
Consider the following examples of how you could implement different functions.
Example scenario | Trigger | Input binding | Output binding |
---|---|---|---|
A new queue message arrives which runs a function to write to another queue. | Queue* | None | Queue* |
A scheduled job reads Blob Storage contents and creates a new Azure Cosmos DB document. | Timer | Blob Storage | Azure Cosmos DB |
The Event Grid is used to read an image from Blob Storage and a document from Azure Cosmos DB to send an email. | Event Grid | Blob Storage and Azure Cosmos DB | SendGrid |
* Represents different queues
These examples aren't meant to be exhaustive, but are provided to illustrate how you can use triggers and bindings together. For a more comprehensive set of scenarios, see Azure Functions scenarios.
Tip
Functions doesn't require you to use input and output bindings to connect to Azure services. You can always create an Azure SDK client in your code and use it instead for your data transfers. For more information, see Connect to services.
Trigger and binding definitions
Triggers and bindings are defined differently depending on the development language. Make sure to select your language at the top of the article.
Bindings can be either input or output bindings. Not all services support both input and output bindings. See your specific binding extension for specific bindings code examples.
This example shows an HTTP triggered function with an output binding that writes a message to an Azure Storage queue.
For C# class library functions, triggers and bindings are configured by decorating methods and parameters with C# attributes, where the specific attribute applied might depend on the C# runtime model:
The HTTP trigger (HttpTrigger
) is defined on the Run
method for a function named HttpExample
that returns a MultiResponse
object:
[Function("HttpExample")]
public static MultiResponse Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req,
FunctionContext executionContext)
{
This example shows the MultiResponse
object definition which both returns an HttpResponse
to the HTTP request and also writes a message to a storage queue using a QueueOutput
binding:
public class MultiResponse
{
[QueueOutput("outqueue",Connection = "AzureWebJobsStorage")]
public string[] Messages { get; set; }
public HttpResponseData HttpResponse { get; set; }
}
For more information, see the C# isolated worker model guide.
Legacy C# Script functions use a function.json definition file. For more information, see the Azure Functions C# script (.csx) developer reference.
For Java functions, triggers and bindings are configured by annotating specific methods and parameters. This HTTP trigger (@HttpTrigger
) is defined on the run
method for a function named HttpTriggerQueueOutput
, which writes to a storage queue defined by the @QueueOutput
annotation on the message
parameter:
@FunctionName("HttpExample")
public HttpResponseMessage run(
@HttpTrigger(name = "req", methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS)
HttpRequestMessage<Optional<String>> request,
@QueueOutput(name = "msg", queueName = "outqueue",
connection = "AzureWebJobsStorage") OutputBinding<String> msg,
final ExecutionContext context) {
context.getLogger().info("Java HTTP trigger processed a request.");
For more information, see the Java developer guide.
The way that triggers and binding are defined for Node.js functions depends on the specific version of Node.js for Functions:
In Node.js for Functions version 4, you configure triggers and bindings using objects exported from the @azure/functions
module. For more information, see the Node.js developer guide.
This example is an HTTP triggered function that creates a queue item for each HTTP request received.
The http
method on the exported app
object defines an HTTP trigger, and the storageQueue
method on output
defines an output binding on this trigger.
const { app, output } = require('@azure/functions');
const queueOutput = output.storageQueue({
queueName: 'outqueue',
connection: 'MyStorageConnectionAppSetting',
});
app.http('httpTrigger1', {
methods: ['GET', 'POST'],
authLevel: 'anonymous',
extraOutputs: [queueOutput],
handler: async (request, context) => {
const body = await request.text();
context.extraOutputs.set(queueOutput, body);
return { body: 'Created queue item.' };
},
});
The http
method on the exported app
object defines an HTTP trigger, and the storageQueue
method on output
defines an output binding on this trigger.
import { app, HttpRequest, HttpResponseInit, InvocationContext, output } from '@azure/functions';
const queueOutput = output.storageQueue({
queueName: 'outqueue',
connection: 'MyStorageConnectionAppSetting',
});
export async function httpTrigger1(request: HttpRequest, context: InvocationContext): Promise<HttpResponseInit> {
const body = await request.text();
context.extraOutputs.set(queueOutput, body);
return { body: 'Created queue item.' };
}
app.http('httpTrigger1', {
methods: ['GET', 'POST'],
authLevel: 'anonymous',
extraOutputs: [queueOutput],
handler: httpTrigger1,
});
This example function.json
file defines the function:
{
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"name": "Request",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "Response"
},
{
"type": "queue",
"direction": "out",
"name": "msg",
"queueName": "outqueue",
"connection": "AzureWebJobsStorage"
}
]
}
For more information, see the PowerShell developer guide.
The way that the function is defined depends on the version of Python for Functions:
In Python for Functions version 2, you define the function directly in code using decorators.
app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS)
@app.route(route="HttpExample")
@app.queue_output(arg_name="msg", queue_name="outqueue", connection="AzureWebJobsStorage")
def HttpExample(req: func.HttpRequest, msg: func.Out [func.QueueMessage]) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
Add bindings to a function
You can connect your function to other services by using input or output bindings. Add a binding by adding its specific definitions to your function. To learn how, see Add bindings to an existing function in Azure Functions.
Supported bindings
This table shows the bindings that are supported in the major versions of the Azure Functions runtime:
Type | 1.x | 2.x and higher1 | Trigger | Input | Output |
---|---|---|---|---|---|
Blob storage | ✔ | ✔ | ✔ | ✔ | ✔ |
Azure Cosmos DB | ✔ | ✔ | ✔ | ✔ | ✔ |
Azure SQL | ✔ | ✔ | ✔ | ✔ | |
Dapr3 | ✔ | ✔ | ✔ | ✔ | |
Event Grid | ✔ | ✔ | ✔ | ✔ | |
Event Hubs | ✔ | ✔ | ✔ | ✔ | |
HTTP & webhooks | ✔ | ✔ | ✔ | ✔ | |
IoT Hub | ✔ | ✔ | ✔ | ||
Kafka2 | ✔ | ✔ | ✔ | ||
Mobile Apps | ✔ | ✔ | ✔ | ||
Notification Hubs | ✔ | ✔ | |||
Queue storage | ✔ | ✔ | ✔ | ✔ | |
RabbitMQ2 | ✔ | ✔ | ✔ | ||
SendGrid | ✔ | ✔ | ✔ | ||
Service Bus | ✔ | ✔ | ✔ | ✔ | |
SignalR | ✔ | ✔ | ✔ | ✔ | |
Table storage | ✔ | ✔ | ✔ | ✔ | |
Timer | ✔ | ✔ | ✔ |
1 Starting with the version 2.x runtime, all bindings except HTTP and Timer must be registered. See Register binding extensions.
2 Triggers aren't supported in the Consumption plan. Requires runtime-driven triggers.
3 Supported only in Kubernetes, IoT Edge, and other self-hosted modes only.
For information about which bindings are in preview or are approved for production use, see Supported languages.
Specific binding extension versions are only supported while the underlying service SDK is supported. Changes to support in the underlying service SDK version affect the support for the consuming extension.
Bindings code examples
Use the following table to find more examples of specific binding types that show you how to work with bindings in your functions. First, choose the language tab that corresponds to your project.
Service | Examples | Samples |
---|---|---|
Blob storage | Trigger Input Output |
Link |
Azure Cosmos DB | Trigger Input Output |
Link |
Azure SQL | Trigger (preview) Input Output |
Link |
Event Grid | Trigger Output |
Link |
Event Hubs | Trigger Output |
|
IoT Hub | Trigger Output |
|
HTTP | Trigger | Link |
Queue storage | Trigger Output |
Link |
RabbitMQ | Trigger Output |
|
SendGrid | Output | |
Service Bus | Trigger Output |
Link |
SignalR | Trigger Input Output |
|
Table storage | Input Output |
|
Timer | Trigger | Link |
Custom bindings
You can create custom input and output bindings. Bindings must be authored in .NET, but can be consumed from any supported language. For more information about creating custom bindings, see Creating custom input and output bindings.