Mapping Environment Variables to Other Environment Variables

Many frameworks are preconfigured to use specific environment variables that, if set, will populate often-critical information typically related to data service connections and/or environment-specific functionality. In the case of data service connections, Nanobox auto-generates environment variables for each data component's required connection credentials. In this article, I'm going to walk through automatically and dynamically mapping environment variables to the values of other environment variables.

The process of mapping environment variables is actually pretty simple, requiring just a little knowledge of how Nanobox reads environment variables into each environment and a touch of bash scripting.

How Environment Variables are Stored

When starting a service or runtime, Nanobox reads any environment variables stored in the /data/etc/env.d directory. Each key-value pair is stored as a single file with the key being the name of the file and the value being the content of the file. For example, if I wanted an environment variable, MY_VARIABLE=thisismyvariable, in my application's build, I could add the following file:

/data/etc/env.d/MY_VARIABLE

thisismyvariable

While it's definitely easier to set environment variables through the Nanobox CLI or dashboard, these methods don't allow you to automatically map the value of an environment to the value of another existing variable. Knowing how environment variables are read into the environment from /data/etc/env.d makes this really easy to script.

Using Bash to Create Environment Variables

Create a nanobox directory in the root of your project (I'm a fan of name-spacing). Inside nanobox, create a file called set-evars.sh.

mkdir nanobox
touch nanobox/set-evars.sh

Add the following to nanobox/set-evars.sh:

#!/bin/sh
set -e

set_evar () {
  echo $2 > /data/etc/env.d/$1
  echo ✓ $1 set
}

This scripts defines a simple function, set_evar, that does the following:

  1. Expects two arguments in the following order:
    Variable Key: The key of the environment variable you want to set.
    Variable Value: The value. It can be a literal string or a variable expansion if you want to map it to the value of an existing environment variable.
  2. Creates a file inside of /data/etc/env.d using the first argument passed as the file name, then echos the second argument into the created file.
  3. Outputs the key of the variable after it is set. If you want it to output the actual key-value pair, change echo ✓ $1 set to echo ✓ $1=$2

Use the set_evar function with the appropriate arguments and include them in set-evars.sh. Below are two examples of the set_evar function.

# Mapping variables with a variable expansion
set_evar "MY_DATABASE_HOST" "${DATA_DB_HOST}"

# Using a literal string to set the variable
set_evar "MY_DATABASE_NAME" "gonano"

So a full-fledged set-evars.sh would look something like this:

nanobox/set-evars.sh

#!/bin/sh
set -e

set_evar () {
  echo $2 > /data/etc/env.d/$1
  echo ✓ $1 set
}

set_evar "MY_DATABASE_HOST" "${DATA_DB_HOST}"
set_evar "MY_DATABASE_USER" "${DATA_DB_USER}"
set_evar "MY_DATABASE_PASS" "${DATA_DB_PASS}"
set_evar "MY_DATABASE_NAME" "gonano"
set_evar "MY_DATABASE_PORT" "3306"

Using the Script

There are two contexts in which you'll want to use this script to add/map your environment variables - locally and when deploying.

Locally

To add/map your environment variables locally, you'll need to startup a new run session and run the set-evars.sh script.

nanobox run bash nanobox/set-evars.sh

The next time you nanobox run, the environment variables will be there.

Notes for local use:

  • Environment variables are loaded as a terminal session is started, so the variables won't be available in the same terminal session in which they're added. You'll need to start a new session to load them. This can be done by simply starting a new nanobox run session.
  • Any time you rebuild your runtime, /data/etc/env.d will get wiped and you'll need to rerun the set-evars.sh script.

On Deploy

Deploying to a live server is where this method of adding and mapping environment variables really shines. To do this, you need to run bash nanobox/set-evars.sh as a transform hook. This is a unique phase of the deploy process where your entire build is still writable and has access to live environment variables.

boxfile.yml

...

deploy.config:
  transform:
    - bash nanobox/set-evars.sh

...

As your app deploys, it will automatically add and map variables included in your set-evars.sh script.

Notes for live use:

  • Environment variables set using this method will not appear in your Nanobox dashboard or in the output of nanobox evar ls.

Posted in Environment Variables