Skip to content

Commit 93a63ed

Browse files
committed
Rewrite bicep/arm quickstarts to use cli simulated device
1 parent ff2edd4 commit 93a63ed

File tree

1 file changed

+50
-66
lines changed

1 file changed

+50
-66
lines changed

articles/iot-hub/quickstart-bicep-route-messages.md

Lines changed: 50 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -13,112 +13,96 @@ ms.custom: mvc, subject-armqs, mode-arm, devx-track-bicep
1313

1414
# Quickstart: Deploy an Azure IoT hub and a storage account using Bicep
1515

16-
In this quickstart, you use Bicep to create an IoT hub that will route messages to Azure Storage and a storage account to hold the messages. After manually adding a virtual IoT device to the hub to submit the messages, you configure that connection information in an application called *arm-read-write* to submit messages from the device to the hub. The hub is configured so the messages sent to the hub are automatically routed to the storage account. At the end of this quickstart, you can open the storage account and see the messages sent.
16+
In this quickstart, you use Bicep to create an IoT hub, an Azure Storage account, and a route to send messages from the IoT hub to storage. The hub is configured so the messages sent to the hub are automatically routed to the storage account if they meet the routing condition. At the end of this quickstart, you can open the storage account and see the messages sent.
1717

1818
[!INCLUDE [About Bicep](../../includes/resource-manager-quickstart-bicep-introduction.md)]
1919

2020
## Prerequisites
2121

22-
If you don't have an Azure subscription, create a [free Azure account](https://azure.microsoft.com/free/) before you begin.
22+
* If you don't have an Azure subscription, create a [free Azure account](https://azure.microsoft.com/free/) before you begin.
2323

24-
## Review the Bicep file
24+
* [Install Azure CLI](/cli/azure/install-azure-cli)
2525

