Skip to content

Commit c58712f

Browse files
committed
ready to go live
1 parent d9a78e6 commit c58712f

File tree

1 file changed

+41
-7
lines changed

1 file changed

+41
-7
lines changed

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

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ ms.subservice: mqtt
1010

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

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.
14-
15-
In this tutorial, you perform the following tasks:
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:
1614

1715
> [!div class="checklist"]
1816
> * Create an Azure function using Event Grid trigger
@@ -26,15 +24,50 @@ If you don't have an Azure subscription, you can sign up for a [free trial](http
2624

2725
## Create an Azure function using Event Grid trigger
2826

29-
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**.
27+
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:
28+
29+
```csharp
30+
using System;
31+
using Azure.Messaging;
32+
using Microsoft.Azure.Functions.Worker;
33+
using Microsoft.Extensions.Logging;
34+
35+
namespace Company.Function
36+
{
37+
public class MyEventGridTriggerFunc
38+
{
39+
private readonly ILogger<MyEventGridTriggerFunc> _logger;
40+
41+
public MyEventGridTriggerFunc(ILogger<MyEventGridTriggerFunc> logger)
42+
{
43+
_logger = logger;
44+
}
45+
46+
[Function(nameof(MyEventGridTriggerFunc))]
47+
public void Run([EventGridTrigger] CloudEvent cloudEvent)
48+
{
49+
_logger.LogInformation("Event type: {type}, Event subject: {subject}", cloudEvent.Type, cloudEvent.Subject);
50+
}
51+
}
52+
}
53+
```
54+
55+
You use this Azure function as an event handler for a topic's subscription later in this tutorial.
56+
57+
> [!NOTE]
58+
> - Create all resources in the same region.
59+
> - This tutorial has been tested with an Azure function that uses .NET 8.0 (isolated) runtime stack.
3060
3161
## Create an Event Grid topic (custom topic)
3262
Create an Event Grid topic. See [Create a custom topic using the portal](/azure/event-grid/custom-event-quickstart-portal). When you create the Event Grid topic, on the **Advanced** tab, for **Event Schema**, select **Cloud Event Schema v1.0**.
3363

3464
:::image type="content" source="./media/mqtt-routing-to-azure-functions-portal/create-topic-cloud-event-schema.png" alt-text="Screenshot that shows the Advanced page of the Create Topic wizard.":::
3565

66+
> [!NOTE]
67+
> Use **Cloud event schema** everywhere in this tutorial.
3668
3769
## Add a subscription to the topic using the function
70+
In this step, you create a subscription to the Event Grid topic using the Azure function you created earlier.
3871

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

@@ -62,15 +95,15 @@ Create an Event Grid topic. See [Create a custom topic using the portal](/azure/
6295

6396
Follow instructions from [Quickstart: Publish and subscribe to MQTT messages using an Event Grid namespace with Azure portal](mqtt-publish-and-subscribe-portal.md) to:
6497

65-
1. Create an Event Grid namespace.1.
98+
1. Create an Event Grid namespace.
6699
1. Create two clients.
67100
1. Create a topic space.
68101
1. Create publisher and subscriber permission bindings.
69102
1. Test using [MQTTX app](https://mqttx.app/) to confirm that clients are able to send and receive messages.
70103

71104
## Enable managed identity for the namespace
72105

73-
In this section, you enable system-assigned managed identity for the Event Grid namespace, and then give the identity **send** access to the Event Grid custom topic you created earlier. You add the managed identity to the **Event Grid Data Sender** role on the custom topic.
106+
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.
74107

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

@@ -97,6 +130,7 @@ In this section, you enable system-assigned managed identity for the Event Grid
97130
1. On the **Review + assign** page, review settings, and then select **Review + assign** at the bottom of the page.
98131

99132
## Configure routing messages to Azure function via custom topic
133+
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.
100134

101135
1. On the **Event Grid Namespace** page, select **Routing** on the left navigation bar.
102136
1. On the **Routing** page, select **Enable routing**.
@@ -110,7 +144,7 @@ In this section, you enable system-assigned managed identity for the Event Grid
110144
## Send test MQTT messages using MQTTX
111145
Send test MQTT messages to the namespace and confirm that the function receives them.
112146

113-
Follow instructions in the [Publish, subscribe messages using MQTTX app](mqtt-publish-and-subscribe-portal.md#publishsubscribe-using-mqttx-app) section to send a few test messages to the Event Grid namespace.
147+
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.
114148

115149
Here's the flow of the events or messages:
116150

0 commit comments

Comments
 (0)