Azure Functions Bindings Overview

Bindings enable you to declaratively connect your function to other services without having to explicitly write plumbing code. Bindings simplify your code by separating the concerns of the business logic from the concerns of connecting to other services.

This overview explains the concept of bindings in Azure Functions and provides a high-level look at the different types of bindings available. For detailed information on specific bindings, refer to the individual binding reference documentation.

What are Bindings?

In Azure Functions, bindings provide a way to easily integrate your function with Azure services and other external services. Think of them as declarative connectors that define how your function input and output data are mapped to and from these services.

Bindings operate in two directions:

By using bindings, you can:

Types of Bindings

Azure Functions supports a wide variety of bindings, categorized by the services they connect to. Here are some of the most common categories:

Trigger Bindings

A trigger is a special type of input binding that determines when your function should execute. When the trigger condition is met, the function runtime is invoked.

Trigger Type Description Example
HTTP Trigger Executes your function when an HTTP request is received. @HttpTrigger(methods=["get", "post"], authLevel="anonymous")
Timer Trigger Executes your function on a schedule defined by a CRON expression. @TimerTrigger(schedule = "0 */5 * * * *")
Blob Trigger Executes your function when a new or updated blob is detected in Azure Blob Storage. @BlobTrigger(path = "samples-workitems/{name}")
Queue Trigger Executes your function when a new message is added to an Azure Storage Queue. @QueueTrigger(name = "myQueueItem", queueName = "myqueue-items")
Event Grid Trigger Executes your function in response to events published to Azure Event Grid. @EventGridTrigger
Cosmos DB Trigger Executes your function when documents are added or modified in a Cosmos DB collection. @CosmosDBTrigger(name = "documents", databaseName = "ToDoList", collectionName = "Items")

Input and Output Bindings

These bindings allow your function to read data from or write data to various services.

Binding Type Description Example (Input) Example (Output)
Blob Storage Read from or write to Azure Blob Storage. @BlobInput(path = "samples-data/{filename}") @BlobOutput(path = "samples-output/{name}.txt")
Table Storage Read from or write to Azure Table Storage. @TableInput(tableName = "Products", rowKey = "{row}", partitionKey = "{partition}") @TableOutput(tableName = "Logs")
Document DB / Cosmos DB Read documents from or write documents to Azure Cosmos DB. @CosmosDBInput(databaseName = "db", collectionName = "items", id = "{id}") @CosmosDBOutput(databaseName = "db", collectionName = "processeditems")
Service Bus Send or receive messages from Azure Service Bus queues or topics. @ServiceBusQueueTrigger(name = "message", queueName = "inputqueue") @ServiceBusOutput(queueName = "outputqueue")
Event Hubs Read events from or send events to Azure Event Hubs. @EventHubTrigger(name = "event", eventHubName = "myEventHub") @EventHubOutput(eventHubName = "outputEventHub")
Twilio Send SMS messages via Twilio. N/A @TwilioSmsOutput(to = "+1234567890", message = "Hello from Functions")
SendGrid Send emails via SendGrid. N/A @SendGridOutput(to = "recipient@example.com", subject = "Function Email")

How Bindings Work

When you define bindings in your function's configuration (typically in a function.json file or using attributes in code), the Azure Functions runtime handles the interaction with the connected services.

For example, with an HTTP trigger and a Blob output binding, the process might look like this:

  1. An HTTP request arrives, triggering the function.
  2. The HTTP trigger binding extracts relevant data from the request (e.g., query parameters, request body).
  3. Your function code executes, processes the input data, and perhaps generates some output data.
  4. The Blob output binding takes the data returned by your function and writes it as a new blob to the specified location in Azure Blob Storage.
Tip: Bindings can significantly simplify your code. Instead of manually writing code to interact with Azure Storage, Service Bus, or other services, you can let the Azure Functions runtime manage these interactions for you.

Configuration

Bindings are configured in one of two ways:

Example using Attributes (C#)

This C# example shows an HTTP trigger with a Blob output binding:

using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;

public static class HttpBlobOutput
{
    [FunctionName("HttpBlobOutput")]
    public static void Run(
        [HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequest req,
        [Blob("output-container/{rand-guid}.txt", FileAccess.Write)] out string outputBlob,
        ILogger log)
    {
        log.LogInformation("C# HTTP trigger function processed a request.");

        string requestBody = new StreamReader(req.Body).ReadToEnd();
        outputBlob = $"Received: {requestBody}";

        log.LogInformation($"Output blob content set to: {outputBlob}");
    }
}

Example using function.json (JavaScript)

This JavaScript example demonstrates the equivalent configuration using function.json:

index.js

module.exports = async function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request.');

    const requestBody = req.body;
    context.bindings.outputBlob = `Received: ${requestBody}`;

    context.res = {
        status: 200,
        body: "Successfully wrote to blob storage."
    };
};

function.json

{
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    },
    {
      "type": "blob",
      "path": "output-container/{rand-guid}.txt",
      "connection": "AzureWebJobsStorage",
      "direction": "out",
      "name": "outputBlob"
    }
  ]
}

Binding Expressions

Binding expressions allow you to dynamically reference values from triggers, other bindings, or application settings. These expressions can be used to construct file paths, queue names, or other dynamic targets.

Common examples include:

Conclusion

Azure Functions bindings are a powerful feature that abstracts away the complexities of integrating with various services. By understanding and leveraging bindings, you can build more efficient, maintainable, and scalable serverless applications.

Explore the individual binding documentation for detailed information on configuration, properties, and advanced usage.