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.


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



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}" 


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


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

    "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

Screenshot showing the MQTT messages data in Event Hubs using Azure Stream Analytics query tool.

Next steps