Skip to content

Commit 054c7ee

Browse files
authored
Merge pull request #262343 from kgremban/jan3-armreadwrite
Rewrite bicep/arm quickstarts to use cli simulated device
2 parents 10fc071 + a1cdd48 commit 054c7ee

File tree

2 files changed

+100
-148
lines changed

2 files changed

+100
-148
lines changed

articles/iot-hub/horizontal-arm-route-messages.md

Lines changed: 46 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,121 +1,99 @@
11
---
2-
title: Quickstart - route messages to storage (ARM)
2+
title: Quickstart - route messages to storage (ARM template)
33
titleSuffix: Azure IoT Hub
44
description: Learn how to use an ARM template to publish Azure IoT Hub, storage account, route messages in this quickstart
55
author: kgremban
66

77
ms.author: kgremban
88
ms.service: iot-hub
99
ms.topic: quickstart-arm
10-
ms.date: 08/24/2020
10+
ms.date: 01/04/2024
1111
ms.custom: mvc, subject-armqs, mode-arm, devx-track-arm-template
1212
---
1313

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

16-
In this quickstart, you use an Azure Resource Manager template (ARM template) 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 an Azure Resource Manager template (ARM template) 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 Azure Resource Manager](../../includes/resource-manager-quickstart-introduction.md)]
1919

20-
If your environment meets the prerequisites and you're familiar with using ARM templates, select the **Deploy to Azure** button. The template will open in the Azure portal.
20+
If your environment meets the prerequisites and you're familiar with using ARM templates, select the **Deploy to Azure** button. The template opens in the Azure portal.
2121

