Azure Functions Bindings

Bindings are a declarative way to connect your Azure Functions to other Azure services and external data sources. They allow you to trigger your function based on events and to write data to other services without explicitly writing client SDK code.

Input and Output Bindings

Bindings are configured in the function.json file (for JavaScript, C#, F#, Java, and PowerShell) or through attributes (for C# and F#). There are two main categories of bindings:

Trigger Bindings

Trigger bindings define what initiates the execution of your function. Common triggers include:

Example: HTTP Trigger (function.json)

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

This configuration defines an HTTP trigger named req that accepts GET and POST requests, and an HTTP output binding named res to send the response.

Common Input and Output Bindings

These bindings simplify interacting with various Azure services:

Example: Blob Input and Queue Output (function.json)

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "name": "inputBlob",
      "type": "blob",
      "direction": "in",
      "path": "samples-workitems/{name}.txt",
      "connection": "AzureWebJobsStorage"
    },
    {
      "name": "outputQueue",
      "type": "queue",
      "direction": "out",
      "queueName": "output-queue",
      "connection": "AzureWebJobsStorage"
    }
  ]
}

This function has a blob input binding that reads a file named .txt from the samples-workitems container and a queue output binding that writes a message to the output-queue.

Example: Cosmos DB Input and Output (C# Attribute)

using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using System.Collections.Generic;
using System.Threading.Tasks;

public static class CosmosDbExample
{
    [FunctionName("CosmosDbFunction")]
    public static async Task Run(
        [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
        [CosmosDB(
            databaseName: "MyDatabase",
            collectionName: "MyCollection",
            ConnectionStringSetting = "CosmosDbConnection",
            Id = "{Query.id}")] Document inputDocument,
        [CosmosDB(
            databaseName: "MyDatabase",
            collectionName: "MyOutputCollection",
            ConnectionStringSetting = "CosmosDbConnection")]IAsyncCollector outputDocument,
        ILogger log)
    {
        log.LogInformation("C# HTTP trigger function processed a request.");

        if (inputDocument != null)
        {
            log.LogInformation($"Found document: {inputDocument.Id}");
            await outputDocument.AddAsync(inputDocument); // Write the input document to another collection
        }
        else
        {
            log.LogWarning("No document found with the provided ID.");
        }
    }
}

This C# example demonstrates using attributes to define Cosmos DB input and output bindings. The inputDocument reads a document based on a query parameter, and outputDocument writes the retrieved document to a different collection.

Bindings provide a powerful abstraction that reduces boilerplate code and makes your Azure Functions more maintainable and scalable. For detailed information on specific binding types, refer to the official Azure Functions documentation.