Last week I spent a few hours trying to debug an Event Grid web-hook not sending notifications to a soon-to-be production system. The web-hook URL and settings both looked correct. I was puzzled and thinking “What is making my Event Grid not work”?
In order to tests my web-hook I decided to code a small solution to help me with the problem, something quick like an Azure Function to help me test the web-hook. Why an Azure Function? Azure Event Grid needs an endpoint for the web-hook to be an HTTPS endpoint. If I were to build something local in my laptop or the cloud, I would have to deal with SSL certificates or some other solution that could take me hours and more than a few lines of code.
So how can I manage to test my Event Grid with Azure Functions? If you want to follow below are my steps:
- First login to the Azure Portal (https://portal.azure.com) then click “Create Resource” then search “Event Grid”.
- Click on Event Grid Topic
- Select “Event Grid Topics”
- Name your topic, pick your subscription, resource group and location.
- After creation “Go to Resource Group” or the “Go to Resource” and click “Create One”
- Select your Endpoint Type as “Web-Hook” and click “Select an endpoint”. Here you will realize you have no Web-Hook, so let’s pause this for a second and create an Azure Function to test the Event Grid web-hook.
- In order to test the web-hook let’s create the Azure Function, by clicking “Create Resource” again (top left of the screen).
- Let’s search for functions, select “Functions App” once it shows up and click “Create”.
- Fill the App name, Subscription, Resource Group, Location, Storage Resource Group, OS (Linux), Runtime Stack (.NET), App Service Plan, and Storage Location and click “Create”.
- Once the Azure Function is created go to the resource.
- Once the “+” icon shows click it and then select “Custom Function”
- Select “HTTP trigger” and name your function
- Once the function is created change the default code in the scope of Run for the one below and click Save.
log.LogInformation("C# HTTP trigger function processed a request."); string validationCode = req.Query["validationCode"]; string requestBody = new StreamReader(req.Body).ReadToEnd(); dynamic payload = JsonConvert.DeserializeObject(requestBody); validationCode = validationCode ?? payload[0]?.data?.validationCode; return validationCode != null ? (ActionResult)new OkObjectResult("{\"validationResponse\": \"" + validationCode + "\" }") : new BadRequestObjectResult("Please pass a name on the query string or in the request body");
You can also find the code here: https://github.com/code4clouds/eventgridproxy/blob/master/eventgrid.cs. Once you save the function click “Get Function URL” and copy it.
NOTE: The Azure Function (or web-hook receiver) needs to return the validationResponse sent by the Azure Event Grid in order to successfully work, failing to do so will cause an error.
- Paste the URL you copied (step above) and paste it in the Event Grid (step 7), confirm the seletion and fill the rest of the page then click “Create”.
- Now you have an event grid with a web-hook all connected.
- Lets test it. I will use the Cloud shell in the Azure Portal.event
- Use the following commands to set your testing environment and run the curl command to execute the tests:
az account set -s <subscriptionId> endpoint=$(az eventgrid topic show --name <topic_name> -g myResourceGroup --query "endpoint" --output tsv) key=$(az eventgrid topic key list --name <topic_name> -g myResourceGroup --query "key1" --output tsv) body=$(eval echo "'$(curl https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/event-grid/customevent.json)'") curl -X POST -H "aeg-sas-key: $key" -d "$body" $endpoint
From <https://docs.microsoft.com/en-us/azure/event-grid/custom-event-quickstart-portal>
- You can monitor the Azure function execution using “Platform Features” and clicking on “Log Streaming”.
- Also the Event Grid has monitoring right on the page and by clicking on the Topic’s metrics you should see metrics specific to it.
Once I made sure the web-hook indeed works, I pointed my event grid to the soon-to-be production web-hook endpoint and nothing worked, no errors, no message. Now I can rule my Event Grid. It was definitively not the web-hook, so the problem had to be on the network and indeed it was. The soon-to-production system had a self-sign SSL which of course makes it a non-valid endpoint. Once we changed the SSL certificate to a valid one, everything worked like a charm. I hope sharing this experience here, will help others understand Azure Functions and how easy they are to setup for any project with or without Azure Event Grid web-hooks.