3

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?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Hassan Abbas
  • 1,166
  • 20
  • 47

3 Answers3

7

os.environ should have it.

Something like

import os
os.environ['test_var']
salparadise
  • 5,699
  • 1
  • 26
  • 32
1

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)```
Hassan Abbas
  • 1,166
  • 20
  • 47
1

For .Net C# / .Net core

string environmentValue = Environment.GetEnvironmentVariable("environment-name");
Sgedda
  • 1,323
  • 1
  • 18
  • 27