Skip to content

Environment Variables

Jeff Felchner edited this page Mar 6, 2023 · 5 revisions

Due to the way Chamber is architected, you won't have much of a need for using environment variables to override the settings from your YAML files. But if you do, Chamber has you covered.

Just Getting Started?

Chamber uses a simple formula to determine which environment variables override which YAML settings.

Given the following settings.yml:

# settings.yml

redis:
  url: 'redis://localhost:1234'

When Chamber loads the settings, it would look for an environment variable with the name REDIS_URL. If it finds one, it will replace the YAML value with whatever the value of the environment variable is.

Venturing On...

Environment Variable Name Formula

How do we know which environment variable it's going to look for? This one is pretty easy. Just look at all the parent nesting for the item in the YAML and combine them with underscores.

# settings.yml

http:
  url_components:
    host:   'api.example.com'
    domain: 'example.com'
    port:   '443'

If we wanted to override the port, we would simply define an environment variable named HTTP_URL_COMPONENTS_PORT.

export HTTP_URL_COMPONENTS_PORT="8443"

Then when we load Chamber and try to get the value:

Chamber.dig!('http', 'url_components', 'port')
# => '8443'

we get the correct value back.

Benefits

While Chamber's flexibility means that you rarely need to override values with environment variables, there are a couple handy use cases for it.

Emergency Configuration Changes

If there's a configuration value that was accidentally deployed which is causing a problem, or was properly deployed, but now needs to be changed to fix a production issue, creating a commit to update the YAML file, pushing it up, waiting for CI, waiting for the deploy, etc, may be longer than you want to wait.

In these cases, you can SSH into the box (or use a CLI like Heroku) to add or change the environment variable which corresponds to the value you wish to have, do a soft restart of the app, and you're back in business.

It's the difference of seconds versus minutes.

Note: Just make sure that you don't forget to make the same change in the YAML files so that everyone has access to it. However, it's always possible to forget. Check out chamber compare for tools that can help.

Heroku Addon's Default Environment Variables

When you install Heroku addons (such as Heroku Redis), it will automatically set environment variables for you. Specifically in the case of Redis, it will set REDIS_URL.

As long as Chamber is either using Chamber.dig!('redis', 'url') or Chamber.dig!('redis_url') to access the value, you can rest assured that if the addon ever changes the Redis URL, your app will still continue to work.


Learn More:

Clone this wiki locally