Tutorial: Route MQTT messages to Azure Event Hubs from Azure Event Grid with the Azure portal

Use message routing in Azure Event Grid to send data from your MQTT clients to Azure services, such as storage queues and Azure Event Hubs.

In this tutorial, you learn how to:

  • Create an event subscription in your Event Grid topic.
  • Configure routing in your Event Grid namespace.
  • View the MQTT messages in the event hubs by using Azure Stream Analytics.

If you don't have an Azure subscription, create an Azure free account before you begin.

Prerequisites

Create an event subscription with Event Hubs as the endpoint

  1. On your Event Grid topic, go to the Event Subscription page under Entities in the left pane.

  2. Select + Event Subscription to add a new subscription.

    Screenshot that shows the Add Subscription button on the Event Grid topic overview page.

  3. For Event Schema, select Cloud Event Schema v1.0.

  4. For Endpoint Type, select Event Hubs, enter your event hub information for the endpoint, and select Confirm Selection.

    Screenshot that shows the event subscription creation flow and configuration.

  5. Create the event subscription.

Configure routing in the Event Grid namespace

  1. Go to the Routing page in your Event Grid namespace.

    Screenshot that shows the Event Grid routing configuration page to enable routing for the namespace.

  2. Select Enable routing.

  3. For Topic type, select Custom topic.

  4. For Topic, select the Event Grid topic that you created where all MQTT messages will be routed.

    Screenshot that shows the routing configuration through the portal.

  5. Select Apply.

Routing Event Grid topic considerations

The Event Grid topic used for routing must fulfill the following requirements:

  • It must be set to use the Cloud Event Schema v1.0.
  • It must be in the same region as the namespace.
  • You need to assign the Event Grid Data Sender role to yourself on the Event Grid topic.

Configuration

  1. Go to the Event Grid topic resource.
  2. On the Access control (IAM) menu, select Add role assignment.
  3. On the Role tab, select Event Grid Data Sender. Then select Next.
  4. On the Members tab, click + Select members. Then enter your Microsoft Entra username in the Select box that appears (for example, user@contoso.com).
  5. Select your Microsoft Entra username, and then select Review + assign.

View the routed MQTT messages in Event Hubs by using a Stream Analytics query

  1. After you configure the routing for the namespace, publish MQTT messages among the clients. For more information, see Publish and subscribe on an MQTT topic.

  2. Go to the Event Hubs instance within your event subscription in the Azure portal. Process data from your event hub by using Stream Analytics. For more information, see Process data from Azure Event Hubs using Stream Analytics - Azure Event Hubs | Microsoft Learn. You can see the MQTT messages in the query.

    Screenshot that shows the MQTT messages data in Event Hubs by using the Stream Analytics query tool.

Next steps

For code samples, go to this GitHub repository.