Azure Event Grid testing using Azure Functions.

Posted by

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:

  1. First login to the Azure Portal (https://portal.azure.com) then click “Create Resource” then search “Event Grid”.
    Selection_008.png
  2. Click on Event Grid Topic
    Selection_019.pngSelection_007.png
  3. Select “Event Grid Topics”Selection_011.png
  4. Name your topic, pick your subscription, resource group and location.
    Selection_014.png
  5. After creation “Go to Resource Group” or the “Go to Resource” and click “Create One”
    Selection_016.png
  6. 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.
    Selection_020.png
  7. In order to test the web-hook let’s create the Azure Function, by clicking “Create Resource” again (top left of the screen).
    Selection_020.png
  8. Let’s search for functions, select “Functions App” once it shows up and click “Create”.
    Selection_021.pngSelection_022.png
    Selection_023.png
  9. 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”.
    Selection_031.png
  10. Once the Azure Function is created go to the resource.
    Selection_033.png
  11. Once the “+” icon shows click it and then select “Custom Function”
    Selection_035.png
  12. Select “HTTP trigger” and name your function
    Selection_036.png
    Selection_038.png
  13. Once the function is created change the default code in  the scope of Run for the one below and click Save.Selection_052.png
    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.

  14. 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”.Selection_047.png
  15. Now you have an event grid with a web-hook all connected.Selection_057.png
  16. Lets test it.  I will use the Cloud shell in the Azure Portal.eventSelection_058.png
  17. 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>
    Selection_059.png

  18. You can monitor the Azure function execution using “Platform Features” and clicking on “Log Streaming”.Selection_061.pngSelection_062.png
  19. 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.
    Selection_064.pngSelection_063.png

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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.