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
# Quickstart: Deploy an Azure IoT hub and a storage account using Bicep
15
15
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.
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).
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).
29
29
30
30
Two Azure resources are defined in the Bicep file:
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.
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.
58
40
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.
60
42
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.
62
44
63
-

45
+
```azurecli
46
+
az group create --name ContosoResourceGrp --location eastus
47
+
az deployment group create --resource-group exampleRG --template-file main.bicep
48
+
```
64
49
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.
66
51
67
-

52
+
## Send device-to-cloud messages
68
53
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.
70
55
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.
72
58
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.
76
60
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.
78
62
79
-
```cmd
80
-
SET IOT_HUB_URI=ContosoTestHubdlxlud5h.azure-devices-net;
63
+
```azurecli
64
+
az iot hub list --resource-group ContosoResourceGrp --output table
81
65
```
82
66
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`
88
68
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.
90
70
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}
93
73
```
94
74
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.
96
76
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.
102
78
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."
105
84
```
106
85
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.
>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.
110
90
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.
112
92
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`.
114
94
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
+
```
118
102
119
-
## Review deployed resources
103
+
## Review routed messages
120
104
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.
122
106
123
107
1. Drill down into the storage account until you find files.
0 commit comments