Test your Event Grid handler locally

Testing Event Grid triggered Azure Functions locally can be complicated. You don't want to have to trigger events over and over to test your flow. It can also get expensive as triggering those events might require you perform an event that costs money like sending an SMS or placing a phone call. To help with testing, we show you how to use Postman to trigger your Azure Function with a payload that mimics the Event Grid event.

Pre-requisites

  • Install Postman.
  • Have a running Azure Function that can be triggered by Event Grid. If you don't have one, you can follow the quickstart to create one.

The Azure Function can be running either in Azure if you want to test it with some test events or if you want to test the entire flow locally (press F5 in Visual Studio Code to run it locally). If you want to test the entire flow with an externally triggered webhook, you need to use ngrok to expose your locally running Azure Function to the public, allowing it to be triggered by internet sources (as an example from Azure Event WebHooks). Configure ngrok by running the command:


ngrok http 7071

It is worth remembering that exposing development resources publicly might not be considered as secure. That is why you can also run the entire workflow locally without ngrok by invoking requests to:

http://localhost:7071/runtime/webhooks/EventGrid?functionName={functionname}

Configure Postman

  1. Open Postman and create a new request.

    Screenshot of Postman body configuration.

  2. Select the POST method.

  3. Enter the URL of your Azure Function. Can either be the URL of the Azure Function running in Azure or the ngrok URL if you're running it locally. Ensure that you add the function name at the end of the URL: /runtime/webhooks/EventGrid?functionName=<<FUNCTION_NAME>>.

  4. Select the Body tab and select raw and JSON from the dropdown. In the body, you add a test schema for the event you want to trigger. For example, if you're testing an Azure Function that is triggered by receiving SMS events, you add the following:

    
    {
      "id": "Incoming_20200918002745d29ebbea-3341-4466-9690-0a03af35228e",
      "topic": "/subscriptions/50ad1522-5c2c-4d9a-a6c8-67c11ecb75b8/resourcegroups/acse2e/providers/microsoft.communication/communicationservices/{communication-services-resource-name}",
      "subject": "/phonenumber/15555555555",
      "data": {
        "MessageId": "Incoming_20200918002745d29ebbea-3341-4466-9690-0a03af35228e",
        "From": "15555555555",
        "To": "15555555555",
        "Message": "Great to connect with Azure Communication Services events",
        "ReceivedTimestamp": "2020-09-18T00:27:45.32Z"
      },
      "eventType": "Microsoft.Communication.SMSReceived",
      "dataVersion": "1.0",
      "metadataVersion": "1",
      "eventTime": "2020-09-18T00:27:47Z"
    }
    
    

    You can find more information for the different event types used for Azure Communication Services in the documentation.

  5. Select the Headers tab and add the following headers:

    • Content-Type: application/json
    • aeg-event-type: Notification

    Screenshot of Postman headers configuration.

  6. Select the Send button to trigger the event.

    Screenshot of Postman send button.

    At this point, an event should trigger in your Azure Function. You can verify the event by looking at the execution of your Azure Function. You can then validate that the function is doing its job correctly.