Ask Learn
Preview
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign inThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
When working with Azure Event Grid, say for something like Azure Blob Storage Events, it's useful to be able to debug your handling code locally before going through the process of pushing up to its final resting place (in Azure, of course 😉).
However, when routing Storage Events to a custom web endpoint you're met with a bit of a conundrum - how can I get the event to hit my local machine so I can debug my code locally?
This can be accomplished quite easily using a free, publicly-available tool called ngrok. Ngrok effectively creates a tunnel to your local machine on HTTP ports (80, 443) that's accessible via the public internet. Upon running it, you're automatically assigned a subdomain of ngrok.io, and any traffic to that endpoint is routed to your local machine.
How can we use this in conjunction with Event Grid? With our friend Azure Functions! By creating an HTTP-triggered Azure Function, running it locally, and setting breakpoints in our code we can inspect the payload coming from Event Grid and also ensure we're reacting properly to it. Doing so requires a couple of important steps, though.
host-header
parameter. All-in-all, your ngrok launch command should look something like this (for Azure Functions .Net development):ngrok http -host-header=localhost 7071
Upon running this, you'll get a console window:
Take note of the https endpoint given to you by ngrok; Event Grid allows only https endpoints as targets for subscriptions to topics.
SubscriptionValidationEvent
sent out by Event Grid when a subscription endpoint is created. You can read more about this here, but suffice to say here's the code you need to do this: var payloadFromEventGrid = JToken.ReadFrom(new JsonTextReader(new StreamReader(await req.Content.ReadAsStreamAsync())));
dynamic eventGridSoleItem = (payloadFromEventGrid as JArray)?.SingleOrDefault();
if (eventGridSoleItem == null)
{
return req.CreateErrorResponse(HttpStatusCode.BadRequest, $@"Expecting only one item in the Event Grid message");
}
if (eventGridSoleItem.eventType == @"Microsoft.EventGrid.SubscriptionValidationEvent")
{
log.Verbose(@"Event Grid Validation event received.");
return new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent($"{{ \"validationResponse\" : \"{((dynamic)payloadFromEventGrid)[0].data.validationCode}\" }}")
};
}
That's all there is to setup! The final step is, of course, to put the correct URL in to your Event Grid subscription. For the example above, and an Azure HTTP-triggered function called "TestEventGrid" that URL would look like https://5abd9dff.ngrok.io/api/TestEventGrid
Put this URL in to the Event Grid subscription for an Azure Storage Event account as shown:
As soon as you hit 'Create', you'll see your Function get hit with the SubscriptionValidationEvent
, then the subscription should be created successfully. You can then drop a blob in to any container in that storage account and you'll see the events fire & hit your locally-running Azure Function!
A couple of caveats:
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign in