Search code examples
ruby-on-railsamazon-web-servicesamazon-elastic-beanstalkcredstash

Set environment variable from credstash on Elastic Beanstalk


I'm having some issues with Elastic Beanstalk environment variables which I want to set from credstash.

option_settings:
  - namespace: aws:elasticbeanstalk:application:environment
    option_name: SECRET_KEY_BASE
    value: $(credstash --region eu-west-1 -t credstash get test.secret_key_base)

I have specified that credstash should be installed from Python:

packages:
  python:
    credstash: []

However, when I deploy asset precompilation fails (rake assets:precompile).

The EB health page shows that application deployment failed.

/opt/elasticbeanstalk/support/envvars: line 5: credstash: command not found
...
+ su -s /bin/bash -c 'bundle exec rake assets:precompile' webapp
`/home/webapp` is not a directory.
Bundler will use `/tmp/bundler/home/webapp' as your home directory temporarily.
rake aborted!
ArgumentError: `secret_key_base` for production environment must be a type of String`

secret_key_base is set to ENV['SECRET_KEY_BASE'] in secrets.yml, so it should contain the value from credstash. However, due to the credstash: command not found output, I'm guessing credstash was not installed or is not on the path for some reason.

Does anyone have any idea what could be going on here?


Solution

  • The answer was to set RAILS_SKIP_ASSET_COMPILATION to true. This will skip the /opt/elasticbeanstalk/hooks/appdeploy/pre/11_asset_compilation.sh hook, which runs before credstash is installed.

    Instead I included this in a .config file:

    container_commands:
      01_assets_compile:
        command: bundle exec rake assets:precompile RAILS_ENV=production
    

    This will precompile the assets on each container, when credstash will be installed.