There are a few ways to add environment variables to your Rails application. On Engine Yard Cloud, the easiest way is through the Environment Variables link on the environment page as shown below.
Important: This feature only works with the stable-v5 stack, from version 3.0.40 upwards. Refer to the upgrade guide for further details on how to upgrade an environment.
Back to the environments page, click Environment Variables, then click New Variable. Enter the Name and Value. Check the Sensitive box if you're adding a credential or any other info that needs to be stored encrypted on the instances.
After adding environment variables, click Apply or Upgrade (if you haven't upgraded yet).
When all instances are green, you need to restart your Rails application for the environment variables to take effect. You can deploy your application using the Deploy button or the engineyard gem.
How does this work?
When you click Apply or Upgrade, our Chef recipes will create env.cloud located in /data/APPNAME/shared/config. This file is sourced by the scripts that start Unicorn, Passenger, DelayedJob, Resque, and Sidekiq thus making the variables available to the underlying Rails app.
A few things need to be taken into account when using environment variables on an application deployed on Engine Yard. Here we go:
- Environment variables are put in place by Chef scripts. Hence, every time one is added/removed/modified an 'Apply' is needed on the environment.
- Environment variables are read only on the start of a process. Hence, for new values to be recognized the process has to be restarted. The Engine Yard deployment process restarts DelayedJob, Resque, and Sidekiq workers, but reloads Unicorn, Passenger, and Puma workers. As such, for restarting those workers you'll need to access every app instance and run /engineyard/bin/app_<appname> restart or through the use of the ey cli tool so to automate that command.