Azure Functions Bindings

Bindings provide a declarative way to connect to data services and other resources. They simplify your code by removing the need for developers to write boilerplate code for connecting to and interacting with these services. Bindings are implemented as input and output bindings.

Input bindings make data available to your function as parameters. Output bindings allow your function to write data to a service. Azure Functions has built-in support for a wide variety of triggers and bindings.

Understanding Bindings

Bindings abstract away the complexities of connecting to external services. You define bindings in your function's configuration file (e.g., function.json or via attributes in code), and the Functions runtime handles the rest.

Key Benefits of Bindings:

  • Simplified Code: No need for explicit SDK calls to connect to services like Blob Storage, Cosmos DB, or queues.
  • Declarative Configuration: Define connections and data sources in a clear, configuration-driven way.
  • Type Safety: Bindings often map directly to strongly-typed parameters in your function code.
  • Extensibility: You can create custom bindings if your specific needs aren't met by the built-in options.

Types of Bindings

Bindings are categorized into two main types:

1. Triggers

A trigger defines how a function is invoked. It's the event that starts your function's execution. For example, an HTTP trigger starts a function when an HTTP request is received, and a Timer trigger starts a function on a schedule.

Triggers are essentially a type of input binding that initiates execution.

Common Triggers:

  • HTTP Trigger: Invoked by an HTTP request.
  • Timer Trigger: Invoked on a schedule defined by a CRON expression.
  • Blob Trigger: Invoked when a new or updated blob is detected in Azure Blob Storage.
  • Queue Trigger: Invoked when a message is added to an Azure Storage queue.
  • Cosmos DB Trigger: Invoked when a new or updated document is detected in a Cosmos DB collection.

2. Input and Output Bindings

These bindings allow your function to read data from or write data to other Azure services or external data sources.

Input Bindings:

Input bindings provide data to your function. The data is typically passed as a parameter to your function. For example, an input binding for Azure Blob Storage can make the content of a blob available as a string or stream.

Output Bindings:

Output bindings enable your function to send data to an external service. This is usually achieved by returning a value from your function or by using an output parameter.

Binding Type Description Direction
HTTP Trigger Initiates execution via HTTP requests. Trigger (Input)
Azure Blob Storage Read/Write blobs from/to Azure Blob Storage. Input/Output
Azure Queue Storage Read messages from/write messages to Azure Queue Storage. Input/Output
Azure Cosmos DB Read documents from/write documents to Azure Cosmos DB. Input/Output
Service Bus Send/receive messages from Azure Service Bus. Input/Output
Twilio Send SMS messages via Twilio. Output

Configuring Bindings

Bindings are configured in one of two primary ways:

  • function.json (for JavaScript, Python, PowerShell, etc.): A JSON file that defines the bindings for a specific function.
  • Attributes (for .NET, Java, TypeScript): Decorators or annotations applied directly to function parameters or return values in your code.

Here's a simplified example of a function.json snippet for an HTTP-triggered function that reads a blob:


{
  "scriptFile": "index.js",
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    },
    {
      "type": "blob",
      "direction": "in",
      "name": "myBlob",
      "path": "samples-data/{name}.txt",
      "connection": "AzureWebJobsStorage"
    }
  ]
}
                

And here's a conceptual example using .NET attributes:


using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using System.IO;

public static class BlobReaderFunction
{
    [FunctionName("ReadBlob")]
    public static void Run(
        [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
        [Blob("samples-data/{name}.txt", Connection = "AzureWebJobsStorage")] Stream myBlob,
        string name)
    {
        // 'myBlob' now contains the content of the blob.
        // 'name' is extracted from the route.
        // Process the blob content...
    }
}
                

Common Bindings in Detail

Azure Functions offers a rich set of bindings. For detailed documentation on specific bindings, please refer to the Azure Functions bindings reference.

Custom Bindings

If the built-in bindings don't meet your needs, you can create custom bindings to integrate with virtually any service or API.