Azure Functions

Serverless computing for modern applications

Azure Functions: Triggers and Bindings

Welcome to the Azure Functions tutorial on triggers and bindings. This guide will walk you through the core concepts that make Azure Functions so powerful and flexible.

What are Triggers?

A trigger is what causes a function to execute. Each Azure Function has a single trigger. Triggers define how a function is invoked and provide input data to the function.

Common triggers include:

What are Bindings?

Bindings allow your function to connect to other Azure services and SaaS offerings without requiring you to write complex integration code. They represent an abstraction layer that makes it easy to declaratively connect to various data sources and services.

Bindings come in two main forms:

Key Benefits

Example: HTTP Trigger with an Output Binding

Let's consider a simple example where an HTTP-triggered function writes a message to an Azure Storage Queue.

Function.json (Configuration)


{
  "scriptFile": "run.py",
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    },
    {
      "type": "queue",
      "direction": "out",
      "name": "outputQueueItem",
      "queueName": "my-output-queue",
      "connection": "AzureWebJobsStorage"
    }
  ]
}
        

Python Code (Example Logic)


import logging
import azure.functions as func

def main(req: func.HttpRequest, outputQueueItem: func.Out[str]) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    name = req.params.get('name')
    if not name:
        try:
            req_body = req.get_json()
        except ValueError:
            pass
        else:
            name = req_body.get('name')

    if name:
        message = f"Hello, {name}! This message is being sent to the queue."
        outputQueueItem.set(message)
        return func.HttpResponse(
             "Please pass a name on the query string or in the request body. Message sent to queue.",
             status_code=200
        )
    else:
        return func.HttpResponse(
             "Please pass a name on the query string or in the request body.",
             status_code=400
        )
        

Common Service Integrations

Azure Blob Storage

Interact with files and data in Blob Storage.

  • Triggers: Blob Trigger
  • Bindings: Blob Input/Output

Azure Cosmos DB

Read from and write to NoSQL documents.

  • Bindings: Cosmos DB Input/Output

Azure Service Bus

Work with queues and topics for reliable messaging.

  • Triggers: Service Bus Trigger
  • Bindings: Service Bus Input/Output

Azure Event Hubs

Ingest high-throughput telemetry data.

  • Triggers: Event Hubs Trigger
  • Bindings: Event Hubs Input/Output

Azure Table Storage

Store and query large amounts of structured, non-relational data.

  • Bindings: Table Input/Output

SendGrid

Send emails programmatically.

  • Bindings: SendGrid Output

Next Steps

Explore the HTTP Trigger documentation and the full list of bindings to see how you can integrate with your favorite services.