Azure Functions Triggers and Bindings

Azure Functions uses triggers and bindings to enable code-first development. Triggers and bindings let you develop your business logic in pure functions. When a function is triggered, Functions reads trigger data and makes that data available to your function in a structured format. Bindings allow you to easily connect to other Azure services and developer services, declarative in your function definition.

What are Triggers and Bindings?

In Azure Functions, a trigger defines how a function is invoked. A function must have exactly one trigger. A trigger can be a timer, an HTTP request, a message from a queue, or an event from another Azure service.

Bindings connect your function to other services without you needing to write explicit client code. Bindings can be used to inject data into your function (input bindings) or to send data from your function to another service (output bindings).

Types of Triggers and Bindings

Azure Functions supports a wide variety of triggers and bindings for many Azure services and external services. Some of the most common include:

  • HTTP Trigger: Invokes a function via an HTTP request. Useful for building web APIs and webhooks.
  • Timer Trigger: Invokes a function on a schedule defined by a cron expression.
  • Blob Trigger: Invokes a function when a new or updated blob is detected in Azure Blob Storage.
  • Queue Trigger: Invokes a function when a message is added to an Azure Storage Queue.
  • Service Bus Trigger: Invokes a function when a message is received from an Azure Service Bus queue or topic.
  • Cosmos DB Trigger: Invokes a function in response to changes in a Cosmos DB collection.
  • DocumentDB Trigger: (Legacy) Similar to Cosmos DB trigger for Azure DocumentDB.
  • Event Hub Trigger: Invokes a function when events are received from an Azure Event Hub.
  • Event Grid Trigger: Invokes a function in response to events published to Azure Event Grid.

Output bindings allow you to send data to services like:

  • Azure Blob Storage
  • Azure Table Storage
  • Azure Queue Storage
  • Azure Service Bus
  • Cosmos DB
  • SendGrid (for email)

How to Use Triggers and Bindings

Triggers and bindings are configured in the function.json file (for Node.js, Python, PowerShell, Java, and .NET Core) or through attributes in your code (for .NET Framework and C++).

Example: HTTP Trigger with an Output Binding to Blob Storage

Consider a function that is triggered by an HTTP request and writes a greeting to a blob in Azure Blob Storage.

{
  "scriptFile": "index.js",
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    },
    {
      "type": "blob",
      "direction": "out",
      "name": "outputBlob",
      "path": "greetings/{name}.txt",
      "connection": "AzureWebJobsStorage"
    }
  ]
}

In your function code (e.g., index.js):

module.exports = async function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request.');

    const name = (req.query.name || req.body.name);
    const responseMessage = name
        ? "Hello, " + name + ". This HTTP triggered function executed successfully."
        : "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.";

    context.bindings.outputBlob = `Hello from Azure Functions! Your name is ${name}.`;

    context.res = {
        status: 200,
        body: responseMessage
    };
};
Note: The path in the blob binding greetings/{name}.txt uses a placeholder {name} which is typically populated from the trigger data or HTTP request parameters.

Binding Expressions

Binding expressions are powerful placeholders that allow you to reference data from triggers, other bindings, or application settings. They are used in the function.json configuration or attribute properties.

Common examples include:

  • {trigger.data}: Accessing trigger payload.
  • {myQueueItem}: Accessing a queue message.
  • {name}: Referencing a route parameter or query string value from an HTTP trigger.
  • %MySetting%: Referencing an application setting.

Learn More

Explore the official Azure Functions Triggers and Bindings documentation for detailed information on specific triggers, bindings, and advanced configuration.