Azure Functions Triggers Explained

Triggers define how a function is invoked. When a trigger event occurs, Azure Functions runtime executes your function. Each trigger type has specific configuration requirements and payload structures.

What is a Trigger?

A trigger is a part of an Azure Functions binding that initiates the execution of a function. Triggers can be based on various events, such as an HTTP request, a new message in a queue, a file upload to Blob Storage, a scheduled time, or a change in a database.

Common Trigger Types

1. HTTP Trigger

The HTTP trigger allows your function to be invoked via an HTTP request. This is useful for building web APIs, webhooks, or any scenario where you need to respond to web requests.


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

2. Timer Trigger

The Timer trigger allows you to run a function on a schedule. You can define the schedule using a CRON expression.


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

The example above schedules the function to run every 5 minutes.

3. Blob Trigger

The Blob trigger executes a function when a new or updated blob is detected in Azure Blob Storage.


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

This trigger monitors the container named samples-workitems for new blobs. The {name} parameter captures the name of the blob, which can be passed to your function.

4. Queue Trigger

The Queue trigger executes a function when a new message arrives in an Azure Storage Queue.


{
  "queueName": "myqueue-items",
  "connection": "AzureWebJobsStorage",
  "bindings": [
    {
      "name": "myQueueItem",
      "type": "queueTrigger",
      "direction": "in",
      "queueName": "myqueue-items",
      "connection": "AzureWebJobsStorage"
    }
  ]
}
        

5. Cosmos DB Trigger

The Cosmos DB trigger executes a function when documents are created or updated in a Cosmos DB collection.


{
  "databaseName": "ToDoList",
  "collectionName": "Items",
  "connectionStringSetting": "CosmosDBConnectionString",
  "bindings": [
    {
      "name": "inputDocument",
      "type": "cosmosDBTrigger",
      "direction": "in",
      "databaseName": "ToDoList",
      "collectionName": "Items",
      "connectionStringSetting": "CosmosDBConnectionString",
      "createLeaseCollectionIfNotExists": true
    }
  ]
}
        

Key Considerations for Triggers

Note: The specific configuration details and available trigger types can evolve. Always refer to the official Azure Functions documentation for the most up-to-date information.