|
| 1 | +--- |
| 2 | +title: 'Tutorial: Route MQTT messages to Azure Functions - CLI' |
| 3 | +description: 'Tutorial: Use custom topics in Azure Event Grid to route MQTT messages to Azure Functions using the Routing feature. You use the Azure CLI in this tutorial.' |
| 4 | +ms.topic: tutorial |
| 5 | +ms.date: 03/14/2024 |
| 6 | +author: george-guirguis |
| 7 | +ms.author: geguirgu |
| 8 | +ms.subservice: mqtt |
| 9 | +--- |
| 10 | + |
| 11 | +# Tutorial: Route MQTT messages in Azure Event Grid to Azure Functions using custom topics - Azure portal |
| 12 | + |
| 13 | +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: |
| 14 | + |
| 15 | +If you don't have an Azure subscription, you can sign up for a [free trial](https://azure.microsoft.com/free/dotnet). |
| 16 | + |
| 17 | +## Prerequisites |
| 18 | +Follow instructions from [Create an Azure function using Visual Studio Code](../azure-functions/functions-develop-vs-code.md), but use the **Azure Event Grid Trigger** instead of using the **HTTP Trigger**. You should see code similar to the following example: |
| 19 | + |
| 20 | +```csharp |
| 21 | +using System; |
| 22 | +using Azure.Messaging; |
| 23 | +using Microsoft.Azure.Functions.Worker; |
| 24 | +using Microsoft.Extensions.Logging; |
| 25 | + |
| 26 | +namespace Company.Function |
| 27 | +{ |
| 28 | + public class MyEventGridTriggerFunc |
| 29 | + { |
| 30 | + private readonly ILogger<MyEventGridTriggerFunc> _logger; |
| 31 | + |
| 32 | + public MyEventGridTriggerFunc(ILogger<MyEventGridTriggerFunc> logger) |
| 33 | + { |
| 34 | + _logger = logger; |
| 35 | + } |
| 36 | + |
| 37 | + [Function(nameof(MyEventGridTriggerFunc))] |
| 38 | + public void Run([EventGridTrigger] CloudEvent cloudEvent) |
| 39 | + { |
| 40 | + _logger.LogInformation("Event type: {type}, Event subject: {subject}", cloudEvent.Type, cloudEvent.Subject); |
| 41 | + } |
| 42 | + } |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +You use this Azure function as an event handler for a topic's subscription later in this tutorial. |
| 47 | + |
| 48 | +> [!NOTE] |
| 49 | +> - Create all resources in the same region. |
| 50 | +> - This tutorial has been tested with an Azure function that uses .NET 8.0 (isolated) runtime stack. |
| 51 | +
|
| 52 | +## Create an Event Grid topic (custom topic) |
| 53 | + |
| 54 | +```azurecli-interactive |
| 55 | +rgName="RESOURCEGROUPNAME" |
| 56 | +location="REGION" |
| 57 | +topicName="TOPICNAME" |
| 58 | +
|
| 59 | +az group create -n $rgName -l $location |
| 60 | +az eventgrid topic create --name $topicName -l $location -g $rgName --input-schema cloudeventschemav1_0 |
| 61 | +``` |
| 62 | + |
| 63 | +> [!NOTE] |
| 64 | +> Use **Cloud event schema** everywhere in this tutorial. |
| 65 | +
|
| 66 | +## Add a subscription to the topic using the function |
| 67 | +In this step, you create a subscription to the Event Grid topic using the Azure function you created earlier. |
| 68 | + |
| 69 | +```azurecli-interactive |
| 70 | +funcAppRgName="FUNCTIONRESOURCEGROUP" |
| 71 | +funcAppName="FUNCTIONSAPPNAME" |
| 72 | +funcName="FUNCTIONNAME" |
| 73 | +funcResourceId=$(az functionapp function show -g $funcAppRgName -n $funcAppName --function-name $funcName --query "{I:id}" -o tsv) |
| 74 | +
|
| 75 | +topicResourceId=$(az eventgrid topic show --name $topicName -g $rgName --query id --output tsv) |
| 76 | +eventSubscriptionName="EVENTSUBSCRIPTIONNAME" |
| 77 | +az eventgrid event-subscription create --name $eventSubscriptionName --source-resource-id $topicResourceId --endpoint-type azurefunction --endpoint $funcResourceId --event-delivery-schema cloudeventschemav1_0 |
| 78 | +``` |
| 79 | + |
| 80 | + |
| 81 | +## Create namespace, clients, topic spaces, and permission bindings |
| 82 | + |
| 83 | +Follow instructions from [Quickstart: Publish and subscribe to MQTT messages on an Event Grid namespace with the Azure CLI](mqtt-publish-and-subscribe-cli.md) to: |
| 84 | + |
| 85 | +1. Create an Event Grid namespace. |
| 86 | +1. Create two clients. |
| 87 | +1. Create a topic space. |
| 88 | +1. Create publisher and subscriber permission bindings. |
| 89 | +1. Test using [MQTTX app](mqtt-publish-and-subscribe-portal.md#connecting-the-clients-to-the-eg-namespace-using-mqttx-app) to confirm that clients are able to send and receive messages. |
| 90 | + |
| 91 | +## Enable managed identity for the namespace |
| 92 | + |
| 93 | +Enable system-assigned managed identity for the Event Grid namespace. |
| 94 | + |
| 95 | +```azurecli-interactive |
| 96 | +nsName="EVENTGRIDNAMESPACENAME" |
| 97 | +az eventgrid namespace update -g $rgName -n $nsName --topic-spaces-configuration "{state:Enabled}" --identity "{type:SystemAssigned}" |
| 98 | +``` |
| 99 | + |
| 100 | +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. |
| 101 | + |
| 102 | +```azurecli-interactive |
| 103 | +egNamespaceServicePrincipalObjectID=$(az ad sp list --display-name $nsName --query [].id -o tsv) |
| 104 | +topicResourceId=$(az eventgrid topic show --name $topicName -g $rgName --query id --output tsv) |
| 105 | +az role assignment create --assignee $egNamespaceServicePrincipalObjectID --role "EventGrid Data Sender" --scope $topicResourceId |
| 106 | +``` |
| 107 | + |
| 108 | +## Configure routing messages to Azure function via custom topic |
| 109 | +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. |
| 110 | + |
| 111 | +```azurecli-interactive |
| 112 | +az eventgrid namespace update -g $rgName -n $nsName --topic-spaces-configuration "{state:Enabled,'routeTopicResourceId':$topicResourceId,'routingIdentityInfo':{type:SystemAssigned}}" |
| 113 | +``` |
| 114 | + |
| 115 | +## Send test MQTT messages using MQTTX |
| 116 | +Send test MQTT messages to the namespace and confirm that the function receives them. |
| 117 | + |
| 118 | +Follow instructions from the [Publish, subscribe messages using MQTTX app](mqtt-publish-and-subscribe-portal.md#publishsubscribe-using-mqttx-app) article to send a few test messages to the Event Grid namespace. |
| 119 | + |
| 120 | +Here's the flow of the events or messages: |
| 121 | + |
| 122 | +1. MQTTX sends messages to the topic space of the Event Grid namespace. |
| 123 | +1. The messages get routed to the custom topic that you configured. |
| 124 | +1. The messages are forwarded to the event subscription, which is the Azure function. |
| 125 | +1. Use the logging feature to verify that the function has received the event. |
| 126 | + |
| 127 | + :::image type="content" source="./media/mqtt-routing-to-azure-functions-portal/function-log-stream.png" alt-text="Screenshot that shows the Log stream page for an Azure function." lightbox="./media/mqtt-routing-to-azure-functions-portal/function-log-stream.png"::: |
| 128 | + |
| 129 | +## Next step |
| 130 | + |
| 131 | +> [!div class="nextstepaction"] |
| 132 | +> See code samples in [this GitHub repository](https://github.com/Azure-Samples/MqttApplicationSamples/tree/main). |
| 133 | +
|
0 commit comments