Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debug an Azure Function that is triggered using an Event Grid

I have an Azure Function (C#) that is triggered by an Event Grid. I am having difficulty debugging my Function locally as it can only be triggered via the Event Grid.

I'm not sure if there is a way to wire the Event Grid up to my local host address for my Function? I have tried using a Web Hook but it only support HTTPS which my localhost uses HTTP.

public static void Run([EventGridTrigger]EventGridEvent eventGridEvent, ILogger log)
{
 ID = eventGridEvent.Subject;
 log.LogInformation($"ID: {ID.ToString()}");
}

Any advice is appreciated.

like image 796
Craig Robinson Avatar asked Nov 21 '19 14:11

Craig Robinson


Video Answer


2 Answers

I found this blog post that helped me resolve this when debugging event grid azure function locally: https://blog.mcilreavy.com/articles/2018-12/debug-eventgrid-triggered-azure-function

One piece that was missing in my case was the aeg-event-type header. aeg-event-type: Notification

After adding it, I was able to debug locally using this url: http://localhost:7071/runtime/webhooks/EventGrid?functionName=nameOfYourFunction

And include this event json in the request body, something like this:

[ { "id": "'1", "eventType": "yourEventType", "eventTime":"10:59:00.000", "subject": "subject1", "data":{"make": "Ducati", "model": "Monster"}, "dataVersion": "1.0" } ]

like image 199
user1106591 Avatar answered Sep 30 '22 03:09

user1106591


Actually, there is a nice and easy way of doing this using a ngrok as a tunnel. So you basically create a EventGrid Subscription that publishes your events to your ngrok tunnel (e. g. https://ec291dd9.ngrok.io/runtime/webhooks/EventGrid?functionName=myfunc) and start debugging your function in Visual Studio. You don't need to change your function at all.

This approach is also well documented from Microsoft: Azure Function Event Grid Trigger Local Debugging

like image 40
Martin Brandl Avatar answered Sep 30 '22 02:09

Martin Brandl