2222
[![Deploy To Azure](https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/1-CONTRIBUTION-GUIDE/images/deploytoazure.svg?sanitize=true)](https://portal.azure.com/#create/Microsoft.Template/uri/https%3A%2F%2Fraw.githubusercontent.com%2FAzure%2Fazure-quickstart-templates%2Fmaster%2Fquickstarts%2Fmicrosoft.devices%2Fiothub-auto-route-messages%2Fazuredeploy.json)
2323

2424
## Prerequisites
2525

26-
- If you don't have an Azure subscription, create a [free Azure account](https://azure.microsoft.com/free/) before you begin.
27-
28-
- The sample application you run in this quickstart is written using C#. You need the .NET SDK 6.0 or greater on your development machine.
29-
30-
You can download the .NET Core SDK for multiple platforms from [.NET](https://dotnet.microsoft.com/download).
31-
32-
You can verify the current version of C# on your development machine using the following command:
33-
34-
```cmd/sh
35-
dotnet --version
36-
```
37-
38-
- Download and unzip the [IoT C# SDK](https://github.com/Azure/azure-iot-sdk-csharp/archive/main.zip).
26+
* If you don't have an Azure subscription, create a [free Azure account](https://azure.microsoft.com/free/) before you begin.
3927

4028
## Review the template
4129

4230
The template 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).
4331

44-
:::code language="json" source="~/quickstart-templates/quickstarts/microsoft.devices/iothub-auto-route-messages/azuredeploy.json":::
45-
4632
Two Azure resources are defined in the template:
4733

48-
- [Microsoft.Storage/storageAccounts](/azure/templates/microsoft.storage/storageaccounts)
49-
- [Microsoft.Devices/IotHubs](/azure/templates/microsoft.devices/iothubs)
50-
51-
## Deploy the template and run the sample app
52-
53-
This section provides the steps to deploy the template, create a virtual device, and run the arm-read-write application to send the messages.
34+
* [Microsoft.Storage/storageAccounts](/azure/templates/microsoft.storage/storageaccounts): A storage account with a container.
35+
* [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.
5436

55-
1. Create the resources by deploying the ARM template.
56-
57-
> [!TIP]
58-
> Select the button below to start the deployment of the template. While it's running, set up the arm-read-write application to run.
37+
:::code language="json" source="~/quickstart-templates/quickstarts/microsoft.devices/iothub-auto-route-messages/azuredeploy.json":::
5938

60-
[![Deploy To Azure](https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/1-CONTRIBUTION-GUIDE/images/deploytoazure.svg?sanitize=true)](https://portal.azure.com/#create/Microsoft.Template/uri/https%3A%2F%2Fraw.githubusercontent.com%2FAzure%2Fazure-quickstart-templates%2Fmaster%2Fquickstarts%2Fmicrosoft.devices%2Fiothub-auto-route-messages%2Fazuredeploy.json)
39+
## Deploy the template
6140

62-
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.
41+
This section provides the steps to deploy the ARM template.
6342

64-
![Select the resource group](./media/horizontal-arm-route-messages/01-select-resource-group.png)
43+
- Create the resources by deploying the ARM template.
6544

66-
1. You see the IoT Hub and storage account that were created when you deployed the ARM template. Wait until the template is fully deployed before continuing. Then select your resource group to see your resources.
45+
[![Deploy To Azure](https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/1-CONTRIBUTION-GUIDE/images/deploytoazure.svg?sanitize=true)](https://portal.azure.com/#create/Microsoft.Template/uri/https%3A%2F%2Fraw.githubusercontent.com%2FAzure%2Fazure-quickstart-templates%2Fmaster%2Fquickstarts%2Fmicrosoft.devices%2Fiothub-auto-route-messages%2Fazuredeploy.json)
6746

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

70-
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.
49+
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 template 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.
7150

72-
Substitute the hub name in this command where noted, and execute this command in the command window:
51+
>[!TIP]
52+
>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.
7353
74-
```cmd
75-
SET IOT_HUB_URI=<hub name goes here>.azure-devices-net;
76-
```
54+
1. Retrieve the name of the IoT hub that the template created for you.
7755

78-
which will look this example:
56+
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.
7957

80-
```cmd
81-
SET IOT_HUB_URI=ContosoTestHubdlxlud5h.azure-devices-net;
58+
```azurecli
59+
az iot hub list --resource-group ContosoResourceGrp --output table
8260
```
8361

84-
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.
85-
86-
:::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":::
87-
88-
1. On the right side of the screen, select **+ Add Device** to add a new device.
62+
1. Copy the name of your IoT hub from the output. It should be formatted like `contosoHub{randomidentifier}`
8963

90-
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:
64+
1. Add a device to the hub.
9165

92-
```cmd
93-
SET IOT_DEVICE_KEY=<device-key-goes-here>
66+
```azurecli
67+
az iot hub device-identity create --device-id contosoDevice --hub-name {YourIoTHubName}
9468
```
9569

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

98-
```cmd
99-
SET IOT_DEVICE_ID=<device-id-goes-here>
100-
```
101-
102-
which will look like this example:
72+
The `--data` parameter lets us set the message body.
10373

104-
```cmd
105-
SET IOT_DEVICE_ID=Contoso-Test-Device
74+
```azurecli
75+
az iot device simulate \
76+
--device-id contosoDevice \
77+
--hub-name {YourIoTHubName} \
78+
--data "This message won't be routed."
10679
```
10780

108-
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**.
81+
The simulator sends 100 messages and then disconnects. You don't need to wait for all 100 for the purposes of this quickstart.
10982

110-
![See environment variables](./media/horizontal-arm-route-messages/06-environment-variables.png)
83+
>[!TIP]
84+
>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.
11185
112-
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.
86+
1. Send device-to-cloud messages to be routed to storage.
11387

114-
1. To run the application, type the following command in the command window and press **Enter**.
88+
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`.
11589

116-
`dotnet run arm-read-write`
117-
118-
The application generates and displays messages on the console as it sends each message to the IoT hub. The hub was configured in the ARM template 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.
90+
```azurecli
91+
az iot device simulate \
92+
--device-id contosoDevice \
93+
--hub-name {YourIoTHubName} \
94+
--properties level=storage \
95+
--data "This message will be routed to storage."
96+
```
11997

12098
## Review deployed resources
12199

@@ -125,7 +103,7 @@ This section provides the steps to deploy the template, create a virtual device,
125103

126104
![Look at the storage account files](./media/horizontal-arm-route-messages/07-see-storage.png)
127105

128-
1. Select one of the files and select **Download** and download the file to a location you can find later. It will have a name that's numeric, like 47. Add _.txt_ to the end and then double-click on the file to open it.
106+
1. Select one of the files and select **Download** and download the file to a location you can find later. It has a name that's numeric, like 47. Add _.txt_ to the end and then double-click on the file to open it.
129107

130108
1. When you open the file, each row is for a different message; the body of each message is also encrypted. It must be in order for you to perform queries against the body of the message.
131109

@@ -134,11 +112,11 @@ This section provides the steps to deploy the template, create a virtual device,
134112
> [!NOTE]
135113
> These messages are encoded in UTF-32 and base64. If you read the message back, you have to decode it from base64 and utf-32 in order to read it as ASCII. If you're interested, you can use the method ReadOneRowFromFile in the Routing Tutorial to read one for from one of these message files and decode it into ASCII. ReadOneRowFromFile is in the IoT C# SDK repository that you unzipped for this quickstart. Here is the path from the top of that folder: *./iothub/device/samples/getting started/RoutingTutorial/SimulatedDevice/Program.cs.* Set the boolean `readTheFile` to true, and hardcode the path to the file on disk, and it will open and translate the first row in the file.
136114
137-
You have deployed an ARM template to create an IoT hub and a storage account, and run a program to send messages to the hub. The messages are then automatically stored in the storage account where they can be viewed.
115+
In this quickstart, you deployed an ARM template to create an IoT hub and a storage account, then run a program to send messages to the hub. The messages are routed based on their message properties and stored in the storage account where they can be viewed.
138116

139117
## Clean up resources
140118

141-
To remove the resources added during this quickstart, sign in to the [Azure portal](https://portal.azure.com). Select **Resource Groups**, then find the resource group you used for this quickstart. Select the resource group and then select *Delete*. It will delete all of the resources in the group.
119+
To remove the resources added during this quickstart, sign in to the [Azure portal](https://portal.azure.com). Select **Resource Groups**, then find the resource group you used for this quickstart. Select the resource group and then select *Delete*. When the group is deleted, so are all of the resources in the group.
142120

143121
## Next steps
144122

0 commit comments

Comments
 (0)