-
-
Notifications
You must be signed in to change notification settings - Fork 25
Environment Variables
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.
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.
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.
While Chamber's flexibility means that you rarely need to override values with environment variables, there are a couple handy use cases for it.
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.
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:
Copyright ©2023
- Release News
- Gem Comparison
- 12-Factor App Rebuttal
- Environment Variable Problems
- Installation
- Basics
- Defining Settings
- Accessing Settings
- Verifying Settings
- Namespaces
- Environment Variables
- Integrations
- Encryption
- Advanced Usage
- Command Line Reference