Azure Functions Trigger Bindings

Trigger bindings in Azure Functions define how a function is invoked. They allow you to react to events from various Azure services or external sources without writing extensive boilerplate code for event handling.

Understanding Triggers

A trigger is a type of binding that executes your function code in response to an event. Each function must have exactly one trigger. The trigger configuration is defined in the function.json file (for JavaScript, Python, PowerShell, etc.) or through attributes/decorators (for C#, Java).

Common Trigger Types

Azure Functions supports a wide range of triggers. Here are some of the most frequently used:

1. HTTP Trigger

The HTTP trigger allows your function to be invoked via an HTTP request. This is ideal for building web APIs, webhooks, or simple request-response services.

Example (function.json for JavaScript)

{
  "scriptFile": "index.js",
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    }
  ]
}

This configuration defines an HTTP trigger named req that accepts GET and POST requests and an HTTP output binding named res.

2. Timer Trigger

The timer trigger allows you to run functions on a schedule, similar to cron jobs. It uses a CRON expression to define the timing.

Example (function.json for JavaScript)

{
  "scriptFile": "index.js",
  "bindings": [
    {
      "type": "timerTrigger",
      "direction": "in",
      "name": "myTimer",
      "schedule": "0 */5 * * * *"
    }
  ]
}

This timer trigger fires every 5 minutes. The schedule property uses a CRON expression. The myTimer object will contain information about the scheduled execution.

3. Queue Trigger

The queue trigger invokes your function when a new message is added to an Azure Storage Queue. This is useful for processing background tasks.

Example (function.json for JavaScript)

{
  "scriptFile": "index.js",
  "bindings": [
    {
      "name": "myQueueItem",
      "type": "queueTrigger",
      "direction": "in",
      "queueName": "my-queue-items",
      "connection": "AzureWebJobsStorage"
    }
  ]
}

This trigger listens for messages on the my-queue-items queue, using the default Azure Storage connection string defined in application settings.

4. Blob Trigger

The blob trigger invokes your function when a new or updated blob is detected in an Azure Blob Storage container. This is commonly used for processing file uploads.

Example (function.json for JavaScript)

{
  "scriptFile": "index.js",
  "bindings": [
    {
      "name": "myBlob",
      "type": "blobTrigger",
      "direction": "in",
      "path": "samples-workitems/{name}",
      "connection": "AzureWebJobsStorage"
    }
  ]
}

This trigger monitors the samples-workitems container. The path with the {name} wildcard captures the name of the blob, making it available in your function.

Configuring Triggers

The configuration for each trigger varies depending on its type. Key parameters often include:

Best Practice: Use application settings for connection strings and sensitive information rather than hardcoding them directly in your configuration files.

Trigger vs. Input Bindings

It's important to distinguish between triggers and input bindings. Triggers are responsible for invoking your function, while input bindings are used to read data from other services within your function's execution context. A function has one trigger and zero or more input/output bindings.

Learn More

Explore the specific documentation for each trigger type to understand its capabilities and configuration options: