Skip to content

Commit 58f7092

Browse files
authored
Merge pull request #301065 from PatAltimore/patricka-freshness2
Freshness review
2 parents 61f2b2c + 91cd440 commit 58f7092

File tree

1 file changed

+47
-48
lines changed

1 file changed

+47
-48
lines changed

articles/iot-edge/how-to-monitor-module-twins.md

Lines changed: 47 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,36 @@ description: How to interpret device twins and module twins to determine connect
44
author: PatAltimore
55

66
ms.author: patricka
7-
ms.date: 06/03/2024
7+
ms.date: 06/09/2025
88
ms.topic: how-to
9-
ms.reviewer: veyalla
109
ms.service: azure-iot-edge
1110
services: iot-edge
1211
---
1312
# Monitor module twins
1413

1514
[!INCLUDE [iot-edge-version-all-supported](includes/iot-edge-version-all-supported.md)]
1615

17-
Module twins in Azure IoT Hub enable monitoring the connectivity and health of your IoT Edge deployments. Module twins store useful information in your IoT hub about the performance of your running modules. The [IoT Edge agent](iot-edge-runtime.md#iot-edge-agent) and the [IoT Edge hub](iot-edge-runtime.md#iot-edge-hub) runtime modules each maintain their module twins, `$edgeAgent` and `$edgeHub`, respectively:
16+
Module twins in Azure IoT Hub let you monitor the connectivity and health of your IoT Edge deployments. Module twins store information in your IoT hub about the performance of your running modules. The [IoT Edge agent](iot-edge-runtime.md#iot-edge-agent) and the [IoT Edge hub](iot-edge-runtime.md#iot-edge-hub) runtime modules each maintain their own module twins: `$edgeAgent` and `$edgeHub`.
1817

19-
* `$edgeAgent` contains health and connectivity data about both the IoT Edge agent and IoT Edge hub runtime modules, and your custom modules. The IoT Edge agent is responsible for deploying the modules, monitoring them, and reporting connection status to your Azure IoT hub.
20-
* `$edgeHub` contains data about communications between the IoT Edge hub running on a device and your Azure IoT hub. This includes processing incoming messages from downstream devices. IoT Edge hub is responsible for processing the communications between the Azure IoT Hub and the IoT Edge devices and modules.
18+
* `$edgeAgent` has health and connectivity data about the IoT Edge agent, IoT Edge hub runtime modules, and your custom modules. The IoT Edge agent deploys the modules, monitors them, and reports connection status to your Azure IoT hub.
19+
* `$edgeHub` has data about communications between the IoT Edge hub running on a device and your Azure IoT hub. This includes processing incoming messages from downstream devices. The IoT Edge hub processes communications between Azure IoT Hub and IoT Edge devices and modules.
2120

22-
The data is organized into metadata, tags, along with desired and reported property sets in the module twins' JSON structures. The desired properties you specified in your deployment.json file are copied to the module twins. The IoT Edge agent and the IoT Edge hub each update the reported properties for their modules.
21+
The data is organized into metadata, tags, and desired and reported property sets in the module twins' JSON structures. The desired properties you specify in your deployment.json file are copied to the module twins. The IoT Edge agent and the IoT Edge hub each update the reported properties for their modules.
2322

24-
Similarly, the desired properties specified for your custom modules in the deployment.json file are copied to its module twin, but your solution is responsible for providing its reported property values.
23+
Similarly, the desired properties you specify for your custom modules in the deployment.json file are copied to its module twin, but your solution provides its reported property values.
2524

26-
This article describes how to review the module twins in the Azure portal, Azure CLI, and in Visual Studio Code. For information on monitoring how your devices receive the deployments, see [Monitor IoT Edge deployments](how-to-monitor-iot-edge-deployments.md). For an overview on the concept of module twins, see [Understand and use module twins in IoT Hub](../iot-hub/iot-hub-devguide-module-twins.md).
25+
This article shows how to review the module twins in the Azure portal, Azure CLI, and Visual Studio Code. For information on monitoring how your devices receive the deployments, see [Monitor IoT Edge deployments](how-to-monitor-iot-edge-deployments.md). For an overview of module twins, see [Understand and use module twins in IoT Hub](../iot-hub/iot-hub-devguide-module-twins.md).
2726

2827
> [!TIP]
29-
> The reported properties of a runtime module could be stale if an IoT Edge device gets disconnected from its IoT hub. You can [ping](how-to-edgeagent-direct-method.md#ping) the `$edgeAgent` module to determine if the connection was lost.
28+
> The reported properties of a runtime module can be stale if an IoT Edge device gets disconnected from its IoT hub. [Ping](how-to-edgeagent-direct-method.md#ping) the `$edgeAgent` module to check if the connection was lost.
3029
3130
## Monitor runtime module twins
3231

3332
To troubleshoot deployment connectivity issues, review the IoT Edge agent and IoT Edge hub runtime module twins and then drill down into other modules.
3433

3534
### Monitor IoT Edge agent module twin
3635

37-
The following JSON shows the `$edgeAgent` module twin in Visual Studio Code with most of the JSON sections collapsed.
36+
This JSON shows the `$edgeAgent` module twin in Visual Studio Code with most sections collapsed.
3837

3938
```json
4039
{
@@ -75,41 +74,41 @@ The following JSON shows the `$edgeAgent` module twin in Visual Studio Code with
7574
}
7675
```
7776

78-
The JSON can be described in the following sections, starting from the top:
77+
This JSON has these sections:
7978

80-
* Metadata - Contains connectivity data. Interestingly, the connection state for the IoT Edge agent is always in a disconnected state: `"connectionState": "Disconnected"`. The reason being the connection state pertains to device-to-cloud (D2C) messages and the IoT Edge agent doesn't send D2C messages.
81-
* Properties - Contains the `desired` and `reported` subsections.
82-
* Properties.desired - (shown collapsed) Expected property values set by the operator in the deployment.json file.
83-
* Properties.reported - Latest property values reported by IoT Edge agent.
79+
* Metadata - Has connectivity data. The connection state for the IoT Edge agent is always `"Disconnected"` because it applies to device-to-cloud (D2C) messages, and the IoT Edge agent doesn't send D2C messages.
80+
* Properties - Has the `desired` and `reported` subsections.
81+
* Properties.desired - (shown collapsed) Expected property values set in the deployment.json file.
82+
* Properties.reported - Latest property values reported by the IoT Edge agent.
8483

85-
Both the `properties.desired` and `properties.reported` sections have a similar structure and contain additional metadata for schema, version, and runtime information. Also included is the `modules` section for any custom modules (such as `SimulatedTemperatureSensor`), and the `systemModules` section for `$edgeAgent` and the `$edgeHub` runtime modules.
84+
Both the `properties.desired` and `properties.reported` sections have a similar structure and include metadata for schema, version, and runtime information. They also have a `modules` section for custom modules like `SimulatedTemperatureSensor`, and a `systemModules` section for `$edgeAgent` and `$edgeHub` runtime modules.
8685

87-
By comparing the reported property values against the desired values, you can determine discrepancies and identify disconnections that can help you troubleshoot issues. In doing these comparisons, check the `$lastUpdated` reported value in the `metadata` section for the property you're investigating.
86+
Compare the reported property values to the desired values to find discrepancies and identify disconnections that help you troubleshoot issues. When you compare values, check the `$lastUpdated` reported value in the `metadata` section for the property you're investigating.
8887

89-
The following properties are important to examine for troubleshooting:
88+
Check these properties when you troubleshoot:
9089

91-
* **exitcode** - Any value other than zero indicates that the module stopped with a failure. However, error codes 137 or 143 are used if a module was intentionally set to a stopped status.
90+
* **exitcode** - Any value other than zero means the module stopped with a failure. Error codes 137 or 143 are used if a module is intentionally set to a stopped status.
9291

93-
* **lastStartTimeUtc** - Shows the **DateTime** that the container was last started. This value is 0001-01-01T00:00:00Z if the container wasn't started.
92+
* **lastStartTimeUtc** - Shows the **DateTime** when the container last started. This value is 0001-01-01T00:00:00Z if the container isn't started.
9493

95-
* **lastExitTimeUtc** - Shows the **DateTime** that the container last finished. This value is 0001-01-01T00:00:00Z if the container is running and was never stopped.
94+
* **lastExitTimeUtc** - Shows the **DateTime** when the container last finished. This value is 0001-01-01T00:00:00Z if the container is running and was never stopped.
9695

97-
* **runtimeStatus** - Can be one of the following values:
96+
* **runtimeStatus** - Has one of these values:
9897

9998
| Value | Description |
10099
| --- | --- |
101100
| unknown | Default state until deployment is created. |
102-
| backoff | The module is scheduled to start but isn't currently running. This value is useful for a module undergoing state changes in restarting. When a failing module is awaiting restart during the cool-off period, the module will be in a backoff state. |
103-
| running | Indicates that the module is currently running. |
104-
| unhealthy | Indicates a health-probe check failed or timed out. |
105-
| stopped | Indicates that the module exited successfully (with a zero exit code). |
106-
| failed | Indicates that the module exited with a failure exit code (non-zero). The module can transition back to backoff from this state depending on the restart policy in effect. This state can indicate that the module has experienced an unrecoverable error. Failure occurs when the Microsoft Monitoring Agent (MMA) can no longer resuscitate the module, requiring a new deployment. |
101+
| backoff | The module is scheduled to start but isn't running. This value is useful for a module that's restarting. When a failing module is awaiting restart during the cool-off period, the module is in a backoff state. |
102+
| running | The module is running. |
103+
| unhealthy | A health-probe check failed or timed out. |
104+
| stopped | The module exited successfully (with a zero exit code). |
105+
| failed | The module exited with a failure exit code (non-zero). The module can go back to backoff from this state depending on the restart policy. This state means the module has an unrecoverable error. Failure happens when the Microsoft Monitoring Agent (MMA) can't restart the module, requiring a new deployment. |
107106

108-
See [EdgeAgent reported properties](module-edgeagent-edgehub.md#edgeagent-reported-properties) for details.
107+
For details, see [EdgeAgent reported properties](module-edgeagent-edgehub.md#edgeagent-reported-properties).
109108

110109
### Monitor IoT Edge hub module twin
111110

112-
The following JSON shows the `$edgeHub` module twin in Visual Studio Code with most of the JSON sections collapsed.
111+
The following JSON shows the `$edgeHub` module twin in Visual Studio Code with most sections collapsed.
113112

114113
```json
115114
{
@@ -156,31 +155,31 @@ The JSON can be described in the following sections, starting from the top:
156155

157156
* Properties - Contains the `desired` and `reported` subsections.
158157
* Properties.desired - (shown collapsed) Expected property values set by the operator in the deployment.json file.
159-
* Properties.reported - Latest property values reported by IoT Edge hub.
158+
* Properties.reported - Latest property values reported by the IoT Edge hub.
160159

161-
If you're experiencing issues with your downstream devices, examining this data would be a good place to start.
160+
If you're having issues with downstream devices, start by checking this data.
162161

163162
## Monitor custom module twins
164163

165-
The information about the connectivity of your custom modules is maintained in the IoT Edge agent module twin. The module twin for your custom module is used primarily for maintaining data for your solution. The desired properties you defined in your deployment.json file are reflected in the module twin, and your module can update reported property values as needed.
164+
The IoT Edge agent module twin keeps information about the connectivity of your custom modules. The module twin for your custom module mainly stores data for your solution. The desired properties you define in your deployment.json file appear in the module twin, and your module can update reported property values as needed.
166165

167-
You can use your preferred programming language with the [Azure IoT Hub Device SDKs](../iot-hub/iot-hub-devguide-sdks.md#azure-iot-hub-device-sdks) to update reported property values in the module twin, based on your module's application code. The following procedure uses the Azure SDK for .NET to do this, using code from the [SimulatedTemperatureSensor](https://github.com/Azure/iotedge/blob/main/edge-modules/SimulatedTemperatureSensor/src/Program.cs) module:
166+
Use your preferred programming language with the [Azure IoT Hub Device SDKs](../iot-hub/iot-hub-devguide-sdks.md#azure-iot-hub-device-sdks) to update reported property values in the module twin based on your module's application code. The following procedure uses the Azure SDK for .NET and code from the [SimulatedTemperatureSensor](https://github.com/Azure/iotedge/blob/main/edge-modules/SimulatedTemperatureSensor/src/Program.cs) module:
168167

169-
1. Create an instance of the [ModuleClient](/dotnet/api/microsoft.azure.devices.client.moduleclient) with the [CreateFromEnvironmentAsync](/dotnet/api/microsoft.azure.devices.client.moduleclient.createfromenvironmentasync) method.
168+
1. Create an instance of the [ModuleClient](/dotnet/api/microsoft.azure.devices.client.moduleclient) by using the [CreateFromEnvironmentAsync](/dotnet/api/microsoft.azure.devices.client.moduleclient.createfromenvironmentasync) method.
170169

171-
1. Get a collection of the module twin's properties with the [GetTwinAsync](/dotnet/api/microsoft.azure.devices.client.moduleclient.gettwinasync) method.
170+
1. Get the module twin's properties by using the [GetTwinAsync](/dotnet/api/microsoft.azure.devices.client.moduleclient.gettwinasync) method.
172171

173-
1. Create a listener (passing a callback) to catch changes to desired properties with the [SetDesiredPropertyUpdateCallbackAsync](/dotnet/api/microsoft.azure.devices.client.deviceclient.setdesiredpropertyupdatecallbackasync) method.
172+
1. Create a listener with a callback to catch changes to desired properties by using the [SetDesiredPropertyUpdateCallbackAsync](/dotnet/api/microsoft.azure.devices.client.deviceclient.setdesiredpropertyupdatecallbackasync) method.
174173

175-
1. In your callback method, update the reported properties in the module twin with the [UpdateReportedPropertiesAsync](/dotnet/api/microsoft.azure.devices.client.moduleclient) method, passing a [TwinCollection](/dotnet/api/microsoft.azure.devices.shared.twincollection) of the property values that you want to set.
174+
1. In the callback method, update the reported properties in the module twin by using the [UpdateReportedPropertiesAsync](/dotnet/api/microsoft.azure.devices.client.moduleclient) method, and pass a [TwinCollection](/dotnet/api/microsoft.azure.devices.shared.twincollection) of the property values to set.
176175

177176
## Access the module twins
178177

179-
You can review the JSON for module twins in the Azure IoT Hub, in Visual Studio Code, and with Azure CLI.
178+
Review the JSON for module twins in Azure IoT Hub, Visual Studio Code, or Azure CLI.
180179

181180
### Monitor in Azure IoT Hub
182181

183-
To view the JSON for the module twin:
182+
View the JSON for the module twin:
184183

185184
1. Sign in to the [Azure portal](https://portal.azure.com) and navigate to your IoT hub.
186185
1. Select **Devices** under the **Device management** menu.
@@ -189,25 +188,25 @@ To view the JSON for the module twin:
189188

190189
:::image type="content" source="./media/how-to-monitor-module-twins/select-module-twin.png" alt-text="Screenshot showing how to select a module twin to view in the Azure portal.":::
191190

192-
If you see the message "A module identity doesn't exist for this module", this error indicates that the back-end solution is no longer available that originally created the identity.
191+
If you see the message "A module identity doesn't exist for this module", this error means the back-end solution that originally created the identity isn't available.
193192

194193
### Monitor module twins in Visual Studio Code
195194

196-
To review and edit a module twin:
195+
Review and edit a module twin:
197196

198-
1. If not already installed, install the [Azure IoT Edge](https://marketplace.visualstudio.com/items?itemName=vsciot-vscode.azure-iot-edge) and [Azure IoT Hub](https://marketplace.visualstudio.com/items?itemName=vsciot-vscode.azure-iot-toolkit) extensions. The *Azure IoT Edge tools for Visual Studio Code* extension is in [maintenance mode](https://github.com/microsoft/vscode-azure-iot-edge/issues/639).
199-
1. In the **Explorer**, expand the **Azure IoT Hub**, and then expand the device with the module you want to monitor.
200-
1. Right-click the module and select **Edit Module Twin**. A temporary file of the module twin is downloaded to your computer and displayed in Visual Studio Code.
197+
1. Install the [Azure IoT Edge](https://marketplace.visualstudio.com/items?itemName=vsciot-vscode.azure-iot-edge) and [Azure IoT Hub](https://marketplace.visualstudio.com/items?itemName=vsciot-vscode.azure-iot-toolkit) extensions if they aren't already installed. The *Azure IoT Edge tools for Visual Studio Code* extension is in [maintenance mode](https://github.com/microsoft/vscode-azure-iot-edge/issues/639).
198+
1. In the **Explorer**, expand **Azure IoT Hub**, then expand the device with the module you want to monitor.
199+
1. Select the module, then select **Edit Module Twin**. A temporary file of the module twin downloads to your computer and opens in Visual Studio Code.
201200

202201
:::image type="content" source="./media/how-to-monitor-module-twins/edit-module-twin-vscode.png" alt-text="Screenshot showing how to get a module twin to edit in Visual Studio Code.":::
203202

204-
If you make changes, select **Update Module Twin** above the code in the editor to save changes to your IoT hub.
203+
After making changes, select **Update Module Twin** above the code in the editor to save them to your IoT hub.
205204

206205
:::image type="content" source="./media/how-to-monitor-module-twins/update-module-twin-vscode.png" alt-text="Screenshot showing how to update a module twin in Visual Studio Code.":::
207206

208207
### Monitor module twins in Azure CLI
209208

210-
To see if IoT Edge is running, use the [az iot hub invoke-module-method](how-to-edgeagent-direct-method.md#ping) to ping the IoT Edge agent.
209+
Check if IoT Edge is running by using the [az iot hub invoke-module-method](how-to-edgeagent-direct-method.md#ping) command to ping the IoT Edge agent.
211210

212211
The [az iot hub module-twin](/cli/azure/iot/hub/module-twin) structure provides these commands:
213212

@@ -216,7 +215,7 @@ The [az iot hub module-twin](/cli/azure/iot/hub/module-twin) structure provides
216215
* **az iot hub module-twin replace** - Replace a module twin definition with a target JSON.
217216

218217
>[!TIP]
219-
>To target the runtime modules with CLI commands, you may need to escape the `$` character in the module ID. For example:
218+
>To target the runtime modules with CLI commands, you might need to escape the `$` character in the module ID. For example:
220219
>
221220
>```azurecli
222221
>az iot hub module-twin show -m '$edgeAgent' -n <hub name> -d <device name>
@@ -230,4 +229,4 @@ The [az iot hub module-twin](/cli/azure/iot/hub/module-twin) structure provides
230229
231230
## Next steps
232231
233-
Learn how to [communicate with EdgeAgent using built-in direct methods](how-to-edgeagent-direct-method.md).
232+
Learn how to [communicate with EdgeAgent by using built-in direct methods](how-to-edgeagent-direct-method.md).

0 commit comments

Comments
 (0)