Azure Functions Binding Patterns

Azure Functions bindings provide a declarative way to connect your function to other Azure services and external data sources. They simplify development by abstracting away the complexities of service integrations. This document explores common binding patterns and how to use them effectively.

Input and Output Bindings

Functions can have one or more input bindings and one or more output bindings.

Input Bindings

Input bindings are used to read data from an external source into your function. This data is typically passed as a parameter to your function.

Example of an input binding in function.json:


{
  "bindings": [
    {
      "name": "req",
      "type": "httpTrigger",
      "direction": "in",
      "methods": [ "get", "post" ]
    },
    {
      "name": "myBlob",
      "type": "blob",
      "direction": "in",
      "path": "mycontainer/{name}",
      "connection": "AzureWebJobsStorage"
    }
  ]
}
            

Output Bindings

Output bindings are used to write data from your function to an external destination. This is typically done by returning a value from your function or by using a specific output parameter.

Example of an output binding in function.json:


{
  "bindings": [
    {
      "name": "req",
      "type": "httpTrigger",
      "direction": "in",
      "methods": [ "get", "post" ]
    },
    {
      "name": "$return",
      "type": "http",
      "direction": "out"
    },
    {
      "name": "outputQueue",
      "type": "queue",
      "direction": "out",
      "queueName": "myqueue-items",
      "connection": "AzureWebJobsStorage"
    }
  ]
}
            

Binding Direction

Each binding has a direction property, which can be either in or out.

Binding Types

The type property specifies the Azure service or data source you are integrating with. Common types include:

Parameter Mapping

The name property of a binding in function.json maps to a parameter in your function code. For output bindings, the special name $return is used to bind to the function's return value.

Common Scenarios

Reading Data from Storage

Fetch data from Blob Storage and process it.


// C# example
public static void Run(Stream myBlob, string name, ILogger log)
{
    log.LogInformation($"C# Trigger function processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");
    // Process the blob content from myBlob
}
            

Writing Data to a Queue

Send messages to an Azure Queue.


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

    const name = (req.query.name || req.body.name);
    const responseMessage = name
        ? "Hello, " + name + ". This HTTP triggered function executed successfully. Sending message to queue."
        : "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.";

    context.bindings.outputQueue = {
        message: `Processing request for: ${name}`
    };

    context.res = {
        body: responseMessage
    };
};
            

Understanding and utilizing Azure Functions binding patterns is key to building efficient and scalable serverless applications.