Question

I'm using AWS Opsworks to host my Rails App (Ruby 2.0/Rails 3.2). For assets compilation process, I am using AssetSync to upload the compiled assets automatically on S3. I used to store the credentials as environment variables.

Do you have any idea how can I do this with Chef/Opsworks?

Thanks.

Was it helpful?

Solution 6

This can now be done directly from the AWS Console, on the application configuration, as per documentation : http://docs.aws.amazon.com/opsworks/latest/userguide/workingapps-creating.html#workingapps-creating-environment

OTHER TIPS

I know this is an older post, but I'm posting this in case this helps someone else.

I found the easiest way actually was to use one of Chef's deploy hooks (http://docs.opscode.com/resource_deploy.html#deploy-phases).

Add a directory called 'deploy' at the Rails project root.

In it add a file called before_restart.rb, with the code:

Chef::Log.info("Running deploy/before_restart.rb")

# map the environment_variables node to ENV
node[:deploy].each do |application, deploy|
  deploy[:environment_variables].each do |key, value|
    Chef::Log.info("Setting ENV[#{key}] to #{value}")
    ENV[key] = value
  end
end

When you trigger the OpsWorks deploy, you should be able to see the ENV vars being set in the Rails App Server instance log.

I ended up using https://github.com/joeyAghion/opsworks_custom_env. It works pretty well.

I used a slightly different approach, using OpsWorks hook to copy JSON to application.yml. you can read more about it here: http://zaman.io/how-to-import-aws-opsworks-json-into-rails-app/

Another option outside of environment variables is you can generate a file with the variables in it at deploy time.

For example, for a Rails app, the config/secrets.yml is a reasonable place to put these. I created a deploy/before_restart.rb deploy hook with the following content:

def create_secrets(secrets, release_path)
  Chef::Log.info("Creating secrets")
  file_path = ::File.join(release_path, 'config/secrets.yml')
  ::File.open(file_path, 'w') do |f|
    f.write("production:\n")
    secrets.each do |k,v|
      f.write("  #{k}: #{v}\n")
    end
  end
end

node[:deploy].each do |application, deploy|
  create_secrets(deploy[:secrets], release_path)
end

And then in your OpsWorks stack Custom JSON you can add your secrets:

  "deploy": {
    "super_cool_app": {
      "secrets": {
        "some_service_id": "foo",
        "some_password": "bar"
      }
    }

You can create a deploy folder in the root of your application, create a file before_restart.rb inside it, then in your file run the precompile task

run "cd /srv/www/myapp/current && /usr/local/bin/bundle exec rake assets:precompile"

This file will run on every deployment

Source:

https://www.youtube.com/watch?v=nHu8fCp9GR4&list=WL&index=7

I already answered here: AWS OpsWorks Environment variables not working

Is important to understand that from OpsWorks dashboard we can pass all declared environment variables to Chef, then we need to handle these variables with a Chef recipe to let them available to Rails environment.

Here you can find what you are looking for: https://medium.com/@diego_durante/opsworks-rails-and-environment-variables-30c6a143253c#.696grsgg9

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top