Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / DevOps

Connecting a Serverless Function to an Event Grid Custom Topic

5.00/5 (2 votes)
31 Mar 2018CPOL2 min read 11.4K  
How to trigger an Azure serverless function from an event grid custom topic

Introduction

A custom topic, in Azure Event Grid is a user defined type of event to which events can be routed to one or more subscribers.  

Creating a Custom Topic

You can add an event grid custom topic through the Azure Portal by searching for "Event Grid Topic":

Image 1

You would need to give a unique topic name - and it is a good idea to use a long descriptive name as you will use that when linking subscribers.

Image 2

It is a good idea to group custom topics in a resource group.

Coding the Function

The first thing you need is to install the packages that will be used to interact with Azure event grid:

Image 3

These then need to be imported into your classes that will provide the serverless functions.

C#
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.EventGrid;
using Microsoft.Azure.WebJobs.Host;

To write a function that can react to an event grid message passed by a topic, we use the FunctionName attribute to make it known to the Azure function app, and use an EventGridTrigger attribute to tag the first parameter as coming from an event grid message.

This example simply echos the content of the message, which is useful for testing the custom topic is correctly firing.

C#
[FunctionName("EchoContent")]
public static void EchoContent([EventGridTrigger] EventGridEvent eventGridEvent,
       TraceWriter log)
{
    log.Info($"Echo function executed at: {DateTime.Now}");

    log.Info($"Topic : {eventGridEvent.Topic} , Subject : {eventGridEvent.Subject } ,
                        Event Type: {eventGridEvent.EventType }  ");
    log.Info($" Payload : { eventGridEvent.Data.ToString()} ");

}

Linking the Function to the Topic

In the Azure portal, navigate to the function app and find the function you wish to attach to the custom topic.  To the right hand side of the pane containing the function.json, you will see a link captioned "Add Event Grid subscription".  

Image 4

This will bring up a task pane that you fill out with the custom topic properties:

Image 5

You can repeat this process for any more serverless functions which you wish to be triggered by the same event hub custom event notification.

Sending a Message to the Event Grid Topic

You can use curl or any similar tool to POST an event to the custom topic (in fact, I use and recommend PostMan).

The url you need is https://{topic name}.{region}.eventgrid.azure.net/api/events 

You need to add a token to the headers named aeg-sas-key which has your authentication key and set the Content-Type to application/json.

The message content is then passed in the message body:-

JavaScript
[{
    "topic": "/subscriptions/9f18ed62-6f67-4bb8-e272-407f08a7b20d/resourceGroups/
               thelongrun/providers/microsoft.eventgrid/topics/create-new-league-command",
    "subject": "TheLongRun/Leagues/blackrock-meet-and-train",
    "eventType": "Create-New-League-Command",
    "eventTime": "2018-03-30T21:42:00.9584103Z",
    "id": "831f1650-001e-001b-62ab-ecb72e000822",
    "data": {
      "LeagueName": "Blackrock Meet And Train",
      "Date_Incorporated" : "2018-01-01T16:41:00.9584103Z",
      "Location" : "Blackrock, County Dublin, Ireland",
      "Twitter_Handle" : "@Blackrock_AC",
      "Email_Address" : "information@blackrockathleticclub.ie"
    }
  }]

The custom content of your message being contained in the data object.

History

  • 2018-03-31- First cut

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)