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're new to Event Grid, read the Event Grid overview before you start this tutorial.
Register the Event Grid resource provider according to the steps in Register the Event Grid resource provider.
Make sure that port 8883 is open in your firewall. The sample in this tutorial uses the MQTT protocol, which communicates over port 8883. This port might be blocked in some corporate and educational network environments.
You need an Event Grid namespace in your Azure subscription. If you don't have a namespace yet, you can follow the steps in Publish and subscribe on a MQTT topic.
This tutorial uses event hubs, Event Grid custom topics, and event subscriptions. You can find more information here:
- Create an Event Grid topic. See Create a custom topic using the portal. When you create the Event Grid topic, on the Advanced tab, for Event Schema, select Cloud Event Schema v1.0.
- Create an event hub. See Quickstart: Create an event hub using the Azure portal.
View the Event Hubs data by using Stream Analytics. For more information, see Process data from Azure Event Hubs using Stream Analytics.
Create an event subscription with Event Hubs as the endpoint
On your Event Grid topic, go to the Event Subscription page under Entities in the left pane.
Select + Event Subscription to add a new subscription.
For Event Schema, select Cloud Event Schema v1.0.
For Endpoint Type, select Event Hubs, enter your event hub information for the endpoint, and select Confirm Selection.
Create the event subscription.
Configure routing in the Event Grid namespace
Go to the Routing page in your Event Grid namespace.
Select Enable routing.
For Topic type, select Custom topic.
For Topic, select the Event Grid topic that you created where all MQTT messages will be routed.
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.
- Go to the Event Grid topic resource.
- On the Access control (IAM) menu, select Add role assignment.
- On the Role tab, select Event Grid Data Sender. Then select Next.
- On the Members tab, click + Select members. Then enter your Microsoft Entra username in the Select box that appears (for example, firstname.lastname@example.org).
- Select your Microsoft Entra username, and then select Review + assign.
View the routed MQTT messages in Event Hubs by using a Stream Analytics query
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.
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.
For code samples, go to this GitHub repository.