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 a free trial.
Create an Azure function using Event Grid triggerFollow instructions from Create an Azure function using Visual Studio Code, but use the Azure Event Grid Trigger instead of using the HTTP 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 (custom topic)Create an Event Grid topic. See Create a custom topic using the Azure portal. When you create the Event Grid topic, on the Advanced tab, for Event Schema, select Cloud Event Schema v1.0.
Note
Use Cloud Event Schema v1.0 everywhere in this tutorial.
Add a subscription to the topic using the functionIn this step, create a subscription to the Event Grid topic using the Azure function you created earlier.
On the Event Grid Topic page, select Subscriptions.
On the Create Event Subscription page, do these steps:
Enter a Name for the event subscription.
For Event Schema, select Cloud Event Schema 1.0.
For Endpoint Type, select Azure Function.
Then, select Configure an endpoint.
On the Select Azure function page, do these steps:
For Subscription, select your Azure subscription.
For Resource group, select the resource group that has your Azure function.
For Function app, select the Functions app that has the function.
For Slot, select Production.
For Function, select your Azure Function.
Then, select Confirm Selection.
On the Create Event Subscription page, select Create.
On the Event Subscriptions page, you should see the subscription you created.
Follow instructions from Quickstart: 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 the send 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 the Event Grid Data Sender role on the custom topic.
On the Event Grid Namespace page, select Identity. Select On and then Save.
Navigate to the Event Grid Topic for your Event Grid custom topic.
Select Access control on the left navigation bar.
On the Access control page, select Add, and then select Add role assignment.
On the Role page of the Add role assignment wizard, select Event Grid Data Sender role, and select Next.
On Add role assignment, on the Members page, select Managed identity, and then choose Select members.
On the Select managed identities page, do these steps:
Select your Azure subscription.
For Managed identity, select Event Grid Namespace.
Select the managed identity that has the same name as the Event Grid namespace.
Choose Select.
On the Add role assignment page, select Next.
On the Review + assign page, review settings, and then select Review + 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 the Event Grid Namespace page, select Routing.
On the Routing page, select Enable routing.
For Topic type, select Custom topic.
For Topic, select the custom topic you created for this tutorial.
For Managed identity for delivery, select System Assigned.
Select Apply.
Send test MQTT messages to the namespace and confirm that the function receives them.
Follow instructions from Publish 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.
Samples in MqttApplicationSamples.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4