Azure Functions Triggers and Bindings

This document provides a comprehensive guide to triggers and bindings in Azure Functions, a serverless compute service that enables you to run small pieces of code, called "functions," without worrying about managing infrastructure. Triggers and bindings simplify how you connect to other Azure services and resources.

Understanding Triggers and Bindings

In Azure Functions, a trigger defines how a function is invoked. A trigger is a specific type of binding that causes a function to execute. For example, an HTTP trigger causes a function to run when an HTTP request is received, while a timer trigger runs a function on a schedule.

Bindings provide a declarative way to connect to other Azure services and resources without requiring you to write complex integration code. Bindings allow you to pass data into and out of your function. You can use input bindings to read data from a service and output bindings to write data to a service.

Types of Triggers

Azure Functions supports a wide variety of triggers. Some common ones include:

  • HTTP Trigger: Invokes your function when an HTTP request is received.
  • Timer Trigger: Invokes your function on a schedule defined by a CRON expression.
  • Blob Trigger: Invokes your function when a blob is created or updated in Azure Blob Storage.
  • Queue Trigger: Invokes your function when a message is added to an Azure Storage Queue.
  • Event Hub Trigger: Invokes your function when events are received by an Azure Event Hub.
  • Service Bus Trigger: Invokes your function when a message is received by an Azure Service Bus Queue or Topic.

Types of Bindings

Bindings can be categorized as input, output, or trigger bindings. Here are some examples:

Binding Type Description Direction
Azure Blob Storage Connect to Azure Blob Storage to read or write blobs. Input/Output
Azure Cosmos DB Interact with Azure Cosmos DB documents. Input/Output
Azure Table Storage Read and write data to Azure Table Storage. Input/Output
Azure Queue Storage Send or receive messages from Azure Queue Storage. Input/Output
Service Bus Send or receive messages from Azure Service Bus. Input/Output

Configuring Triggers and Bindings

Triggers and bindings are configured in the function.json file for your function. This file defines the bindings that your function uses, including their type, direction, and connection settings.

Here's an example of a function.json for an HTTP-triggered function that reads a blob:


{
  "scriptFile": "run.csx",
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "blob",
      "direction": "in",
      "name": "myBlob",
      "path": "samples-workitems/{name}.txt",
      "connection": "AzureWebJobsStorage"
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    }
  ]
}
                

In this example:

  • The first binding defines an httpTrigger for incoming requests (direction: "in") named req.
  • The second binding defines a blob input binding (direction: "in") named myBlob, which reads a file from the samples-workitems container.
  • The third binding defines an HTTP output binding (direction: "out") named res for returning the HTTP response.

Using Triggers and Bindings in Code

Once configured in function.json, triggers and bindings are available as parameters in your function code. The name specified in the binding configuration (e.g., req, myBlob) is used to access the data provided by the binding.

Tip: When using bindings, Azure Functions runtime handles the boilerplate code for interacting with the connected service. This significantly simplifies your development process and allows you to focus on your core business logic.

Learn More