Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit editor mode

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

Feedback

In this article

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.

Create an Azure function using Event Grid trigger

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 (custom topic)

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.

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

Note

UseCloud Event Schema v1.0 everywhere in this tutorial.

Add a subscription to the topic using the function

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

  1. On theEvent Grid Topic page, selectSubscriptions.

    Screenshot that shows the Event Subscriptions page for a topic.

  2. On theCreate Event Subscription page, do these steps:

    1. Enter aName for the event subscription.

    2. ForEvent Schema, selectCloud Event Schema 1.0.

    3. ForEndpoint Type, selectAzure Function.

    4. Then, selectConfigure an endpoint.

      Screenshot that shows the Create event subscription page.

  3. On theSelect Azure function page, do these steps:

    1. ForSubscription, select your Azure subscription.

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

    3. ForFunction app, select the Functions app that has the function.

    4. ForSlot, selectProduction.

    5. ForFunction, select your Azure Function.

    6. Then, selectConfirm Selection.

      Screenshot that shows the Select Azure function page.

  4. On theCreate Event Subscription page, selectCreate.

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

Create namespace, clients, topic spaces, and permission bindings

Follow instructions fromQuickstart: 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 usingMQTTX 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 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.

  1. On theEvent Grid Namespace page, selectIdentity. SelectOn and thenSave.

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

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

  3. SelectAccess control on the left navigation bar.

  4. On theAccess control page, selectAdd, and then selectAdd role assignment.

    Screenshot that shows the Access control page.

  5. On theRole page of theAdd role assignment wizard, selectEvent Grid Data Sender role, and selectNext.

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

  6. OnAdd role assignment, on theMembers page, selectManaged identity, and then chooseSelect members.

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

  7. On theSelect managed identities page, do these steps:

    1. Select your Azure subscription.

    2. ForManaged identity, selectEvent Grid Namespace.

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

    4. ChooseSelect.

      Screenshot that shows the Select managed identities page.

  8. On theAdd role assignment page, selectNext.

  9. On theReview + assign page, review settings, and then selectReview + assign.

Configure routing messages to Azure function via custom topic

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

  1. On theEvent Grid Namespace page, selectRouting.

  2. On theRouting page, selectEnable routing.

  3. ForTopic type, selectCustom topic.

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

  5. ForManaged identity for delivery, selectSystem Assigned.

  6. SelectApply.

    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 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:

  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 received the event.

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

Next step


Feedback

Was this page helpful?

YesNoNo

Need help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?

  • Last updated on

In this article

Was this page helpful?

YesNo
NoNeed help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?