As an alternative to Azure IoT Hub routing, Event Grid can help us route messages to additional Azure services through topics and subscriptions.
In this lab, we will integrate an Azure IoT Hub with Event Grid and set up a subscriber service to consume messages sent by a simulated device. As an extra challenge, the configuration of your Event Grid topic and subscription will be described in an ARM template, which you will use as your roadmap, while navigating through the Event Grid wizard in the Azure portal.
Successfully complete this lab by achieving the following learning objectives:
- Create and Connect a Simulated Device to Azure IoT Hub
- Log in to the Azure portal, and navigate to the only IoT Hub already deployed in the lab subscription.
- Create a new device, and copy the Primary Connection String.
- Use the following link to open the device simulator in a new browser tab or window.
- Once the simulator is open, in the code pane, find and modify the connection string using the Primary Connection String you copied from the new device registration.
- Run the code and observe changes in the telemetry messages.
- Allow the code to continue to run.
- Back in the Azure portal subscription, return to the IoT Hub, and view the IoT Hub Usage tile. You should see at least a few messages after the simulated device has been running a few minutes. Refresh as needed.
Note: Even after confirming message delivery in the hub, allow the simulated device to continue sending messages. You will want data to be streaming in order to validate your Event Grid solution in the remaining objectives.
- Create an Event Grid Topic and Subscription
- Retrieve the ARM template as directed in the Additional Resources section of this lab, and open it in Visual Studio Code or another code editor. A text editor is fine, but it will be harder to read.
- In the Azure portal, go to the Azure IoT Hub where your device is deployed. Select Events to add a new Event Grid subscription and topic.
- Using the ARM template as your guide, walk through the wizard to create a new topic and subscription that matches, as closely as possible, the resources described in the ARM template.
Optional: In the last step of the wizard, on the Advanced tab, the portal displays an ARM template of the resources you are about to create. Copy the template code and save it in a .JSON document for later reference.
Tip: See the Additional Resources section of this lab for tips on utilizing the ARM template.
- Verify Your Event Grid Integration
- Double-check that the simulated device is continuing to send telemetry to the hub.
- On the Events page (where you should have landed after creating the topic and subscription), refresh the metrics charts to verify that data is flowing through the Event Grid. You may need to wait a few minutes after deployment for data to begin flowing. Also, because the Event Grid subscription has a filter based on a randomly generated temperature value of above 30 degrees, you may have an additional delay in seeing messages flowing through Event Grid.
- Navigate to the target event handler of the subscription endpoint you created, and verify that the correct data is arriving there.
Note: The message header will be in clear text, but the message body is encrypted, which is the expected behavior.
- Optional: If you chose to save off the ARM template of your new resources in the last objective, use whatever code-compare feature you have in your code editor to see how close your new template came to matching the template you were provided.