Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pass environment variables in iot edge module

I am building a Microsoft IoT edge python module for my Linux arm device. I have to access the environment variables in the python module from my deployment.template.json file. The file shows how the environment variables are passed from the environment to the module. The problem is how to access them inside the code.

deployment.template.json file:

  "$schema-template": "2.0.0",
  "modulesContent": {
    "$edgeAgent": {
      "properties.desired": {
        "schemaVersion": "1.0",
        "runtime": {
          "type": "docker",
          "settings": {
            "minDockerVersion": "v1.25",
            "loggingOptions": "",
            "registryCredentials": {
              "myRegistryName": {
                "username": "$CONTAINER_REGISTRY_USERNAME",
                "password": "$CONTAINER_REGISTRY_PASSWORD",
                "address": "myRegistryAddress.azurecr.io"
              }
            }
          }
        },
        "systemModules": {
          "edgeAgent": {
            "type": "docker",
            "settings": {
              "image": "mcr.microsoft.com/azureiotedge-agent:1.0",
              "createOptions": {}
            }
          },
          "edgeHub": {
            "type": "docker",
            "status": "running",
            "restartPolicy": "always",
            "settings": {
              "image": "mcr.microsoft.com/azureiotedge-hub:1.0",
              "createOptions": {
                "HostConfig": {
                  "PortBindings": {
                    "5671/tcp": [
                      {
                        "HostPort": "5671"
                      }
                    ],
                    "8883/tcp": [
                      {
                        "HostPort": "8883"
                      }
                    ],
                    "443/tcp": [
                      {
                        "HostPort": "443"
                      }
                    ]
                  }
                }
              }
            }
          }
        },
        "modules": {
          "Module_Name": {
            "version": "1.0",
            "type": "docker",
            "status": "running",
            "restartPolicy": "always",
            "settings": {
              "image": "${MODULES.Module_Name}",
              "createOptions": {}
            },
            "env": {
              "test_var": {
                "value": "secret"
              }
            }
          }
        }
      }
    },
    "$edgeHub": {
      "properties.desired": {
        "schemaVersion": "1.0",
        "routes": {
          "route": "FROM /messages/* INTO $upstream"
        },
        "storeAndForwardConfiguration": {
          "timeToLiveSecs": 7200
        }
      }
    }
  }
}

How can I access the environment test_var variable inside the python code module?

like image 732
Hassan Abbas Avatar asked Mar 15 '26 14:03

Hassan Abbas


2 Answers

os.environ should have it.

Something like

import os
os.environ['test_var']
like image 172
salparadise Avatar answered Mar 17 '26 04:03

salparadise


One needs to build the IoT edge solution first in order to access the variables by using Generate IoT Edge Deployment Manifest in VS Code and then run the solution in the simulator. My code shows how the variable can be accessed.


def main(protocol):
    try:
        print ( "\nPython %s\n" % sys.version )
        print ( "Module client" )

        hub_manager = HubManager(protocol)
        os.environ['test_var'] // prints secret i.e. value of the variable itself


    except IoTHubError as iothub_error:
        print ( "Unexpected error %s from IoTHub" % iothub_error )
        return
    except KeyboardInterrupt:
        print ( "client stopped" )

if __name__ == '__main__':
    main(PROTOCOL)```
like image 44
Hassan Abbas Avatar answered Mar 17 '26 03:03

Hassan Abbas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!