Skip to content

Commit 10aafea

Browse files
Merge pull request #267705 from PatAltimore/patricka-improve-mqtt-bridge-cmds
Improve MQTT event grid commands
2 parents 99b678a + 70a01f0 commit 10aafea

File tree

1 file changed

+69
-14
lines changed

1 file changed

+69
-14
lines changed

articles/iot-operations/connect-to-cloud/tutorial-connect-event-grid.md

Lines changed: 69 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ ms.subservice: mq
77
ms.custom: devx-track-azurecli
88
ms.author: patricka
99
ms.topic: tutorial
10-
ms.date: 11/15/2023
10+
ms.date: 02/28/2024
1111

1212
#CustomerIntent: As an operator, I want to configure IoT MQ to bridge to Azure Event Grid MQTT broker PaaS so that I can process my IoT data at the edge and in the cloud.
1313
---
@@ -22,12 +22,47 @@ In this tutorial, you learn how to configure IoT MQ for bi-directional MQTT brid
2222

2323
* [Deploy Azure IoT Operations](../get-started/quickstart-deploy.md)
2424

25+
## Set environment variables
26+
27+
28+
Sign in with Azure CLI:
29+
30+
```azurecli
31+
az login
32+
```
33+
34+
Set environment variables for the rest of the setup. Replace values in `<>` with valid values or names of your choice. A new Azure Event Grid namespace and topic space are created in your Azure subscription based on the names you provide:
35+
36+
```azurecli
37+
# For this tutorial, the steps assume the IoT Operations cluster and the Event Grid
38+
# are in the same subscription, resource group, and location.
39+
40+
# Name of the resource group of Azure Event Grid and IoT Operations cluster
41+
export RESOURCE_GROUP=<RESOURCE_GROUP_NAME>
42+
43+
# Azure region of Azure Event Grid and IoT Operations cluster
44+
export LOCATION=<LOCATION>
45+
46+
# Name of the Azure Event Grid namespace
47+
export EVENT_GRID_NAMESPACE=<EVENT_GRID_NAMESPACE>
48+
49+
# Name of the Arc-enabled IoT Operations cluster
50+
export CLUSTER_NAME=<CLUSTER_NAME>
51+
52+
# Subscription ID of Azure Event Grid and IoT Operations cluster
53+
export SUBSCRIPTION_ID=<SUBSCRIPTION_ID>
54+
```
55+
2556
## Create Event Grid namespace with MQTT broker enabled
2657

27-
[Create Event Grid namespace](../../event-grid/create-view-manage-namespaces.md) with Azure CLI. Replace `<EG_NAME>`, `<RESOURCE_GROUP>`, and `<LOCATION>` with your own values. The location should be the same as the one you used to deploy Azure IoT Operations.
58+
[Create Event Grid namespace](../../event-grid/create-view-manage-namespaces.md) with Azure CLI. The location should be the same as the one you used to deploy Azure IoT Operations.
2859

2960
```azurecli
30-
az eventgrid namespace create -n <EG_NAME> -g <RESOURCE_GROUP> --location <LOCATION> --topic-spaces-configuration "{state:Enabled,maximumClientSessionsPerAuthenticationName:3}"
61+
az eventgrid namespace create \
62+
--namespace-name $EVENT_GRID_NAMESPACE \
63+
--resource-group $RESOURCE_GROUP \
64+
--location $LOCATION \
65+
--topic-spaces-configuration "{state:Enabled,maximumClientSessionsPerAuthenticationName:3}"
3166
```
3267

3368
By setting the `topic-spaces-configuration`, this command creates a namespace with:
@@ -39,20 +74,30 @@ The max client sessions option allows IoT MQ to spawn multiple instances and sti
3974

4075
## Create a topic space
4176

42-
In the Event Grid namespace, create a topic space named `tutorial` with a topic template `telemetry/#`. Replace `<EG_NAME>` and `<RESOURCE_GROUP>` with your own values.
77+
In the Event Grid namespace, create a topic space named `tutorial` with a topic template `telemetry/#`.
4378

4479
```azurecli
45-
az eventgrid namespace topic-space create -g <RESOURCE_GROUP> --namespace-name <EG_NAME> --name tutorial --topic-templates "telemetry/#"
80+
az eventgrid namespace topic-space create \
81+
--resource-group $RESOURCE_GROUP \
82+
--namespace-name $EVENT_GRID_NAMESPACE \
83+
--name tutorial \
84+
--topic-templates "telemetry/#"
4685
```
4786

