Azure Functions Documentation

Azure Functions: Triggers and Bindings

Azure Functions provide a powerful and flexible way to build event-driven applications. At the core of this model are Triggers and Bindings. They abstract away the complexity of integrating with other Azure services and external systems, allowing you to focus on your business logic.

What are Triggers?

A trigger is what causes a function to execute. Every Azure Function must have exactly one trigger. Triggers define how a function is initiated, and they can be based on a variety of events, such as:

Each trigger type comes with a specific set of configuration options and input parameters. For example, an HTTP trigger allows you to define routes, methods (GET, POST, etc.), and accepts request details like headers and body.

What are Bindings?

Bindings allow you to declaratively connect your function to other Azure services and data sources without writing boilerplate integration code. They simplify input and output operations. You can think of bindings as defining the inputs to your function and the outputs from your function.

Bindings can be configured in the function.json file or using attributes in your code. This declarative approach makes your function code cleaner and easier to understand.

Key Concept: Event-Driven Architecture

Triggers and bindings are fundamental to building event-driven systems with Azure Functions. They enable your code to react to events and interact with various services seamlessly.

Common Trigger and Binding Types

Azure Functions supports a rich set of triggers and bindings. Here are some of the most commonly used:

HTTP Trigger

Executes your function in response to an HTTP request. Perfect for building APIs and webhooks.

function.json snippet for HTTP Trigger:
{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    }
  ]
}

Blob Trigger

Executes your function when a new or updated blob is detected in Azure Blob Storage.

function.json snippet for Blob Trigger:
{
  "scriptFile": "main.js",
  "bindings": [
    {
      "name": "myBlob",
      "type": "blobTrigger",
      "direction": "in",
      "path": "samples-workitems/{name}",
      "connection": "AzureWebJobsStorage"
    }
  ]
}

Queue Trigger

Executes your function in response to a message being added to an Azure Storage Queue.

function.json snippet for Queue Trigger:
{
  "scriptFile": "run.ps1",
  "bindings": [
    {
      "name": "myQueueItem",
      "type": "queueTrigger",
      "direction": "in",
      "queueName": "myqueue-items",
      "connection": "AzureWebJobsStorage"
    }
  ]
}

Timer Trigger

Executes your function on a schedule defined by a CRON expression.

function.json snippet for Timer Trigger:
{
  "scriptFile": "handler.go",
  "bindings": [
    {
      "name": "myTimer",
      "type": "timerTrigger",
      "direction": "in",
      "schedule": "0 */5 * * * *"
    }
  ]
}

Cosmos DB Input Binding

Fetches a document from Cosmos DB based on trigger data.

function.json snippet for Cosmos DB Input Binding:
{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "name": "inputDocument",
      "type": "cosmosDB",
      "direction": "in",
      "databaseName": "Tasks",
      "collectionName": "Items",
      "connectionStringSetting": "CosmosDBConnection",
      "id": "{QueueTrigger}",
      "partitionKey": "{QueueTrigger}"
    },
    {
      "name": "myQueueItem",
      "type": "queueTrigger",
      "direction": "in",
      "queueName": "myqueue-items",
      "connection": "AzureWebJobsStorage"
    }
  ]
}

Table Storage Output Binding

Writes data to Azure Table Storage.

function.json snippet for Table Storage Output Binding:
{
  "scriptFile": "main.js",
  "bindings": [
    {
      "name": "myQueueItem",
      "type": "queueTrigger",
      "direction": "in",
      "queueName": "myqueue-items",
      "connection": "AzureWebJobsStorage"
    },
    {
      "name": "outputTable",
      "type": "table",
      "direction": "out",
      "tableName": "Orders",
      "connection": "AzureWebJobsStorage"
    }
  ]
}

Declarative Configuration

Bindings are configured in the function.json file (for JavaScript, Python, PowerShell, etc.) or using attributes (for C#, Java). This configuration links your code to Azure services without requiring explicit SDK calls within your function logic for common operations.

For example, a Python function with a Blob trigger and a Blob output binding might look like this:

# __init__.py
import logging
import azure.functions as func

def main(myBlob: func.InputStream, outputBlob: func.Out[str]):
    logging.info(f"Python blob trigger function processed blob\n"
                 f"Name: {myBlob.name}\n"
                 f"Size: {myBlob.length} Bytes")

    content = myBlob.read()
    processed_content = content.decode('utf-8').upper()
    outputBlob.set(f"Processed content: {processed_content}")
    logging.info("Blob content processed and written to output.")

Advantages of Triggers and Bindings