Azure Functions: Triggers and Bindings

Understanding Azure Functions Triggers and Bindings

Azure Functions provides a powerful event-driven serverless compute experience. At its core, the programming model revolves around two key concepts: Triggers and Bindings. These allow your functions to be executed in response to various events and to interact with other Azure services and external data sources with minimal code.

What are Triggers?

A trigger defines how a function is invoked. It's the event that starts the execution of your function. Each Azure Function must have exactly one trigger. Triggers can be based on a schedule (like a timer) or on an event from a particular service.

Common Trigger Types:
  • HTTP Trigger: Invokes a function when an HTTP request is received. Ideal for building web APIs and webhooks.
  • Timer Trigger: Invokes a function on a schedule, defined by a CRON expression. Useful for scheduled tasks.
  • Blob Trigger: Invokes a function when a blob is added, updated, or deleted in Azure Blob Storage.
  • Queue Trigger: Invokes a function when a message is added to an Azure Storage Queue.
  • Event Hub Trigger: Invokes a function in response to events from Azure Event Hubs.
  • Service Bus Trigger: Invokes a function when a message arrives in an Azure Service Bus queue or topic.

What are Bindings?

Bindings connect your function to other Azure services and data sources. They represent declarative ways to input data to your function and output data from your function. Bindings simplify your code by handling the complexities of interacting with these services.

There are two types of bindings:

Common Binding Types:
  • Blob Input/Output Binding: Reads from or writes to Azure Blob Storage.
  • Table Input/Output Binding: Reads from or writes to Azure Table Storage.
  • Queue Output Binding: Writes messages to an Azure Storage Queue.
  • Document DB/Cosmos DB Input/Output Binding: Interacts with Azure Cosmos DB.
  • Service Bus Queue/Topic Output Binding: Sends messages to Azure Service Bus.

How Triggers and Bindings Work Together

A trigger initiates function execution, and bindings provide the data the function needs to operate and a way to send results back. For example:

Example: HTTP Trigger with Blob Output Binding (JavaScript)

This function is triggered by an HTTP request. It takes a name from the query string and writes a greeting message to a blob in Azure Storage.


const { BlobServiceClient } = require("@azure/storage-blob");

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

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

    // --- Output Binding for Blob Storage ---
    // The blob name is dynamically set using the 'name' parameter
    const blobName = `greetings/${name || 'default'}.txt`;
    context.bindings.outputBlob = `Greeting for ${name || 'Guest'}: ${new Date().toISOString()}`;
    // --------------------------------------

    context.res = {
        // status: 200, /* Defaults to 200 */
        body: responseMessage
    };
};
                

In the function.json file, you would define the bindings:


{
  "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": "output-container/{name}.txt", // Path to the blob, can use tokens
      "connection": "AzureWebJobsStorage" // Name of the app setting for the connection string
    }
  ]
}
                

By leveraging triggers and bindings, you can significantly reduce the amount of boilerplate code required to build robust, scalable serverless applications on Azure Functions.