You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/event-grid/mqtt-routing-to-azure-functions-cli.md
+45-3Lines changed: 45 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -51,20 +51,48 @@ You use this Azure function as an event handler for a topic's subscription later
51
51
52
52
## Create an Event Grid topic (custom topic)
53
53
54
+
In this step, you create an Event Grid topic.
55
+
56
+
1. Copy and paste the script to an editor.
57
+
1. Replace the following values.
58
+
1. Select **Open Cloud Shell**.
59
+
1. Switch from **PowerShell** to **Bash** (in the upper-left corner of the Cloud Shell window).
60
+
1. Copy and paste the script from the editor to Cloud Shell and run the script.
61
+
62
+
The script creates an Azure resource group and an Event Grid custom topic in it. Later in this tutorial, you configure routing for an Event Grid namespace so that the events or messages sent to the namespace are routed to the custom topic and then to the Azure function via subscription to the topic.
63
+
64
+
| Placeholder | Description |
65
+
| ----------- | ----------- |
66
+
|`RESOURCEGROUPNAME`| Name of the resource group to be created. |
67
+
|`REGION`| Region in which you want to create the resource group and the custom topic. |
68
+
|`TOPICNAME`| Name of the custom topic to be created. |
69
+
70
+
The script uses the [`az eventgrid topic create`](/cli/azure/eventgrid/topic#az-eventgrid-topic-create) command to create an Event Grid topic or custom topic. The schema type is specified as the cloud event schema.
> Use **Cloud event schema** everywhere in this tutorial.
65
84
66
85
## 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.
86
+
87
+
In this step, you create a subscription to the custom topic using the Azure function you created earlier.
88
+
89
+
Replace the following values and run the script in the Cloud Shell. The script uses the [`az eventgrid event-subscription create`](/cli/azure/eventgrid/event-subscription#az-eventgrid-event-subscription-create) command to create an Azure function subscription to the custom topic. In the command, source ID is the topic's resource ID and endpoint is the function's resource ID. The endpoint type is set to Azure function and event delivery schema is specified as the cloud event schema.
90
+
91
+
| Placeholder | Description |
92
+
| ----------- | ----------- |
93
+
|`FUNCTIONRESOURCEGROUP`| Name of the resource group that has the Azure Functions app. |
94
+
|`FUNCTIONSAPPNAME`| Name of the Azure Functions app. |
95
+
|`FUNCTIONNAME`| Name of the Azure function. |
68
96
69
97
```azurecli-interactive
70
98
funcAppRgName="FUNCTIONRESOURCEGROUP"
@@ -90,28 +118,42 @@ Follow instructions from [Quickstart: Publish and subscribe to MQTT messages on
90
118
91
119
## Enable managed identity for the namespace
92
120
93
-
Enable system-assigned managed identity for the Event Grid namespace.
121
+
Replace the following value and run the script to enable system-assigned managed identity for the Event Grid namespace.
122
+
123
+
| Placeholder | Description |
124
+
| ----------- | ----------- |
125
+
|`EVENTGRIDNAMESPACENAME`| Name of the Event Grid namespace. |
126
+
127
+
The script uses the [`az eventgrid namespace update`](/cli/azure/eventgrid/namespace#az-eventgrid-namespace-update) command with `identity` set to `SystemAssigned` identity.
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.
135
+
Then, grant namespace's managed identity the **send** permission on the Event Grid custom topic you created earlier so that the namespace can send or route messages to the custom topic. You do so by adding the managed identity to the **Event Grid Data Sender** role on the custom topic.
136
+
101
137
102
138
```azurecli-interactive
103
139
egNamespaceServicePrincipalObjectID=$(az ad sp list --display-name $nsName --query [].id -o tsv)
104
140
topicResourceId=$(az eventgrid topic show --name $topicName -g $rgName --query id --output tsv)
105
141
az role assignment create --assignee $egNamespaceServicePrincipalObjectID --role "EventGrid Data Sender" --scope $topicResourceId
106
142
```
107
143
144
+
The script uses the [`az role assignment create`](/cli/azure/role/assignment#az-role-assignment-create) command with the IDs of namespace's managed identity and the custom topic, and assigns **Event Grid Data Sender** role to the namespace's managed identity on the custom topic.
145
+
146
+
108
147
## Configure routing messages to Azure function via custom topic
148
+
109
149
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
150
111
151
```azurecli-interactive
112
152
az eventgrid namespace update -g $rgName -n $nsName --topic-spaces-configuration "{state:Enabled,'routeTopicResourceId':$topicResourceId,'routingIdentityInfo':{type:SystemAssigned}}"
113
153
```
114
154
155
+
The script uses the [`az eventgrid namespace update`](/cli/azure/eventgrid/namespace#az-eventgrid-namespace-update) command to set the routing topic and the type of managed identity to use to route events to the topic.
156
+
115
157
## Send test MQTT messages using MQTTX
116
158
Send test MQTT messages to the namespace and confirm that the function receives them.
0 commit comments