4887
By using the `#` wildcard in the topic template, you can publish to any topic under the `telemetry` topic space. For example, `telemetry/temperature` or `telemetry/humidity`.
4988

5089
## Give IoT MQ access to the Event Grid topic space
5190

52-
Using `az k8s-extension show`, find the principal ID for the Azure IoT MQ Arc extension.
91+
Using `az k8s-extension show`, find the principal ID for the Azure IoT MQ Arc extension. The command stores the principal ID in a variable for later use.
5392

5493
```azurecli
55-
az k8s-extension show --resource-group <RESOURCE_GROUP> --cluster-name <CLUSTER_NAME> --name mq --cluster-type connectedClusters --query identity.principalId -o tsv
94+
export PRINCIPAL_ID=$(az k8s-extension show \
95+
--resource-group $RESOURCE_GROUP \
96+
--cluster-name $CLUSTER_NAME \
97+
--name mq \
98+
--cluster-type connectedClusters \
99+
--query identity.principalId -o tsv)
100+
echo $PRINCIPAL_ID
56101
```
57102

58103
Take note of the output value for `identity.principalId`, which is a GUID value with the following format:
@@ -61,29 +106,39 @@ Take note of the output value for `identity.principalId`, which is a GUID value
61106
d84481ae-9181-xxxx-xxxx-xxxxxxxxxxxx
62107
```
63108

64-
Then, use Azure CLI to assign publisher and subscriber roles to IoT MQ for the topic space you created. Replace `<MQ_ID>` with the principal ID you found in the previous step, and replace `<SUBSCRIPTION_ID>`, `<RESOURCE_GROUP>`, `<EG_NAME>` with your values matching the Event Grid namespace you created.
109+
Then, use Azure CLI to assign publisher and subscriber roles to IoT MQ for the topic space you created.
65110

66-
Assigning the publisher role:
111+
Assign the publisher role:
67112

68113
```azurecli
69-
az role assignment create --assignee <MQ_ID> --role "EventGrid TopicSpaces Publisher" --scope /subscriptions/<SUBSCRIPTION_ID>/resourceGroups/<RESOURCE_GROUP>/providers/Microsoft.EventGrid/namespaces/<EG_NAME>/topicSpaces/tutorial
114+
az role assignment create \
115+
--assignee $PRINCIPAL_ID \
116+
--role "EventGrid TopicSpaces Publisher" \
117+
--scope /subscriptions/$SUBSCRIPTION_ID/resourceGroups/$RESOURCE_GROUP/providers/Microsoft.EventGrid/namespaces/$EVENT_GRID_NAMESPACE/topicSpaces/tutorial
70118
```
71119

72-
Assigning the subscriber role:
120+
Assign the subscriber role:
73121

74122
```azurecli
75-
az role assignment create --assignee <MQ_ID> --role "EventGrid TopicSpaces Subscriber" --scope /subscriptions/<SUBSCRIPTION_ID>/resourceGroups/<RESOURCE_GROUP>/providers/Microsoft.EventGrid/namespaces/<EG_NAME>/topicSpaces/tutorial
123+
az role assignment create \
124+
--assignee $PRINCIPAL_ID \
125+
--role "EventGrid TopicSpaces Subscriber" \
126+
--scope /subscriptions/$SUBSCRIPTION_ID/resourceGroups/$RESOURCE_GROUP/providers/Microsoft.EventGrid/namespaces/$EVENT_GRID_NAMESPACE/topicSpaces/tutorial
76127
```
77128

78129
> [!TIP]
79130
> The scope matches the `id` of the topic space you created with `az eventgrid namespace topic-space create` in the previous step, and you can find it in the output of the command.
80131
81132
## Event Grid MQTT broker hostname
82133

83-
Use Azure CLI to get the Event Grid MQTT broker hostname. Replace `<EG_NAME>` and `<RESOURCE_GROUP>` with your own values.
134+
Use Azure CLI to get the Event Grid MQTT broker hostname.
84135

85136
```azurecli
86-
az eventgrid namespace show -g <RESOURCE_GROUP> -n <EG_NAME> --query topicSpacesConfiguration.hostname -o tsv
137+
az eventgrid namespace show \
138+
--resource-group $RESOURCE_GROUP \
139+
--namespace-name $EVENT_GRID_NAMESPACE \
140+
--query topicSpacesConfiguration.hostname \
141+
-o tsv
87142
```
88143

89144
Take note of the output value for `topicSpacesConfiguration.hostname` that is a hostname value that looks like:

0 commit comments

Comments
 (0)