Tutorial: Forward events to Event Grid cloud

This article walks through all the steps needed to forward edge events to Event Grid in the Azure cloud. You might want to do it for the following reasons:

  • React to edge events in the cloud.
  • Forward events to Event Grid in the cloud and use Azure Event Hubs or Azure Storage queues to buffer events before processing them in the cloud.

To complete this tutorial, you need have an understanding of Event Grid concepts on edge and Azure.

Prerequisites

In order to complete this tutorial, you will need:

  • Azure subscription - Create a free account if you don't already have one.
  • Azure IoT Hub and IoT Edge device - Follow the steps in the quick start for Linux or Windows devices if you don't already have one.

Deploy Event Grid IoT Edge module

There are several ways to deploy modules to an IoT Edge device and all of them work for Azure Event Grid on IoT Edge. This article describes the steps to deploy Event Grid on IoT Edge from the Azure portal.

Note

In this tutorial, you will deploy the Event Grid module without persistence. It means that any topics and subscriptions you create in this tutorial will be deleted when you redeploy the module. For more information on how to setup persistence, see the following articles: Persist state in Linux or Persist state in Windows. For production workloads, we recommend that you install the Event Grid module with persistence.

Important

In this tutorial, Event Grid module will be deployed with client authentication turned-off, and allow HTTP subscribers. For production workloads, we recommend that you enable only HTTPS requests and subscribers with client authentication enabled. For more information on how to configure Event Grid module securely, see Security and authentication.

Select your IoT Edge device

  1. Sign in to the Azure portal
  2. Navigate to your IoT Hub.
  3. Select IoT Edge from the menu in the Automatic Device Management section.
  4. Click on the ID of the target device from the list of devices
  5. Select Set Modules. Keep the page open. You will continue with the steps in the next section.

Configure a deployment manifest

A deployment manifest is a JSON document that describes which modules to deploy, how data flows between the modules, and desired properties of the module twins. The Azure portal has a wizard that walks you through creating a deployment manifest, instead of building the JSON document manually. It has three steps: Add modules, Specify routes, and Review deployment.

Add modules

  1. In the Deployment Modules section, select Add

  2. From the types of modules in the drop-down list, select IoT Edge Module

  3. Provide the name, image, container create options of the container:

    • Name: eventgridmodule
    • Image URI: mcr.microsoft.com/azure-event-grid/iotedge:latest
    • Container Create Options:
        {
          "Env": [
            "inbound:clientAuth:clientCert:enabled=false",
            "outbound:webhook:httpsOnly=false"
          ],
          "HostConfig": {
            "PortBindings": {
              "4438/tcp": [
                {
                  "HostPort": "4438"
                }
              ]
            }
          }
        }
    
  4. Click Save

  5. Click Next to continue to the routes section

    Note

    If you are using an Azure VM as an edge device, add an inbound port rule to allow inbound traffic on the port 4438. For instructions on adding the rule, see How to open ports to a VM.

Setup routes

Keep the default routes, and select Next to continue to the review section

Review deployment

  1. The review section shows you the JSON deployment manifest that was created based on your selections in the previous two sections. Confirm that you see the two modules in the list: $edgeAgent and $edgeHub. These two modules make up the IoT Edge runtime and are required defaults in every deployment.
  2. Review your deployment information, then select Submit.

Verify your deployment

  1. After you submit the deployment, you return to the IoT Edge page of your IoT hub.
  2. Select the IoT Edge device that you targeted with the deployment to open its details.
  3. In the device details, verify that the Event Grid module is listed as both Specified in deployment and Reported by device.

It may take a few moments for the module to be started on the device and then reported back to IoT Hub. Refresh the page to see an updated status.

Create event grid topic and subscription in cloud

Create an event grid topic and subscription in the cloud by following this tutorial. Note down topicURL, sasKey, and topicName of the newly created topic that you'll use later in the tutorial.

For example, if you created a topic named testegcloudtopic in West US, the values would look something like:

  • TopicUrl: https://testegcloudtopic.westus2-1.eventgrid.azure.net/api/events
  • TopicName: testegcloudtopic
  • SasKey: Available under AccessKey of your topic. Use key1.

