This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can trysigning in orchanging directories.
Access to this page requires authorization. You can trychanging directories.
In this tutorial, you route MQTT messages received by an Azure Event Grid namespace to an Azure function by using an Event Grid custom topic.
If you don't have an Azure subscription, you can sign up for afree trial.
Follow instructions fromCreate an Azure function using Visual Studio Code, but use theAzure Event Grid Trigger instead of using theHTTP Trigger.
Important
Create all resources in the same region.
You should see code similar to the following example:
using System;using Azure.Messaging;using Microsoft.Azure.Functions.Worker;using Microsoft.Extensions.Logging;namespace Company.Function{ public class MyEventGridTriggerFunc { private readonly ILogger<MyEventGridTriggerFunc> _logger; public MyEventGridTriggerFunc(ILogger<MyEventGridTriggerFunc> logger) { _logger = logger; } [Function(nameof(MyEventGridTriggerFunc))] public void Run([EventGridTrigger] CloudEvent cloudEvent) { _logger.LogInformation("Event type: {type}, Event subject: {subject}", cloudEvent.Type, cloudEvent.Subject); } }}You use this Azure function as an event handler for a topic's subscription later in this tutorial.
Note
This tutorial has been tested with an Azure function that uses .NET 8.0 (isolated) runtime stack.
Create an Event Grid topic. SeeCreate a custom topic using the Azure portal. When you create the Event Grid topic, on theAdvanced tab, forEvent Schema, selectCloud Event Schema v1.0.

Note
UseCloud Event Schema v1.0 everywhere in this tutorial.
In this step, create a subscription to the Event Grid topic using the Azure function you created earlier.
On theEvent Grid Topic page, selectSubscriptions.
On theCreate Event Subscription page, do these steps:
Enter aName for the event subscription.
ForEvent Schema, selectCloud Event Schema 1.0.
ForEndpoint Type, selectAzure Function.
Then, selectConfigure an endpoint.

On theSelect Azure function page, do these steps:
ForSubscription, select your Azure subscription.
ForResource group, select the resource group that has your Azure function.
ForFunction app, select the Functions app that has the function.
ForSlot, selectProduction.
ForFunction, select your Azure Function.
Then, selectConfirm Selection.

On theCreate Event Subscription page, selectCreate.
On theEvent Subscriptions page, you should see the subscription you created.
Follow instructions fromQuickstart: Publish and subscribe to MQTT messages using an Event Grid namespace with Azure portal to:
In this section, you enable system-assigned managed identity for the Event Grid namespace. Then, grant the identity thesend permission to the Event Grid custom topic. It can then route message to the custom topic. You enable it by adding the managed identity to theEvent Grid Data Sender role on the custom topic.
On theEvent Grid Namespace page, selectIdentity. SelectOn and thenSave.
Navigate to theEvent Grid Topic for your Event Grid custom topic.
SelectAccess control on the left navigation bar.
On theAccess control page, selectAdd, and then selectAdd role assignment.
On theRole page of theAdd role assignment wizard, selectEvent Grid Data Sender role, and selectNext.
OnAdd role assignment, on theMembers page, selectManaged identity, and then chooseSelect members.

On theSelect managed identities page, do these steps:
Select your Azure subscription.
ForManaged identity, selectEvent Grid Namespace.
Select the managed identity that has the same name as the Event Grid namespace.
ChooseSelect.

On theAdd role assignment page, selectNext.
On theReview + assign page, review settings, and then selectReview + assign.
In this section, configure routing for the Event Grid namespace so that the messages it receives are routed to the custom topic you created.
On theEvent Grid Namespace page, selectRouting.
On theRouting page, selectEnable routing.
ForTopic type, selectCustom topic.
ForTopic, select the custom topic you created for this tutorial.
ForManaged identity for delivery, selectSystem Assigned.
SelectApply.
Send test MQTT messages to the namespace and confirm that the function receives them.
Follow instructions fromPublish and subscribe using MQTTX app to send a few test messages to the Event Grid namespace.
Here's the flow of the events or messages:
MQTTX sends messages to the topic space of the Event Grid namespace.
The messages get routed to the custom topic that you configured.
The messages are forwarded to the event subscription, which is the Azure function.
Use the logging feature to verify that the function received the event.
Was this page helpful?
Need help with this topic?
Want to try using Ask Learn to clarify or guide you through this topic?
Was this page helpful?
Want to try using Ask Learn to clarify or guide you through this topic?