Deliver IoT Device Telemetry to Other Services Using Event Grid

45 minutes
  • 3 Learning Objectives

About this Hands-on Lab

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.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Create and Connect a Simulated Device to Azure IoT Hub
  1. Log in to the Azure portal, and navigate to the only IoT Hub already deployed in the lab subscription.
  2. Create a new device, and copy the Primary Connection String.
  3. Use the following link to open the device simulator in a new browser tab or window.
  4. 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.
  5. Run the code and observe changes in the telemetry messages.
  6. Allow the code to continue to run.
  7. 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
  1. 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.
  2. 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.
  3. 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
  1. Double-check that the simulated device is continuing to send telemetry to the hub.
  2. 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.
  3. 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.
  4. 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.

Additional Resources

Imagine that you have recently been hired by Railtronics, a monitoring company that works with rail carriers to automate the monitoring and management of station equipment and facilities. They have decided to use Azure IoT to develop a solution that manages environmental temperature and humidity on sensors in both public areas, as well as enclosed equipment housing.

You have investigated IoT Hub routing options for the delivery of data to data processing services, but the short list of valid destination endpoints available through hub routing is not sufficient to your needs. You have read that Event Grid might be a better solution, and you are asked to put together a small proof-of-concept for the next sprint demo.

As a sideline to your main tasks, you’ve been trying to put together a proposal for CI/CD pipelines for Azure resources, through the use of ARM templates and/or Bicep documents, so you have been educating yourself around ARM templates. As luck would have it, this lab is your chance to practice your ARM template reading skills along with demonstrating how Event Grid might present a routing option for your IoT solutions!

Accessing the Device Simulator

Use the following link to access the device simulator.

Downloading the master ARM Template

Use the following link to access the ARM template. When the ARM template code displays, select the Copy raw contents icon in the upper right of the data pane. Alternatively, select the Raw button to manually select and copy the text. Paste the code into a new document in your code editor, and save it as a .JSON document.

ARM Template Tips

Because every lab environment is slightly different, certain existing resources will be named differently from your lab. For example, the name of your hub might be myHub-123 and the name of the hub in an ARM template might be myHub-999. On the other hand, if a resource in the ARM template is something new you are creating, you should go ahead and give your resource the same name. For example, if the ARM template has a subscription called mySubscription, you should give yours the same name, for the purposes of this exercise.

Check out this link for a basic introduction to ARM Templates.

WARNING: Be Prepared for UI Changes

Given the fluid nature of Microsoft cloud tools and open-source projects, you may experience user interface (UI) changes that were made following the development of this hands-on lab that do not match up with lab instructions. When any such changes are brought to our attention, we will attempt to update the content accordingly. However, if changes occur, students will have to adapt to the changes and work through them in the hands-on labs as needed.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?