Azure Functions Output Bindings

Output bindings allow your Azure Function to send data to other Azure services or external systems without writing explicit SDK code. This simplifies your function code and decouples it from specific service integrations.

How Output Bindings Work

When you define an output binding in your function's configuration (e.g., function.json for JavaScript/TypeScript or attributes in C#), you specify the type of binding, a name, and the target service. Your function code then returns a value or uses a provided output object to send data to the configured destination.

Common Output Binding Scenarios

1. Sending Messages to a Queue

Example: Azure Storage Queue Output Binding

This example shows how to send a message to an Azure Storage Queue.

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

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

    // The 'outputQueueItem' binding name must match the binding name in function.json
    context.bindings.outputQueueItem = queueMessage;

    context.res = {
        status: 200,
        body: responseMessage
    };
};

In your function.json:

{
  "scriptFile": "index.js",
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    },
    {
      "type": "queue",
      "direction": "out",
      "name": "outputQueueItem",
      "queueName": "myoutputqueue",
      "connection": "AzureWebJobsStorage"
    }
  ]
}

2. Writing to a Document Database

Example: Azure Cosmos DB Output Binding

This demonstrates writing a document to Azure Cosmos DB.

In your C# function:

using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Azure.WebJobs.Extensions.CosmosDB;
using System.Threading.Tasks;

public static class CosmosDbOutput
{
    [FunctionName("CosmosDbOutputFunction")]
    public static async Task Run(
        [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
        [CosmosDB(
            databaseName: "MyDatabase",
            collectionName: "MyCollection",
            ConnectionStringSetting = "CosmosDbConnectionString")] out object document)
    {
        string newItem = await req.ReadAsStringAsync();
        document = Newtonsoft.Json.JsonConvert.DeserializeObject(newItem);
    }
}

3. Sending Notifications

Example: Azure Service Bus Output Binding

Sending a message to a Service Bus topic.

In your JavaScript function:

module.exports = async function (context, myQueueItem) {
    // myQueueItem is the input from a queue trigger
    context.log('JavaScript queue trigger function processed work item:', myQueueItem);

    const serviceBusMessage = {
        body: `Processed item: ${myQueueItem}`
    };

    // The 'outputSbMsg' binding name must match function.json
    context.bindings.outputSbMsg = serviceBusMessage;
};

In your function.json:

{
  "scriptFile": "index.js",
  "bindings": [
    {
      "name": "myQueueItem",
      "type": "queueTrigger",
      "direction": "in",
      "queueName": "myqueue",
      "connection": "AzureWebJobsStorage"
    },
    {
      "type": "serviceBus",
      "name": "outputSbMsg",
      "direction": "out",
      "topicName": "mytopic",
      "connection": "ServiceBusConnection"
    }
  ]
}

Binding Types and Configurations

Azure Functions supports a wide range of output bindings for various services, including:

Service Binding Type Common Scenarios
Azure Storage Queue queue Sending messages for background processing.
Azure Service Bus Queue/Topic serviceBus Reliable message queuing and pub/sub patterns.
Azure Cosmos DB cosmosDB Storing documents or records.
Azure Table Storage table Storing key-value pairs.
Azure Blob Storage blob Writing files or data blobs.
SendGrid sendGrid Sending emails.
Twilio twilioSms Sending SMS messages.
Event Hubs eventHub Sending events to a high-throughput data stream.

Key Considerations

Best Practice: Always store connection strings and secrets in application settings (e.g., via Azure Key Vault or Function App settings) rather than hardcoding them. The connection property in bindings refers to these settings.

By leveraging output bindings, you can significantly reduce the boilerplate code required to integrate your Azure Functions with other services, leading to cleaner, more maintainable, and efficient serverless applications.