Create event grid topic at the edge

  1. Create topic3.json with the following content. See our API documentation for details about the payload.

        {
          "name": "sampleTopic3",
          "properties": {
            "inputschema": "eventGridSchema"
          }
        }
    
  2. Run the following command to create the topic. HTTP Status Code of 200 OK should be returned.

    curl -k -H "Content-Type: application/json" -X PUT -g -d @topic3.json https://<your-edge-device-public-ip-here>:4438/topics/sampleTopic3?api-version=2019-01-01-preview
    
  3. Run the following command to verify topic was created successfully. HTTP Status Code of 200 OK should be returned.

    curl -k -H "Content-Type: application/json" -X GET -g https://<your-edge-device-public-ip-here>:4438/topics/sampleTopic3?api-version=2019-01-01-preview
    

    Sample output:

         [
           {
             "id": "/iotHubs/eg-iot-edge-hub/devices/eg-edge-device/modules/eventgridmodule/topics/sampleTopic3",
             "name": "sampleTopic3",
             "type": "Microsoft.EventGrid/topics",
             "properties": {
               "endpoint": "https://<edge-vm-ip>:4438/topics/sampleTopic3/events?api-version=2019-01-01-preview",
               "inputSchema": "EventGridSchema"
             }
           }
         ]
    

Create Event Grid subscription at the edge

  1. Create subscription3.json with the following content. See our API documentation for details about the payload.

         {
           "properties": {
             "destination": {
               "endpointType": "eventGrid",
               "properties": {
                         "endpointUrl": "<your-event-grid-cloud-topic-endpoint-url>?api-version=2018-01-01",
                         "sasKey": "<your-event-grid-topic-saskey>",
                         "topicName": null
               }
             }
           }
     }
    

    Note

    The endpointUrl specifies that the Event Grid topic URL in the cloud. The sasKey refers to Event Grid cloud topic's key. The value in topicName will be used to stamp all outgoing events to Event Grid. This can be useful when posting to an Event Grid domain topic. For more information about Event Grid domain topic, see Event domains

    For example,

        {
          "properties": {
            "destination": {
              "endpointType": "eventGrid",
              "properties": {
                "endpointUrl": "https://testegcloudtopic.westus2-1.eventgrid.azure.net/api/events?api-version=2018-01-01",
                 "sasKey": "<your-event-grid-topic-saskey>",
                 "topicName": null
              }
            }
          }
        }
    
  2. Run the following command to create the subscription. HTTP Status Code of 200 OK should be returned.

    curl -k -H "Content-Type: application/json" -X PUT -g -d @subscription3.json https://<your-edge-device-public-ip-here>:4438/topics/sampleTopic3/eventSubscriptions/sampleSubscription3?api-version=2019-01-01-preview
    
  3. Run the following command to verify subscription was created successfully. HTTP Status Code of 200 OK should be returned.

    curl -k -H "Content-Type: application/json" -X GET -g https://<your-edge-device-public-ip-here>:4438/topics/sampleTopic3/eventSubscriptions/sampleSubscription3?api-version=2019-01-01-preview
    

    Sample output:

         {
          "id": "/iotHubs/eg-iot-edge-hub/devices/eg-edge-device/modules/eventgridmodule/topics/sampleTopic3/eventSubscriptions/sampleSubscription3",
          "type": "Microsoft.EventGrid/eventSubscriptions",
          "name": "sampleSubscription3",
          "properties": {
            "Topic": "sampleTopic3",
            "destination": {
              "endpointType": "eventGrid",
              "properties": {
                "endpointUrl": "https://testegcloudtopic.westus2-1.eventgrid.azure.net/api/events?api-version=2018-01-01",
                "sasKey": "<your-event-grid-topic-saskey>",
                "topicName": null
              }
            }
          }
        }
    

Publish an event at the edge

  1. Create event3.json with the following content. See API documentation for details about the payload.

        [
          {
            "id": "eventId-egcloud-0",
            "eventType": "recordInserted",
            "subject": "myapp/vehicles/motorcycles",
            "eventTime": "2019-07-28T21:03:07+00:00",
            "dataVersion": "1.0",
            "data": {
              "make": "Ducati",
              "model": "Monster"
            }
          }
        ]
    
  2. Run the following command:

    curl -k -H "Content-Type: application/json" -X POST -g -d @event3.json https://<your-edge-device-public-ip-here>:4438/topics/sampleTopic3/events?api-version=2019-01-01-preview
    

Verify edge event in cloud

For information on viewing events delivered by the cloud topic, see the tutorial.

Cleanup resources

  • Run the following command to delete the topic and all its subscriptions

    curl -k -H "Content-Type: application/json" -X DELETE https://<your-edge-device-public-ip-here>:4438/topics/sampleTopic3?api-version=2019-01-01-preview
    
  • Delete topic and subscriptions created in the cloud (Azure Event Grid) as well.

Next steps

In this tutorial, you published an event on the edge and forwarded to Event Grid in the Azure cloud. Now that you know the basic steps to forward to Event Grid in cloud:

  • To troubleshoot issues with using Azure Event Grid on IoT Edge, see Troubleshooting guide.
  • Forward events to IoTHub by following this tutorial
  • Forward events to Webhook in the cloud by following this tutorial