Search code examples
azure-iot-edge

Azure IoT Edge Runtime - persitency of runtime data with cloud storage


I have an application running on an Azure IoT Edge Runtime which needs to keep values of properties which are changed at runtime by the user. So basically after restarting the device, the user changed data shall still remain. So far I can just store the data to any local storage - no problem.

But when me Edge Device (the hardware) is changed, I would like to but a new one in place and no data shall be lost.

Maybe I'm able to change the device twin desired properties from my edge device application?


Solution

  • Yes, using the device twin desired properties to store runtime data in the cloud and persist it across device restarts and replacements.

    The device twin is a JSON document, stores metadata and configuration information.

    enter image description here

    To update the desired properties from Edge device application, use the Azure IoT SDKs to send a device twin update message to the cloud. The message should contain the updated desired properties for the device twin.

    When replacing the Edge device, you can create a new device twin with the same desired properties as the old device twin, and the runtime data will be restored. And also update the desired properties of the device twin from Edge device application to persist changes done.

    To copy the device twin from the old device

    -Use the Azure IoT Hub REST API or the Azure IoT SDK.

    Mydevice_client = IoTHubDeviceClient.create_from_connection_string(connectionSTring);
    desired_properties = {
        "property1": "value1",
        "property2": "value2"
    }
    
    • The Edge device application has the necessary permissions to read and write to the device twin. And configure the permissions using Azure IoT Hub access policies.

    For more information refer to the MSDoc.