Azure Functions Documentation

Understanding Triggers in Azure Functions

Triggers define how a function is invoked. When a trigger event occurs, Azure Functions runtime executes your function.

What are Triggers?

A trigger is an Azure entity that defines how a unit of work is initiated. In Azure Functions, a trigger is a type of binding that starts your function's execution. Think of it as the "event" that causes your function to run.

Types of Triggers

Azure Functions supports a wide variety of triggers, allowing you to integrate with numerous Azure services and external events. Some common trigger types include:

HTTP Trigger
Invokes your function when an HTTP request is received. Ideal for building web APIs and webhooks.
Timer Trigger
Executes your function on a schedule, typically defined by a CRON expression. Useful for recurring tasks.
Blob Trigger
Runs your function when a new or updated blob is detected in Azure Blob Storage.
Queue Trigger
Starts your function when a new message is added to an Azure Storage Queue.
Cosmos DB Trigger
Executes your function in response to changes in a Cosmos DB collection.
Event Grid Trigger
Responds to events published by Azure Event Grid.
Service Bus Trigger
Processes messages from Azure Service Bus queues or topics.

Configuring a Trigger

Triggers are configured in your function's function.json file (for JavaScript, C#, Python, etc.) or through attributes in your code (for C#). Each trigger type has specific configuration parameters.

Example: HTTP Trigger Configuration (function.json)


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

Example: Timer Trigger Configuration (function.json)


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

The schedule property uses a CRON expression. In this example, it means "every 5 minutes".

Trigger Payload

The data associated with the trigger event is passed to your function as input. The format of this payload depends on the trigger type.

  • An HTTP trigger might pass request details (headers, body, query parameters).
  • A Blob trigger might pass information about the newly created or updated blob.
  • A Queue trigger would pass the message content.

The Trigger `direction`

In the function.json configuration, triggers are defined with "direction": "in". This signifies that the binding is used to receive input and initiate the function's execution.

Key Benefits of Triggers

  • Event-Driven Architecture: Enables building reactive systems that respond to real-time events.
  • Decoupling: Separates the event source from the function logic, making systems more modular.
  • Scalability: Azure Functions automatically scales based on the number of incoming trigger events.
  • Simplified Integration: Provides built-in connectors for many services, reducing boilerplate code.