Event Grid Bindings for Azure Functions

Azure Functions provides robust integration with Azure Event Grid, allowing you to trigger your functions in response to events from various Azure services and custom sources. This document details how to use Event Grid triggers and output bindings within your Azure Functions projects.

Event Grid Triggers

An Event Grid trigger allows your function to be invoked when an event is published to an Event Grid topic that you are subscribed to. This is a powerful way to build event-driven architectures.

Configuration

To configure an Event Grid trigger, you typically define it in your function's function.json file (for JavaScript, Python, PowerShell, etc.) or using attributes (for C#).

Example function.json


{
  "scriptFile": "index.js",
  "bindings": [
    {
      "name": "event",
      "type": "eventGridTrigger",
      "direction": "in",
      "topicEndpoint": "%EVENTGRID_TOPIC_ENDPOINT%",
      "topicKey": "%EVENTGRID_TOPIC_KEY%"
    }
  ]
}
            

Example C# Attribute


using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.EventGrid;
using Microsoft.Extensions.Logging;

public static class EventGridTriggerFunction
{
    [FunctionName("EventGridTriggerFunction")]
    public static void Run(
        [EventGridTrigger] EventGridEvent eventGridEvent,
        ILogger log)
    {
        log.LogInformation($"Event Type: {eventGridEvent.EventType}");
        log.LogInformation($"Subject: {eventGridEvent.Subject}");
        log.LogInformation($"Data: {eventGridEvent.Data}");
    }
}
            

Event Grid Event Schema

The trigger payload is an EventGridEvent object, which typically contains the following properties:

Property Description
id Unique identifier for the event.
topic The full resource path of the topic the event is from.
subject The subject of the event, used to scope the event to a specific subject.
data Event-specific data. The schema of this data varies based on the event source.
eventType One of the registered event types for this event source.
dataVersion The version of the event schema.
eventTime The time (in UTC) the event was generated.
When using Event Grid triggers, ensure your function app is configured with a managed identity or that you provide appropriate credentials for accessing Event Grid.

Event Grid Output Bindings

An Event Grid output binding allows your function to publish events to an Azure Event Grid topic.

Configuration

Similar to triggers, output bindings are configured in function.json or via attributes.

Example function.json


{
  "scriptFile": "index.js",
  "bindings": [
    {
      "name": "event",
      "type": "eventGridTrigger",
      "direction": "in",
      "topicEndpoint": "%EVENTGRID_TOPIC_ENDPOINT%",
      "topicKey": "%EVENTGRID_TOPIC_KEY%"
    },
    {
      "name": "outputEvent",
      "type": "eventGrid",
      "direction": "out",
      "topicEndpoint": "%OUTPUT_TOPIC_ENDPOINT%",
      "topicKey": "%OUTPUT_TOPIC_KEY%"
    }
  ]
}
            

Example C# Attribute


using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.EventGrid;
using Microsoft.Azure.EventGrid.Models;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;

public static class EventGridOutputFunction
{
    [FunctionName("EventGridOutputFunction")]
    public static void Run(
        [EventGridTrigger] EventGridEvent eventGridEvent,
        [EventGrid(TopicEndpoint = "%OUTPUT_TOPIC_ENDPOINT%", TopicKey = "%OUTPUT_TOPIC_KEY%")] out EventGridEvent[] outputEvent,
        ILogger log)
    {
        log.LogInformation($"Received event: {eventGridEvent.EventType}");

        var customEvent = new EventGridEvent
        {
            Id = System.Guid.NewGuid().ToString(),
            EventType = "MyCustomEventType",
            Subject = "ProcessedFile",
            EventTime = System.DateTime.UtcNow,
            Data = new Dictionary { { "processedFileName", "report.pdf" } },
            DataVersion = "1.0",
            Topic = "/subscriptions/YOUR_SUB_ID/resourceGroups/YOUR_RG/providers/Microsoft.EventGrid/topics/YOUR_TOPIC_NAME"
        };

        outputEvent = new EventGridEvent[] { customEvent };
    }
}
            

Publishing Events

When using an output binding, you assign an array of EventGridEvent objects to the output binding. The runtime will then publish these events to the configured Event Grid topic.

For robust event publishing, consider using the Event Grid SDK directly for more advanced scenarios like batching and retries.

Common Scenarios

By leveraging Event Grid bindings, you can create highly scalable and responsive event-driven applications with Azure Functions.