Tutorial: Route MQTT messages to Azure Event Hubs from Azure Event Grid with Azure portal
Use message routing in Azure Event Grid to send data from your MQTT clients to Azure services such as storage queues, and Event Hubs. In this tutorial, you perform the following tasks:
- Create Event Subscription in your Event Grid topic.
- Configure routing in your Event Grid Namespace.
- View the MQTT messages in the Event Hubs using Azure Stream Analytics.
Prerequisites
- If you don't have an Azure subscription, create an Azure free account before you begin.
- If you're new to Azure Event Grid, read through Event Grid overview before starting this tutorial.
- Register the Event Grid resource provider as per Register the Event Grid resource provider.
- Make sure that port 8883 is open in your firewall. The sample in this tutorial uses MQTT protocol, which communicates over port 8883. This port might be blocked in some corporate and educational network environment.
- 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 topic, and Event Subscriptions. You can find more information here:
- Creating an Event Grid topic: Create a custom topic using portal. While creating the Event Grid topic, ensure to create with Event Schema as Cloud Event Schema v1.0 in the Advanced tab.
Creating an Event Hubs: Quickstart - Create an event hub using Azure portal.
You can view the Event Hubs data using Stream Analytics: Process data from Event Hubs Azure using Stream Analytics.
Create Event Subscription with Event Hubs as endpoint
- In your Event Grid topic, go to Event Subscriptions page under Entities in left rail.
- Select + Event Subscription to add new subscription.
- For Event Schema, select Cloud Event Schema v1.0.
- Add Event Hubs as Endpoint Type, and provide your event hub information for endpoint, and select Confirm Selection button.
- Then Create the Event subscription.
Configure routing in the Event Grid Namespace
Select Enable routing
Select 'Custom topic' option for Topic type
Under Topic, select the Event Grid topic that you have created where all MQTT messages will be routed.
- Select Apply
Note
Routing Event Grid topic considerations: The Event Grid topic that is used for routing has to fulfill following requirements:
- It needs to be set to use the Cloud Event Schema v1.0.
- It needs to be in the same region as the namespace.
- You need to assign "EventGrid Data Sender" role to yourself on the Event Grid Topic.
Configuration
- Go to the Event Grid topic resource.
- In the "Access control (IAM)" menu item, select Add role assignment.
- In the "Role" tab, select "EventGrid Data Sender", then select Next.
- In the "Members" tab, click on + Select members, then type your AD user name in the "Select" box that will appear (e.g. user@contoso.com).
- Select your AD user name, then select Review + assign.
Viewing the routed MQTT messages in Azure Event Hubs using Azure Stream Analytics query
After configuring the routing for the namespace, publish MQTT messages among the clients (as described in the article Publish and subscribe on an MQTT topic.)
Navigate to the Event Hubs instance within your Event Subscription on Azure portal. Process data from your event hub using Azure Stream Analytics. (Process data from Event Hubs Azure using Stream Analytics - Azure Event Hubs | Microsoft Learn) You can see the MQTT messages in the query.
Next steps
- For code samples, go to this repository.
Feedback
Submit and view feedback for