Skip to content

Commit bef3f01

Browse files
committed
CLI tutorial - custom topics and Functions
1 parent efad980 commit bef3f01

6 files changed

+142
-90
lines changed

articles/event-grid/.openpublishing.redirection.event-grid.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919
"source_path": "mqtt-routing-to-event-hubs-portal.md",
2020
"redirect_url": "/azure/event-grid/mqtt-routing-to-azure-functions-portal",
2121
"redirect_document_id": false
22+
},
23+
{
24+
"source_path": "mqtt-routing-to-event-hubs-cli.md",
25+
"redirect_url": "/azure/event-grid/mqtt-routing-to-azure-functions-cli",
26+
"redirect_document_id": false
2227
},
2328
{
2429
"source_path": "network-security-mqtt.md",

articles/event-grid/mqtt-publish-and-subscribe-cli.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,5 +165,6 @@ You can replicate and modify the same code for multiple clients to publish and s
165165

166166
## Next steps
167167

168-
- [Route MQTT messages to Event Hubs](mqtt-routing-to-event-hubs-cli.md).
168+
- [Tutorial: Route MQTT messages to Azure Event Hubs using namespace topics](mqtt-routing-to-event-hubs-portal-namespace-topics.md)
169+
- [Tutorial: Route MQTT messages to Azure Functions using custom topics](mqtt-routing-to-azure-functions-portal.md)
169170
- For code samples, go to [this GitHub repository](https://github.com/Azure-Samples/MqttApplicationSamples/tree/main).
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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+

articles/event-grid/mqtt-routing-to-azure-functions-portal.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: 'Tutorial: Route MQTT messages to Azure Functions - portal'
3-
description: 'Tutorial: Use custom topics in Azure Event Grid to route MQTT messages to Azure Event Hubs by using the Routing feature for Event Grid namespaces.'
3+
description: 'Use custom topics in Azure Event Grid to route MQTT messages to Azure Functions by using the Routing feature. You use the Azure portal to do this tutorial.'
44
ms.topic: tutorial
55
ms.date: 03/13/2024
66
author: george-guirguis

articles/event-grid/mqtt-routing-to-event-hubs-cli.md

Lines changed: 0 additions & 85 deletions
This file was deleted.

articles/event-grid/toc.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,8 @@ items:
3535
items:
3636
- name: Azure portal
3737
href: mqtt-routing-to-azure-functions-portal.md
38-
- name: Route MQTT messages to Azure Event Hubs using custom topics
39-
items:
4038
- name: Azure CLI
41-
href: mqtt-routing-to-event-hubs-cli.md
39+
href: mqtt-routing-to-azure-functions-cli.md
4240
- name: Samples
4341
href: https://github.com/Azure-Samples/MqttApplicationSamples
4442
- name: Concepts

0 commit comments

Comments
 (0)