Azure Functions Event Hubs Bindings

This document explains how to use Azure Functions to connect to Azure Event Hubs. Event Hubs is a highly scalable data streaming platform and event ingestion service that can ingest millions of events per second.

Azure Functions provides built-in bindings for Event Hubs, simplifying the process of sending and receiving events from Event Hubs within your serverless applications.

Introduction to Event Hubs Bindings

Event Hubs bindings allow you to declare Event Hubs connections directly in your function's configuration, abstracting away much of the low-level connection and messaging logic.

Input Bindings

An Event Hubs input binding allows your function to read events from an Event Hub. When your function is triggered, the input binding populates context variables with the event data.

When configured as an input binding, your function can receive a single event or a batch of events. The type of the parameter in your function signature determines whether you receive a single event or a batch.

Single Event Input


using Microsoft.Azure.WebJobs;
using Microsoft.Azure.EventHubs;
using System.Text;

public static class EventHubInput
{
    [FunctionName("EventHubInputTrigger")]
    public static void Run(
        [EventHubTrigger("my-event-hub", Connection = "EventHubConnectionAppSetting")] EventData myEvent,
        ILogger log)
    {
        log.LogInformation($"C# Event Hub trigger function processed a message: {Encoding.UTF8.GetString(myEvent.Body.Array)}");
    }
}
            

Batch Event Input


using Microsoft.Azure.WebJobs;
using Microsoft.Azure.EventHubs;
using System.Collections.Generic;
using System.Text;
using Microsoft.Extensions.Logging;

public static class EventHubBatchInput
{
    [FunctionName("EventHubBatchInputTrigger")]
    public static void Run(
        [EventHubTrigger("my-event-hub", Connection = "EventHubConnectionAppSetting")] EventData[] events,
        ILogger log)
    {
        foreach (EventData eventData in events)
        {
            log.LogInformation($"C# Event Hub trigger function processed a message: {Encoding.UTF8.GetString(eventData.Body.Array)}");
        }
    }
}
            

Output Bindings

An Event Hubs output binding allows your function to send events to an Event Hub. You can use this to send processed data or new events to an Event Hub.


using Microsoft.Azure.WebJobs;
using Microsoft.Azure.EventHubs;
using System.Text;
using Microsoft.Extensions.Logging;

public static class EventHubOutput
{
    [FunctionName("EventHubOutputTrigger")]
    [return: EventHub("output-event-hub", Connection = "EventHubConnectionAppSetting")]
    public static EventData Run(
        [TimerTrigger("0 */1 * * * *")] TimerInfo myTimer,
        ILogger log)
    {
        log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
        string message = $"New event generated at {DateTime.Now}";
        log.LogInformation($"Sending message to output Event Hub: {message}");
        return new EventData(Encoding.UTF8.GetBytes(message));
    }
}
            

Configuration

To configure Event Hubs bindings, you typically define them in your function.json file (for JavaScript, Python, PowerShell) or via attributes in your code (for C#, Java, etc.).

function.json Example (JavaScript)


{
  "scriptFile": "index.js",
  "bindings": [
    {
      "name": "myEvent",
      "type": "eventHubTrigger",
      "direction": "in",
      "eventHubName": "my-event-hub",
      "connection": "EventHubConnectionAppSetting",
      "consumerGroup": "$Default"
    },
    {
      "name": "outputEventHub",
      "type": "eventHub",
      "direction": "out",
      "eventHubName": "output-event-hub",
      "connection": "EventHubConnectionAppSetting"
    }
  ]
}
            

Connection String

The Connection property in the binding configuration should refer to an application setting that contains the Event Hubs connection string. This setting is typically stored in your local.settings.json file during local development and in your Azure Function App's configuration settings in the Azure portal.

local.settings.json Example

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "EventHubConnectionAppSetting": "Endpoint=sb://your-event-hubs-namespace.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=YOUR_KEY"
  }
}
            

Important Binding Properties

Advanced Topics

Batch Processing

For efficiency, especially with high throughput, it's recommended to process events in batches. The bindings support this by allowing you to define your trigger parameter as an array (e.g., EventData[] in C#).

Error Handling

Implement robust error handling within your function to deal with potential issues during event processing or sending. For input bindings, unhandled exceptions can lead to message retries or dead-lettering, depending on the configuration and the runtime.

Examples

Refer to the Event Hubs Bindings Examples page for more detailed code samples across different languages.

Note: Ensure your Azure Function App has the necessary network access and permissions to connect to your Azure Event Hubs namespace.
Tip: For real-time monitoring of your Event Hubs, consider using Azure Monitor and Application Insights.