Why avoid using environment variables as configuration?
The use of “environment variables as configuration” is recommended by “12 factor”. While this is a common practice and often leads to few or no problems, calling it a best practice is a bit much.
The reason cited by the 12 factor website for using them is:
The twelve-factor app stores config in environment variables (often shortened to env vars or env). Env vars are easy to change between deploys without changing any code; unlike config files, there is little chance of them being checked into the code repo accidentally; and unlike custom config files, or other config mechanisms such as Java System Properties, they are a language- and OS-agnostic standard.
Two of these are fine reasons. It is true that these are both good reasons:
- Easy to change between deploys.
- Language and OS agnostic.
However, neither of these things requires that config be stored in environment variables. It’s easy enough to create language and OS agnostic configuration files (INI, YAML, etc.) and it’s usually straightforward to make files easy to change between deployments too - e.g. if a deployment is containerized, by mounting the file.
It is less true that environment variables are inherently “easier” to change between deployments - writing a file is not intrinsically difficult unless it is made difficult (e.g. a file is baked in to a container image rather than being mounted), it isn’t hard to change.
Moreover, there are several disadvantages to using environment variables that tend to exhibit themselves nastily when the size of the configuration grows beyond a certain point.
Environment variables are global state
Environment variables are a form of global state. Every variable is associated only with the environment. The variables will be shared with many other variables which have a multitude of different uses:
The chances of variable cross contamination is high - accidentally naming one variable the same as another which is unknowingly used for a different purpose (e.g. PATH) is elevated, and this can have both weird, hard to debug and terrible effects.
If you need to inspect environment variables e.g. to find one that you thought was there and it actually missing, tracking it down is a pain.
Global state in and of itself isn’t a “bad thing” but too much global state is a very bad thing. A small amount of configuration (e.g. less than 10 variables) can often be placed in to environment variables with very little harm, but as soon as the amount grows the danger of the global state grows.
Environment variable values cannot handle structures more complex than a string
Environment variables are a set of key-value pairs where the key is almost always an uppercase string and the value is always a string.
While this is more than sufficient for many purposes, there are many kinds of situations where the configuration data that needs to be stored requires something a bit more complicated than just a string.
Where developers run in to this limitation they have a tendency to create an ugly substructure with cryptic codes within the string.
The way that LS_COLORS is used is a good example:
Clear as mud, right?
(This isn’t actually an example of 12 factor being followed assiduously, but I have seen many examples just like it where it was).
In StrictYAML the following could be represented as:
# Special di: 01;34 # directory is blue # Extensions *.tz: 01;31 # red *.flv: 01;35 # purple
While the codes are cryptic and probably should be changed (e.g. directory: blue), the cryptic nature can at least be easily explained with comments.
Creating naming conventions to handle the inability to handle
A common example:
PERSONNEL_DATABASE_HOST, PERSONNEL_DATABASE_PORT, PERSONNEL_DATABASE_NAME, PERSONNEL_DATABASE_PASSWORD, FACTORY_DATABASE_HOST, FACTORY_DATABASE_PORT, FACTORY_DATABASE_NAME, FACTORY_DATABASE_PASSWORD, HOTEL_BACKUP_DATABASE_HOST, HOTEL_BACKUP_DATABASE_USERNAME, HOTEL_BACKUP_DATABASE_PASSWORD, HOTEL_DATABASE_HOST, HOTEL_DATABASE_PORT, HOTEL_DATABASE_NAME, HOTEL_DATABASE_PASSWORD
Did you spot the accidentally missed variable in the list above which caused the critical bug?
database: personnel: host: xxx port: xxx name: xxx password: xxx factory: host: xxx port: xxx name: xxx password: xxx hotel backup: host: xxx name: xxx password: xxx hotel: host: xxx name: xxx port: xxx password: xxx
What about now?