Referencing variables in code

Amateur question here, but how do I reference balena variables in code?
I’m trying to get the following but it doesn’t appear to be working. Any suggestions?

I’d like to be able to toggle the following from the cloud UI with device variables.

$logs_enabled, $process_enabled, $network_enabled

And I’d like to get the following to populate at runtime for the datadog agent to push tags to my host monitor.



logs_enabled: $logs_enabled    ## Set true or false with variable in Balena UI?
  - name: docker
  - name: docker
    polling: true
  enabled: $process_enabled   ## Set true or false with variable in Balena UI?
  enabled: false # disable APM
  enabled: $network_enabled   ## Set true or false with variable in Balena UI?

In your code, the balena variables you mentioned are available as environment variables inside the balena containers. So, how you reference process environment variables within your code will depend on the language you are using.

I would direct you to our DataDog blog post though if you haven’t seen that. It should provide an interface to manage the configurations you mentioned. If not, let us know, and we’ll do what we can.

Thanks but that’s actually what I was trying to improve. Managed to figure it out. I’ll share the details here and pr the official repo.

Yes, please share your solution here for everyone to benefit.


Before I do is there a way to define environment variables with placeholder values in code so that they populate the balena ui when pushing? Then a user only has to go to the ui and replace the values instead of also creating the variable itself?
I’ve seen this done with deploy to balena buttons but can’t figure out how to do on private projects.

Sounds like you are looking for balena.yml configuration file. Here’s the doc explaining the format and providing an example:

I like where you are going with this @barryjump.
Let us know of any further comments and questions about using balena.yml or the wider topic here.

Great thanks. Is balena.yml only compatible if using a deploy with balena mechanism? Or can it work without the button paradigm and instead work with the traditional “balena push”?

I imagine there might be some state problems, ie. user set variables in the ui, then does a balena push on a repo with an outdated balena.yml - which takes precedence? etc etc.

Hi @barryjump,

When you create an application in balenaCloud, do you see an Advanced toggle? If so, you can click that to open other options and drag and drop a balena.yml to the modal window to prepopulate the device and environment variables. This is not available with Starter apps, however, and may still be in beta for other paid accounts.

To include the variables otherwise, you’ll need to create a standard Docker-style .env file (per the Docker documentation.


You may also find this blog post to have some useful comparisons between Docker .env and balena.yml.


1 Like

That blog post was exactly what I was looking for, thank you!