Debugging Azure Function Event Grid Triggers Locally
By Harry Bellamy
- 3 minutes read - 544 wordsAzure Event Grid is a asynchronous communication technology that uses a ‘push-push’ form of communication. This means that it lends itself to communicating with stateless services, as no listener needs to be setup.
Azure Functions is the obvious candidate here, especially as there is an out-of-the-box integration with Azure Event Grid.
However, when running an Azure Function locally with an Event Grid trigger, there are a few things to be aware of before you can get it working.
Ensure the Trigger is of the Correct Type
If the Function has been created using Visual Studio’s in-built wizard, this should already be correctly setup and look like this:
using Microsoft.Azure.WebJobs.Extensions.EventGrid;
using Microsoft.Azure.EventGrid.Models;
using Microsoft.Azure.WebJobs;
[FunctionName("Function1")]
public void Run([EventGridTrigger]EventGridEvent eventGridEvent)
{
// function body
...
}
The attribute [EventGridTrigger]
determines that this function is triggered by Azure Event Grid events.
Send to the Right URL
Assuming you created your Function through Visual Studio’s in-built wizard, at the top of you function class (named Function1.cs
), there should be a comment containing a local URL that you can send a request to to trigger the function.
It should look something like this:
http://localhost:7071/runtime/webhooks/EventGrid?functionName={functionname}
So if the name of your function is Function1
, the URL would be:
http://localhost:7071/runtime/webhooks/EventGrid?functionName=Function1
Configure the Correct Payload
Now you have the correct URL, you can create a request in an HTTP request generator such as Postman.
For the request body, event grid expects the request to have a certain set of fields to conform to its schema.
Create a POST request in your request generator and add the following JSON to the request body:
[
{
"id": "'1",
"eventType": "yourEventType",
"eventTime":"10:59:00.000",
"subject": "yoursubject",
"data": "<your custom event json>",
"dataVersion": "1.0"
}
]
The data
field defines is where you would add your custom event data.
If you are sending a POCO across event grid such as this:
public class Customer
{
public string FirstName { get; set; }
public string Surname { get; set; }
}
This full event JSON should look something like this:
[
{
"id": "'1",
"eventType": "yourEventType",
"eventTime":"10:59:00.000",
"subject": "yoursubject",
"data": { "FirstName": "John", "Surname": "Smith" },
"dataVersion": "1.0"
}
]
Add the Missing HTTP Header
If you try to send the request to the URL now, you will find that it doesn’t work. This is because Event Grid requests require a ‘magic’ header for them to work.
Add the following header to your request in your request generator:
aeg-event-type: Notification
If you send the request now you should be able to debug through your function.
Deserialise the Event Data
The data property contained within the event will need to be deserialised in into the target object. The following helper method will do this:
using Microsoft.Azure.EventGrid.Models;
using Newtonsoft.Json.Linq;
private static T GetEventData<T>(EventGridEvent eventGridEvent)
{
var jObject = eventGridEvent.Data as JObject;
return jObject.ToObject<T>();
}
This can be used like so using the Customer
class defined earlier:
using Microsoft.Azure.WebJobs.Extensions.EventGrid;
using Microsoft.Azure.EventGrid.Models;
using Microsoft.Azure.WebJobs;
[FunctionName("Function1")]
public void Run([EventGridTrigger]EventGridEvent eventGridEvent)
{
var eventData = GetEventData<Customer>();
// rest of the function body
...
}
Summary
This should give you everything you need to get started debugging your Azure Function Event Grid triggers locally.
Once you’re satisfied it works locally, you can publish the app to Azure and integrate with an actual Azure Event Grid topic.