Tutorial: Route MQTT messages to Azure Event Hubs from Azure Event Grid with Azure CLI (Preview)
Use message routing in Azure Event Grid to send data from your MQTT clients to Azure services such as storage queues, and Event Hubs.
Important
The MQTT Messaging and Pull Delivery features along with the following resources are currently in PREVIEW.
- Namespaces
- Namespace topics
- Event subscriptions to namespace topics
- Topic spaces
- Clients
- Client Groups
- Certificates
- Permission Bindings
In this article, 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 Stream Analytics
Prerequisites
- Complete the Publish and subscribe on a MQTT topic Quickstart. In this tutorial, you can update the same namespace to enable routing.
- This tutorial uses Event Hubs, Event Grid Custom Topics, and Event Subscriptions. You can find more information here:
- Create an event hub that is used as an event handler for events sent to the custom topic - Quickstart - Create an event hub using Azure CLI - Azure Event Hubs.
- Process events sent to the event hub using Stream Analytics, which writes output to any destination that ASA supports - Process data from Event Hubs Azure using Stream Analytics - Azure Event Hubs.
Important
The Azure CLI Event Grid extension does not yet support namespaces and any of the resources it contains. We will use Azure CLI resource to create Event Grid resources.
Create Event Grid topic
- Create Event Grid Custom Topic with your EG custom topic name, region name and resource group name.
az eventgrid topic create --name {EG custom topic name} -l {region name} -g {resource group name} --input-schema cloudeventschemav1_0
- Assign Data Sender role to the Event Grid topic
az role assignment create --assignee "{Your Principal ID}" --role "EventGrid Data Sender" --scope "/subscriptions/{Subscription ID}/resourcegroups/{Resource Group ID}/providers/Microsoft.EventGrid/topics/{EG Custom Topic Name}"
Note
You can find your principal ID using the command in PowerShell
- az ad signed-in-user show
Create Event Subscription with Event Hubs as endpoint
- Create Event Grid Subscription. Update it with your subscription ID, resource group ID, EG custom topic name.
az eventgrid event-subscription create --name contosoEventSubscription --source-resource-id "/subscriptions/{Your Subscription ID}/resourceGroups/{Your Resource Group ID}/providers/Microsoft.EventGrid/topics/{Your Event Grid Topic Name}" --endpoint-type eventhub --endpoint /subscriptions/{Your Subscription ID}/resourceGroups/{Your Resource Group ID}/providers/Microsoft.EventHub/namespaces/{Event Hub Namespace Name}/eventhubs/{Event Hub Name} --event-delivery-schema cloudeventschemav1_0
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.
Configure routing in the Event Grid Namespace
- We use the namespace created in the Publish and subscribe on a MQTT topic. Update the Namespace object in namespace.json file to enable routing to the Event Grid topic in this step.
{
"properties": {
"inputSchema": "CloudEventSchemaV1_0",
"topicSpacesConfiguration": {
"state": "Enabled",
"routeTopicResourceId": "/subscriptions/{Subscription ID}/resourceGroups/{Resource Group ID}/providers/Microsoft.EventGrid/topics/{EG Custom Topic Name}"
},
"isZoneRedundant": true
},
"location": "{region name}"
}
Use the az resource command to create a namespace. Update the command with your subscription ID, Resource group ID, and a Namespace name.
az resource create --resource-type Microsoft.EventGrid/namespaces --id /subscriptions/{Subscription ID}/resourceGroups/{Resource Group}/providers/Microsoft.EventGrid/namespaces/{Namespace Name} --is-full-object --api-version 2023-06-01-preview --properties @./resources/namespace.json
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