26-
The Bicep file used in this quickstart is called `101-iothub-auto-route-messages` from [Azure Quickstart Templates](https://azure.microsoft.com/resources/templates/iothub-auto-route-messages).
26+
## Review the Bicep file
2727

28-
:::code language="bicep" source="~/quickstart-templates/quickstarts/microsoft.devices/iothub-auto-route-messages/main.bicep":::
28+
The Bicep file used in this quickstart is called `101-iothub-auto-route-messages` from [Azure quickstart templates](https://azure.microsoft.com/resources/templates/iothub-auto-route-messages).
2929

3030
Two Azure resources are defined in the Bicep file:
3131

32-
- [Microsoft.Storage/storageAccounts](/azure/templates/microsoft.storage/storageaccounts)
33-
- [Microsoft.Devices/IotHubs](/azure/templates/microsoft.devices/iothubs)
34-
35-
## Deploy the Bicep file and run the sample app
36-
37-
This section provides the steps to deploy the Bicep file, create a virtual device, and run the arm-read-write application to send the messages.
38-
39-
1. Create the resources by deploying the Bicep file using Azure CLI or Azure PowerShell.
40-
41-
# [CLI](#tab/CLI)
42-
43-
```azurecli
44-
az group create --name ContosoResourceGrp --location eastus
45-
az deployment group create --resource-group exampleRG --template-file main.bicep
46-
```
47-
48-
# [PowerShell](#tab/PowerShell)
32+
* [Microsoft.Storage/storageAccounts](/azure/templates/microsoft.storage/storageaccounts): A storage account with a container.
33+
* [Microsoft.Devices/IotHubs](/azure/templates/microsoft.devices/iothubs): An IoT hub with an endpoint that points to the storage container and a route to send filtered messages to that endpoint.
4934

50-
```azurepowershell
51-
New-AzResourceGroup -Name ContosoResourceGrp -Location eastus
52-
New-AzResourceGroupDeployment -ResourceGroupName exampleRG -TemplateFile ./main.bicep
53-
```
35+
:::code language="bicep" source="~/quickstart-templates/quickstarts/microsoft.devices/iothub-auto-route-messages/main.bicep":::
5436

55-
---
37+
## Deploy the Bicep file
5638

57-
When the deployment finishes, you should see a message indicating the deployment succeeded.
39+
This section provides the steps to deploy the Bicep file, create a virtual device, and run the arm-read-write application to send the messages.
5840

59-
1. Download and unzip the [IoT C# SDK](https://github.com/Azure/azure-iot-sdk-csharp/archive/main.zip).
41+
1. Download the [main.bicep](https://github.com/Azure/azure-quickstart-templates/blob/master/quickstarts/microsoft.devices/iothub-auto-route-messages/main.bicep) file from the Azure quickstart templates repo.
6042

61-
1. Open a command window and go to the folder where you unzipped the IoT C# SDK. Find the folder with the arm-read-write.csproj file. You create the environment variables in this command window. Sign in to the [Azure portal](https://portal.azure.com) to get the keys. Select **Resource Groups** then select the resource group used for this quickstart.
43+
1. Create the resources by deploying the Bicep file using Azure CLI.
6244

63-
![Select the resource group](./media/horizontal-arm-route-messages/01-select-resource-group.png)
45+
```azurecli
46+
az group create --name ContosoResourceGrp --location eastus
47+
az deployment group create --resource-group exampleRG --template-file main.bicep
48+
```
6449

65-
1. You see the IoT Hub and storage account that were created when you deployed the Bicep file. Wait until the file is fully deployed before continuing. Then select your resource group to see your resources.
50+
The deployment takes several minutes to complete. When the deployment finishes, you should see output detailing the deployed resources.
6651

67-
![View resources in the resource group](./media/horizontal-arm-route-messages/02-view-resources-in-group.png)
52+
## Send device-to-cloud messages
6853

69-
1. You need the **hub name**. Select the hub in the list of resources. Copy the name of the hub from the top of the IoT Hub section to the Windows clipboard.
54+
In this section, you register a device in your new IoT hub and then send messages from that device to IoT Hub. The route that the Bicep file configured in the IoT hub only sends messages to storage if they contain the message property `level=storage`. To test that this routing condition works as expected. we'll send some messages with that property and some without.
7055

71-
Substitute the hub name in this command where noted, and execute this command in the command window:
56+
>[!TIP]
57+
>This quickstart uses the Azure CLI simulated device for convenience. For a code example of sending device-to-cloud messages with message properties for routing, see [HubRoutingSample](https://github.com/Azure/azure-iot-sdk-csharp/tree/main/iothub/device/samples/how%20to%20guides/HubRoutingSample) in the Azure IoT SDK for .NET.
7258
73-
```cmd
74-
SET IOT_HUB_URI=<hub name goes here>.azure-devices-net;
75-
```
59+
1. Retrieve the name of the IoT hub that the template created for you.
7660

77-
which will look this example:
61+
If you used the default commands in the previous section, your resources were created in the **ContosoResourceGrp** resource group. If you used a different resource group, update the following command to match.
7862

79-
```cmd
80-
SET IOT_HUB_URI=ContosoTestHubdlxlud5h.azure-devices-net;
63+
```azurecli
64+
az iot hub list --resource-group ContosoResourceGrp --output table
8165
```
8266

83-
1. The next environment variable is the IoT Device Key. Add a new device to the hub by selecting **Devices** from the IoT Hub menu for the hub.
84-
85-
:::image type="content" source="./media/horizontal-arm-route-messages/04-select-iot-devices.png" alt-text="Screenshot that shows devices in the left pane." border="true":::
86-
87-
1. On the right side of the screen, select **+ Add Device** to add a new device.
67+
1. Copy the **Name** of your IoT hub from the output. It should be formatted like `contosoHubxxxxxxxxxxxxx`
8868

89-
Fill in the new device name. This quickstart uses a name starting with **Contoso-Test-Device**. Save the device and then open that screen again to retrieve the device key. (The key is generated for you when you close the pane.) Select either the primary or secondary key and copy it to the Windows clipboard. In the command window, set the command to execute and then press **Enter**. The command should look like this one but with the device key pasted in:
69+
1. Add a device to the hub.
9070

91-
```cmd
92-
SET IOT_DEVICE_KEY=<device-key-goes-here>
71+
```azurecli
72+
az iot hub device-identity create --device-id contosoDevice --hub-name {YourIoTHubName}
9373
```
9474

95-
1. The last environment variable is the **Device ID**. In the command window, set up the command and execute it.
75+
1. Simulate the device and send device-to-cloud messages.
9676

97-
```cmd
98-
SET IOT_DEVICE_ID=<device-id-goes-here>
99-
```
100-
101-
which will look like this example:
77+
The `--data` parameter determines the message body.
10278

103-
```cmd
104-
SET IOT_DEVICE_ID=Contoso-Test-Device
79+
```azurecli
80+
az iot device simulate \
81+
--device-id contosoDevice \
82+
--hub-name {YourIoTHubName} \
83+
--data "This message won't be routed."
10584
```
10685

107-
1. To see the environment variables you've defined, type SET on the command line and press **Enter**, then look for the ones starting with **IoT**.
86+
The simulator sends 100 messages and then disconnects. You don't need to wait for all 100 for the purposes of this quickstart.
10887

109-
![See environment variables](./media/horizontal-arm-route-messages/06-environment-variables.png)
88+
>[!TIP]
89+
>The Azure CLI won't print the messages as it sends them. If you want to watch the messages as the arrive at your hub, you can install the [Azure IoT Hub extension for Visual Studio Code](./reference-iot-hub-extension.md) and use it to monitor the built-in endpoint.
11090
111-
Now the environment variables are set, run the application from the same command window. Because you're using the same window, the variables will be accessible in memory when you run the application.
91+
1. Send device-to-cloud messages to be routed to storage.
11292

113-
1. To run the application, type the following command in the command window and press **Enter**.
93+
The `--properties` parameter allows us to add message, application, or system properties to the default message. For this quickstart, the route in your IoT hub is looking for messages that contain the message property `level=storage`.
11494

115-
`dotnet run arm-read-write`
116-
117-
The application generates and displays messages on the console as it sends each message to the IoT hub. The hub was configured in the Bicep file to have automated routing. Messages containing the text `level = storage` are automatically routed to the storage account. Let the app run for 10 to 15 minutes, then press **Enter** once or twice until it stops running.
95+
```azurecli
96+
az iot device simulate \
97+
--device-id contosoDevice \
98+
--hub-name {YourIoTHubName} \
99+
--properties level=storage \
100+
--data "This message will be routed to storage."
101+
```
118102

119-
## Review deployed resources
103+
## Review routed messages
120104

121-
1. Sign in to the [Azure portal](https://portal.azure.com) and select the Resource Group, then select the storage account.
105+
1. Sign in to the [Azure portal](https://portal.azure.com) and select the resource group, then select the storage account.
122106

123107
1. Drill down into the storage account until you find files.
124108

0 commit comments

Comments
 (0)