Azure IoT Edge Runtime environment variables: Retrieving IoT Edge Device ID

Dhanavath Vishnu 366 Reputation points
2023-02-14T13:46:39.63+00:00

Hi Team,

I am looking for retrieving the IoT edge device id using a custom IoT Edge module. The Idea is whenever, a deployment is done on a IoT edge device which is having IoT Edge runtime on Linux amd64, on the successful running of custom IoT Edge module, can the custom IoT Edge Module pick the device id from the IoT Edge runtime.

These will remove the hardcoding of device details and environment variable setup in the deployment manifest.

Is this understanding being correct? If so, can you guide me, how can I do that?

I explored the IoT Edge Metrics Collector module, which is having ability to collect the device id of running device, but the data is collecting so much that it is preorganized in log analytics space.

Thanks for your time

D. Vishnu

Azure IoT Edge
Azure IoT Edge
An Azure service that is used to deploy cloud workloads to run on internet of things (IoT) edge devices via standard containers.
567 questions
Azure IoT Hub
Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
1,175 questions
{count} votes

Accepted answer
  1. AshokPeddakotla-MSFT 32,781 Reputation points
    2023-02-16T14:11:13.6266667+00:00

    Dhanavath Vishnu I'm collating my comments and adding as answer for better understanding.

    I am looking for retrieving the IoT edge device id using a custom IoT Edge module. The Idea is whenever, a deployment is done on a IoT edge device which is having IoT Edge runtime on Linux amd64, on the successful running of custom IoT Edge module, can the custom IoT Edge Module pick the device id from the IoT Edge runtime.

    Can you please elaborate more on "can the custom IoT Edge Module pick the device id from the IoT Edge runtime." ?

    You have access to that via environment variables which are being passed into your module: IOTEDGE_DEVICEID (you can do a docker inspect on your module to see for more details)

    Also, Here is an example of python how to use the IOTEDGE_DEVICEID environment variable to get the device ID in a custom IoT Edge module:

    import os
    device_id = os.environ["IOTEDGE_DEVICEID"]

    Below is the sample code for getting DeviceID using C#.

    string deviceId = Environment.GetEnvironmentVariable("IOTEDGE_DEVICEID");
    
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.