In search for a smart way of overwriting all application service environment variables

In my application I have a number of application service environment variables. The idea was to have them only as standard values since all devices will need to overwrite them with their custom configuration.

Once the device service environment variables exist, I can easily grab all of them from an offboard web application with the python sdk method:
variables = balena.models.environment_variables.device_service_environment_variable.get_all(uuid)

To be able to do this I however manually need to overwrite all of the application service environment variables in the dashboard for each device or add some logic to do it using the sdk. Is this the way to go or am I missing a hidden smart feature to do it more or less automatically?

Hi,
In case that these values need to be unique per device (for example unique client credentials) and the device itself can’t infer/fetch them from anywhere else, then your approach of defining them as device service env vars seems correct.

As per the docs, you could define values common across device services in service env vars (eg: timeout for requests to service XYZ) and moreover use application env vars for values that shoud be common for all services & devices of your whole fleet.
https://www.balena.io/docs/learn/manage/serv-vars/#fleet-environment-and-service-variables

About resin.models.environment_variables.device_service_environment_variable.get_all(uuid), is there a particular reason that you use this method to get all device service env vars?
Have you tried accessing the env vars from the container’s environment variables using os.environ?
Doing something like:

import os
print(os.environ.get('MY_ENV_VAR_FOR_XYZ '))

should print the most specific value for the respective env var.
Containers do get the env vars injected in their environment in order of specificyty, and the values of each level overrides any preexisting values if they exist. The specificity order, which the most specific on top, are as follows:

  • device service env vars
  • device env vars
  • service variables (application level)
  • environment variables (application level)

Thanks for the quick and thorough response, although mine lacks a bit in speed.

I see that I did not provide enough details at the beginning.

The variables are specific to each device and I am accessing them using the python SDK because I am editing them from an offboard Django web application. The web application is configured to read and write service environment variables as this is what the device eventually needs. The app “fails” if the service environment variables are not set because the function call returns an empty list.

The more specific question is thus whether I can easily copy any lower level environment variables with the Python SDK in my web application to have them appear as device service env vars - or perhaps a function call to grab the top level of set variables from the hierarchy you describe and use them to set the device service env vars. In case its not possible I will just do it manually, I am only here to see if there is a smarter way, since Balena seems to have smart ways to accomplish most tasks.

Thanks for getting back to us - I will try finding an answer for you :slight_smile: