Azure Functions Documentation

Event Hub Output Bindings

This document describes how to configure and use output bindings for Azure Event Hubs in Azure Functions.

Overview

Event Hub output bindings allow your Azure Function to send messages to an Azure Event Hub. This is useful for scenarios where your function processes data and needs to emit it to a stream for further processing, analytics, or archival.

Configuration

Output bindings are configured in your function's function.json file (for JavaScript, C#, and other non-Python languages) or using decorators in Python.

function.json Example

{
  "bindings": [
    {
      "name": "msg",
      "type": "eventHubOutput",
      "direction": "out",
      "eventHubName": "my-event-hub",
      "connection": "EventHubConnectionString"
    }
  ]
}

Python Decorator Example

import azure.functions as func

def main(myInput: func.EventGridEvent, msg: func.Out[str]):
    msg.set("Hello from Event Hub output binding!")

Note: The eventHubName property specifies the Event Hub to send messages to. The connection property refers to an application setting that contains the Event Hub connection string.

Using the Output Binding

The output binding is represented by a variable in your function code. You can then set a value on this variable, which will be sent as a message to the configured Event Hub.

JavaScript Example

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

    const message = req.body && req.body.message ? req.body.message : "Default message";

    context.bindings.msg = message; // Set the message to the output binding

    context.res = {
        status: 200,
        body: "Message sent to Event Hub"
    };
};

C# Example

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

public static class EventHubOutputFunction
{
    [FunctionName("SendToEventHub")]
    public static void Run(
        [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
        [EventHub("my-event-hub", Connection = "EventHubConnectionString")] out string msg,
        ILogger log)
    {
        log.LogInformation("C# HTTP trigger function processed a request.");

        string messageToSend = "Hello from C# Azure Function!";
        msg = messageToSend; // Assign the message to the output binding

        log.LogInformation($"Message sent to Event Hub: {messageToSend}");
    }
}

Message Formatting

The output binding typically accepts a string. For complex data structures, you'll want to serialize them into JSON strings before sending.

Important: By default, each invocation of your function sends a single message. If you need to send multiple messages in a single invocation, you can bind to an out array or IAsyncCollector.

Common Scenarios

Troubleshooting

If you encounter issues:

For more advanced scenarios, such as sending batched messages or handling errors, refer to the official Azure Functions documentation.