Tutorial: Route MQTT messages in Azure Event Grid to Azure Functions using custom topics - Azure portal

In this tutorial, you learn how to route MQTT messages received by an Azure Event Grid namespace to an Azure function via an Event Grid custom topic by following these steps:

If you don't have an Azure subscription, you can sign up for a free trial.

Create an Azure function using Event Grid trigger

Follow instructions from Create an Azure function using Visual Studio Code, but use the Azure Event Grid Trigger instead of using the HTTP Trigger. 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

  • Create all resources in the same region.
  • 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 portal. When you create the Event Grid topic, on the Advanced tab, for Event Schema, select Cloud Event Schema v1.0.

Screenshot that shows the Advanced page of the Create Topic wizard.

Note

Use Cloud event schema everywhere in this tutorial.

Add a subscription to the topic using the function

In this step, you create a subscription to the Event Grid topic using the Azure function you created earlier.

  1. On the Event Grid topic page, select Subscriptions on the left navigation bar.

    Screenshot that shows the Event Subscriptions page for a topic.

  2. On the Create event subscription page, do these steps:

    1. Enter a name for the event subscription.

    2. For Event schema, select Cloud Event Schema 1.0.

    3. For Endpoint type, select Azure Functions.

    4. Then, select Configure an endpoint.

      Screenshot that shows the Create event subscription page.

  3. On the Select Azure function page, do these steps:

    1. For Subscription, select your Azure subscription.

    2. For Resource group, select the resource group that has your Azure function.

    3. For Function app, select the Functions app that has the function.

    4. For Slot, select Production.

    5. For Function, select your Azure function.

    6. Then, select Confirm selection at the bottom of the page.

      Screenshot that shows the Select Azure function page.

  4. On the Create Event Subscription page, select Create.

  5. On the Event Subscriptions page, you should see the subscription you created.

Create namespace, clients, topic spaces, and permission bindings

Follow instructions from Quickstart: Publish and subscribe to MQTT messages using an Event Grid namespace with Azure portal to:

  1. Create an Event Grid namespace.
  2. Create two clients.
  3. Create a topic space.
  4. Create publisher and subscriber permission bindings.
  5. Test using MQTTX app to confirm that clients are able to send and receive messages.

Enable managed identity for the namespace

In this section, you enable system-assigned managed identity for the Event Grid namespace. Then, grant identity the send permission to the Event Grid custom topic you created earlier so that it can route message to the custom topic. You do so by adding the managed identity to the Event Grid Data Sender role on the custom topic.

  1. On the Event Grid Namespace page, select Identity on the left navigation menu.

    Screenshot that shows the Event Grid Namespace with the Identity tab selected.

  2. Navigate to the Event Grid Topic for your Event Grid custom topic.

  3. Select Access control on the left navigation bar.

  4. On the Access control page, select Add, and then select Add role assignment.

    Screenshot that shows the Access control page.

  5. On the Role page of the Add role assignment wizard, select Event Grid Data Sender role, and select Next at the bottom of the page.

    Screenshot that shows the **Role** page of the **Add role assignment** wizard.

  6. On the Members page of the Add role assignment wizard, select Managed identity, and then choose Select members.

    Screenshot that shows the Members page of the Add role assignment wizard.

  7. On the Select managed identities page, do these steps:

    1. Select your Azure subscription.

    2. For Managed identity, select Event Grid Namespace.

    3. Select the managed identity that has the same name as the Event Grid namespace.

    4. Choose Select at the bottom of the page.

      Screenshot that shows the Select managed identities page.

  8. On the Add role assignment page, select Next at the bottom of the page.

  9. On the Review + assign page, review settings, and then select Review + assign at the bottom of the page.

Configure routing messages to Azure function via custom topic

In this step, you configure routing for the Event Grid namespace so that the messages it receives are routed to the custom topic you created.

  1. On the Event Grid Namespace page, select Routing on the left navigation bar.

  2. On the Routing page, select Enable routing.

  3. For Topic type, select Custom topic.

  4. For Topic, select the custom topic you created for this tutorial.

  5. For Managed identity for delivery, select System Assigned.

  6. Select Apply at the bottom of the page.

    Screenshot that shows the Routing page for a namespace.

Send test MQTT messages using MQTTX

Send test MQTT messages to the namespace and confirm that the function receives them.

Follow instructions from the Publish, subscribe messages using MQTTX app article to send a few test messages to the Event Grid namespace.

Here's the flow of the events or messages:

  1. MQTTX sends messages to the topic space of the Event Grid namespace.

  2. The messages get routed to the custom topic that you configured.

  3. The messages are forwarded to the event subscription, which is the Azure function.

  4. Use the logging feature to verify that the function has received the event.

    Screenshot that shows the Log stream page for an Azure function.

Next step

See code samples in this GitHub repository.