Azure Functions Host and Trigger Bindings

Last updated: October 26, 2023

Azure Functions relies heavily on its binding model to simplify the development of serverless applications. Bindings allow you to declaratively connect your function code to other Azure services and external data sources without requiring you to write boilerplate code for connecting, retrieving, or storing data. This document dives deep into the concepts of Azure Functions host and trigger bindings.

Understanding Host and Trigger Bindings

In Azure Functions, bindings are declared in your function's configuration file (function.json or using attributes in code). They are categorized into two main types:

The Role of the Host

The Azure Functions host is the runtime environment that manages the execution of your functions. It's responsible for:

The host is an integral part of the Azure Functions platform and works seamlessly with your function code and its defined bindings.

Trigger Bindings Explained

Triggers are the events that cause your function to execute. Common examples include:

Trigger Configuration Example (function.json)

Here's an example of an HTTP trigger configuration:

{
  "scriptFile": "run.py",
  "entryPoint": "main",
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    }
  ]
}
Key Properties:
  • type: The type of trigger (e.g., httpTrigger).
  • direction: Always in for triggers.
  • name: The name used to access the trigger data in your code.
  • methods (for HTTP trigger): Specifies allowed HTTP methods.
  • authLevel (for HTTP trigger): Defines the authentication level.

Input and Output Bindings

Beyond triggers, functions can use input and output bindings to interact with various services. These bindings make it easy to read data from and write data to other resources.

Common Input/Output Bindings:

Input Binding Configuration Example (function.json)

Reading data from a Cosmos DB collection:

{
  "scriptFile": "run.py",
  "entryPoint": "main",
  "bindings": [
    {
      "type": "httpTrigger",
      "direction": "in",
      "name": "req"
    },
    {
      "type": "cosmosDB",
      "direction": "in",
      "name": "inputDocument",
      "databaseName": "MyDatabase",
      "collectionName": "MyCollection",
      "id": "{id}",
      "partitionKey": "{id}",
      "connectionStringSetting": "CosmosDBConnection"
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    }
  ]
}

Output Binding Configuration Example (function.json)

Writing a message to an Azure Storage Queue:

{
  "scriptFile": "run.py",
  "entryPoint": "main",
  "bindings": [
    {
      "type": "httpTrigger",
      "direction": "in",
      "name": "req"
    },
    {
      "type": "queue",
      "direction": "out",
      "name": "outputQueueItem",
      "queueName": "myqueue-items",
      "connectionStringSetting": "AzureWebJobsStorage"
    }
  ]
}
Accessing Bindings in Code:

In your function code, you access these bindings using the name property defined in their configuration. For example, in Python, the req variable would hold the HTTP request, and inputDocument would hold the data retrieved from Cosmos DB.

Bindings and Development Languages

Azure Functions supports multiple programming languages. The way you configure and use bindings can vary slightly:

Example: Python Function with Bindings

A Python function that reads from Cosmos DB and returns an HTTP response:

import logging
import azure.functions as func

def main(req: func.HttpRequest, inputDocument: func.Document) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    if inputDocument:
        return func.HttpResponse(
            body=f"Document found: {inputDocument.id}",
            mimetype="text/plain",
            status_code=200
        )
    else:
        return func.HttpResponse(
             "Please pass an id in the query string or in the request body",
             status_code=400
        )

Conclusion

Host and trigger bindings are fundamental to the Azure Functions programming model. They abstract away complex integrations, allowing you to focus on writing business logic. By understanding and leveraging bindings, you can build efficient, scalable, and cost-effective serverless applications on Azure.