|
| 1 | +--- |
| 2 | +title: Azure IoT Hub module identity & module twin (Azure CLI) |
| 3 | +description: Learn how to create module identity and update module twins using Azure CLI. |
| 4 | +author: kgremban |
| 5 | +ms.author: kgremban |
| 6 | +ms.service: iot-hub |
| 7 | +services: iot-hub |
| 8 | +ms.date: 02/17/2023 |
| 9 | +ms.devlang: azurecli |
| 10 | +ms.topic: conceptual |
| 11 | +ms.custom: mqtt, devx-track-azurecli |
| 12 | +--- |
| 13 | + |
| 14 | +# Get started with IoT Hub module identity and module twins using Azure CLI |
| 15 | + |
| 16 | +[!INCLUDE [iot-hub-selector-module-twin-getstarted](../../includes/iot-hub-selector-module-twin-getstarted.md)] |
| 17 | + |
| 18 | +[Module identities and module twins](iot-hub-devguide-module-twins.md) are similar to Azure IoT Hub device identities and device twins, but provide finer granularity. While Azure IoT Hub device identities and device twins enable the back-end application to configure a device and provide visibility on the device's conditions, a module identity and module twin provide these capabilities at a finer granularity for individual components of a device. On capable devices with multiple components, such as operating system devices or firmware devices, module identities and module twins allow for isolated configuration and conditions for each component. |
| 19 | + |
| 20 | +[!INCLUDE [iot-hub-basic](../../includes/iot-hub-basic-whole.md)] |
| 21 | + |
| 22 | +This article shows you how to create an Azure CLI session in which you: |
| 23 | + |
| 24 | +* Create a device identity, then create a module identity for that device. |
| 25 | + |
| 26 | +* Update a set of desired properties for the module twin associated with the module identity. |
| 27 | + |
| 28 | +> [!NOTE] |
| 29 | +> For more information about the Azure IoT Hub SDKs you can use to create device and back-end apps for supporting similar tasks, see [Azure IoT Hub SDKs](iot-hub-devguide-sdks.md). |
| 30 | +
|
| 31 | +## Prerequisites |
| 32 | + |
| 33 | +* Azure CLI. You can also run the commands in this article using the [Azure Cloud Shell](/azure/cloud-shell/overview), an interactive CLI shell that runs in your browser or in an app such as Windows Terminal. If you use the Cloud Shell, you don't need to install anything. If you prefer to use the CLI locally, this article requires Azure CLI version 2.36 or later. Run `az --version` to find the version. To locally install or upgrade Azure CLI, see [Install Azure CLI](/cli/azure/install-azure-cli). |
| 34 | + |
| 35 | +* An IoT Hub. Create one with the [CLI](iot-hub-create-using-cli.md) or the [Azure portal](iot-hub-create-through-portal.md). |
| 36 | + |
| 37 | +* Make sure that port 8883 is open in your firewall. The device sample in this article uses MQTT protocol, which communicates over port 8883. This port may be blocked in some corporate and educational network environments. For more information and ways to work around this issue, see [Connecting to IoT Hub (MQTT)](iot-hub-mqtt-support.md#connecting-to-iot-hub). |
| 38 | + |
| 39 | +## Prepare the Cloud Shell |
| 40 | + |
| 41 | +If you want to use the Azure Cloud Shell, you must first launch and configure it. If you use the CLI locally, skip to the [Prepare two CLI sessions](#prepare-two-cli-sessions) section. |
| 42 | + |
| 43 | +1. Select the **Cloud Shell** icon from the page header in the Azure portal. |
| 44 | + |
| 45 | + :::image type="content" source="./media/quickstart-send-telemetry-cli/cloud-shell-button.png" alt-text="Screenshot of the global controls from the page header of the Azure portal, highlighting the Cloud Shell icon."::: |
| 46 | + |
| 47 | + > [!NOTE] |
| 48 | + > If this is the first time you've used the Cloud Shell, it prompts you to create storage, which is required to use the Cloud Shell. Select a subscription to create a storage account and Microsoft Azure Files share. |
| 49 | +
|
| 50 | +2. Use the environment selector in the Cloud Shell toolbar to select your preferred CLI environment. This article uses the **Bash** environment. You can also use the **PowerShell** environment. |
| 51 | + |
| 52 | + > [!NOTE] |
| 53 | + > Some commands require different syntax or formatting in the **Bash** and **PowerShell** environments. For more information, see [Tips for using the Azure CLI successfully](/cli/azure/use-cli-effectively?tabs=bash%2Cbash2). |
| 54 | +
|
| 55 | + :::image type="content" source="./media/quickstart-send-telemetry-cli/cloud-shell-environment.png" alt-text="Screenshot of an Azure Cloud Shell window, highlighting the environment selector in the toolbar."::: |
| 56 | + |
| 57 | +## Prepare a CLI session |
| 58 | + |
| 59 | +Next, you must prepare an Azure CLI sessions. If you're using the Cloud Shell, you run the session in a Cloud Shell tab. If using a local CLI client, you run the sessioin in a CLI instance. |
| 60 | +> [!NOTE] |
| 61 | +> Azure CLI requires you to be logged into your Azure account. If you're using the Cloud Shell, you're automatically logged into your Azure account. If you're using a local CLI client, you must log into each CLI session. All communication between your Azure CLI shell session and your IoT hub is authenticated and encrypted. As a result, this article doesn't need extra authentication that you'd use with a real device, such as a connection string. For more information about logging in with Azure CLI, see [Sign in with Azure CLI](/cli/azure/authenticate-azure-cli). |
| 62 | +
|
| 63 | +1. In the CLI session, run the [az extension add](/cli/azure/extension#az-extension-add) command. The command adds the Microsoft Azure IoT Extension for Azure CLI to your CLI shell. The extension adds IoT Hub, IoT Edge, and IoT Device Provisioning Service (DPS) specific commands to Azure CLI. After you install the extension, you don't need to install it again in any Cloud Shell session. |
| 64 | + |
| 65 | + ```azurecli |
| 66 | + az extension add --name azure-iot |
| 67 | + ``` |
| 68 | + |
| 69 | + [!INCLUDE [iot-hub-cli-version-info](../../includes/iot-hub-cli-version-info.md)] |
| 70 | + |
| 71 | + |
| 72 | +## Create a device identity and module identity |
| 73 | + |
| 74 | +In this section, you create a device identity for your IoT hub in the CLI session, and then create a module identity using that device identity. You can create up to 50 module identities under each device identity. |
| 75 | + |
| 76 | +To create a device identity and module identity: |
| 77 | + |
| 78 | +1. In the CLI session, run the [az iot hub device-identity create](/cli/azure/iot/hub/device-identity#az-iot-hub-device-identity-create) command, replacing the following placeholders with their corresponding values. This command creates the device identity for your module. |
| 79 | + |
| 80 | + *{DeviceName}*. The name of your device. |
| 81 | + |
| 82 | + *{HubName}*. The name of your IoT hub. |
| 83 | + |
| 84 | + ```azurecli |
| 85 | + az iot hub device-identity create --device-id {DeviceName} --hub-name {HubName} |
| 86 | + ``` |
| 87 | +
|
| 88 | +1. In the CLI session, run the [az iot hub module-identity create](/cli/azure/iot/hub/device-identity#az-iot-hub-module-identity-create) command, replacing the following placeholders with their corresponding values. This command creates the module identity for your module, under the device identity you created in the previous step. |
| 89 | +
|
| 90 | + *{DeviceName}*. The name of your device. |
| 91 | +
|
| 92 | + *{HubName}*. The name of your IoT hub. |
| 93 | +
|
| 94 | + *{ModuleName}*. The name of your module. |
| 95 | +
|
| 96 | + ```azurecli |
| 97 | + az iot hub device-identity create --device-id {DeviceName} --hub-name {HubName} --module-id {ModuleName} |
| 98 | + ``` |
| 99 | +
|
| 100 | +## Update the module twin |
| 101 | +
|
| 102 | +Once a module identity is created, a module twin is implicitly created in IoT Hub. In this section, you use the CLI session to update a set of desired properties for the module twin associated with the module identity you created in the previous section. |
| 103 | +
|
| 104 | +1. In the CLI session, run the [az iot hub module-twin update](/cli/azure/iot/hub#az-iot-hub-module-twin-update) command, replacing the following placeholders with their corresponding values. In this example, we're updating multiple desired properties. |
| 105 | +
|
| 106 | + *{DeviceName}*. The name of your device. |
| 107 | +
|
| 108 | + *{HubName}*. The name of your IoT hub. |
| 109 | +
|
| 110 | + *{ModuleName}*. The name of your module. |
| 111 | +
|
| 112 | + ```azurecli |
| 113 | + az iot hub module-twin update --device-id {DeviceName} --hub-name {HubName} \ |
| 114 | + --module-id {ModuleName} \ |
| 115 | + --desired '{"conditions":{"temperature":{"warning":75, "critical":100}}}' |
| 116 | + ``` |
| 117 | +
|
| 118 | +1. In the CLI session, confirm that the JSON response shows the results of the update operation. In the following JSON response example, we used `SampleDevice` and `SampleModule` for the `{DeviceName}` and `{ModuleName}` placeholders, respectively, in the `az iot hub module-twin update` CLI command. |
| 119 | +
|
| 120 | + ```json |
| 121 | + { |
| 122 | + "authenticationType": "sas", |
| 123 | + "capabilities": null, |
| 124 | + "cloudToDeviceMessageCount": 0, |
| 125 | + "connectionState": "Disconnected", |
| 126 | + "deviceEtag": "Mzg0OEN1NzW=", |
| 127 | + "deviceId": "SampleDevice", |
| 128 | + "deviceScope": null, |
| 129 | + "etag": "AAAAAAAAAAI=", |
| 130 | + "lastActivityTime": "0001-01-01T00:00:00+00:00", |
| 131 | + "modelId": "", |
| 132 | + "moduleId": "SampleModule", |
| 133 | + "parentScopes": null, |
| 134 | + "properties": { |
| 135 | + "desired": { |
| 136 | + "$metadata": { |
| 137 | + "$lastUpdated": "2023-02-17T21:26:10.5835633Z", |
| 138 | + "$lastUpdatedVersion": 2, |
| 139 | + "conditions": { |
| 140 | + "$lastUpdated": "2023-02-17T21:26:10.5835633Z", |
| 141 | + "$lastUpdatedVersion": 2, |
| 142 | + "temperature": { |
| 143 | + "$lastUpdated": "2023-02-17T21:26:10.5835633Z", |
| 144 | + "$lastUpdatedVersion": 2, |
| 145 | + "critical": { |
| 146 | + "$lastUpdated": "2023-02-17T21:26:10.5835633Z", |
| 147 | + "$lastUpdatedVersion": 2 |
| 148 | + }, |
| 149 | + "warning": { |
| 150 | + "$lastUpdated": "2023-02-17T21:26:10.5835633Z", |
| 151 | + "$lastUpdatedVersion": 2 |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + }, |
| 156 | + "$version": 2, |
| 157 | + "conditions": { |
| 158 | + "temperature": { |
| 159 | + "critical": 100, |
| 160 | + "warning": 75 |
| 161 | + } |
| 162 | + } |
| 163 | + }, |
| 164 | + "reported": { |
| 165 | + "$metadata": { |
| 166 | + "$lastUpdated": "0001-01-01T00:00:00Z" |
| 167 | + }, |
| 168 | + "$version": 1 |
| 169 | + } |
| 170 | + }, |
| 171 | + "status": "enabled", |
| 172 | + "statusReason": null, |
| 173 | + "statusUpdateTime": "0001-01-01T00:00:00+00:00", |
| 174 | + "tags": null, |
| 175 | + "version": 3, |
| 176 | + "x509Thumbprint": { |
| 177 | + "primaryThumbprint": null, |
| 178 | + "secondaryThumbprint": null |
| 179 | + } |
| 180 | + } |
| 181 | + ``` |
| 182 | +
|
| 183 | +## Next steps |
| 184 | +
|
| 185 | +To learn how to use Azure CLI to extend your IoT solution and schedule updates on devices, see [Schedule and broadcast jobs](schedule-jobs-cli.md). |
| 186 | +
|
| 187 | +To continue getting started with IoT Hub and device management patterns, such as end-to-end image-based update, see [Device Update for Azure IoT Hub article using the Raspberry Pi 3 B+ Reference Image](../iot-hub-device-update/device-update-raspberry-pi.md). |
0 commit comments