2

My stack is Puma + Nginx + PostgreSQL, on AWS EC2. I'm using Capistrano 3.4 to deploy a Rails 4.2.4 app. I've been stabbing in the dark trying to solve this for a while now so thought I'd see if I can get some help to shed some light on this issue. It's not conceptually difficult, I just don't know why what I've tried doesn't work.

Here's what the Capistrano output says:

...
DEBUG [f85d7a84] Command: cd /home/deploy/my_app/releases/20151210184710 && ( export RVM_BIN_PATH="~/.rvm/bin" aws_access_key_id="<MY_AWS_ACCESS_KEY_ID>" aws_secret_access_key="<MY_AWS_SECRET_ACCESS_KEY>"  RAILS_ENV="production" ; ~/.rvm/bin/rvm default do bundle exec rake assets:precompile )
DEBUG [f85d7a84]    RVM used your Gemfile for selecting Ruby, it is all fine - Heroku does that too,
DEBUG [f85d7a84]    
DEBUG [f85d7a84]    you can ignore these warnings with 'rvm rvmrc warning ignore /home/deploy/my_app/releases/20151210184710/Gemfile'.
DEBUG [f85d7a84]    
DEBUG [f85d7a84]    To ignore the warning for all files run 'rvm rvmrc warning ignore allGemfiles'.
DEBUG [f85d7a84]    
DEBUG [f85d7a84]    
DEBUG [f85d7a84]    
DEBUG [f85d7a84]    rake aborted!
DEBUG [f85d7a84]    
DEBUG [f85d7a84]    ArgumentError: Missing required arguments: aws_access_key_id, aws_secret_access_key
...

Notice in the first line there has been some attempt in the Bash command to set the required arguments, but clearly they can't be accessed in the rake assets:precompile. So my problem is how to set the 'required arguments' aws_access_key_id and aws_secret_access_key correctly.

What I've tried:

1) Setting them in config/deploy.rb (inspired by this)

To do this I used:

set :default_env, {
  rvm_bin_path: '~/.rvm/bin',
  'aws_access_key_id' => '<MY_AWS_ACCESS_KEY_ID>',
  'aws_secret_access_key' => '<MY_AWS_SECRET_ACCESS_KEY>'
 }

2) Setting them on my EC2 server in shared/secrets.yml: (Inspired by this)

aws_access_key_id: '<MY_AWS_ACCESS_KEY_ID>',
aws_secret_access_key: '<MY_AWS_SECRET_ACCESS_KEY>'

I linked to these in config/deploy.rb as

set :linked_files, %w{config/database.yml config/application.yml secrets.yml}

Currently this is how I'm trying to input the keys in config/initializers/carrierwave.rb:

CarrierWave.configure do |config|
    config.fog_credentials = {
      :provider              => 'AWS',
      #:aws_access_key_id     => ENV['AWS_ACCESS_KEY_ID'], # I still get the same error when I use these two lines instead
      #:aws_secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
      :aws_access_key_id     => Rails.application.secrets.aws_access_key_id,
      :aws_secret_access_key => Rails.application.secrets.aws_secret_access_key
    }
    config.fog_directory    = ENV['AWS_S3_BUCKET']
end

3) Setting them in config/application.yml (inspired by this) as

aws_access_key_id: "<MY_AWS_ACCESS_KEY_ID>"
aws_secret_access_key: "<MY_AWS_SECRET_ACCESS_KEY>"

So why aren't any of these methods working? I'm not very experienced at setting environment variables let alone on a remote server but I've seen threads/blog posts where each of these seem to work.

Thanks for your help everyone. Feel free to ask questions or for more info!

Community
  • 1
  • 1
Dennis
  • 421
  • 1
  • 10
  • 22
  • Have you tried setting manually outside like 1) or 2) in the answer in the link: http://stackoverflow.com/questions/33457422/amazon-ses-error-retrieving-credentials-from-the-instance-profile-metadata-serve/33479906#33479906 – phoenix Dec 11 '15 at 14:37
  • 1) Set the environment variables: AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_DEFAULT_REGION with the values from the IAM profile you created. – phoenix Dec 11 '15 at 14:38
  • 2) Instead of 1), you can also create ~/.aws/credentials file. Here you can add the lines: [default] aws_access_key_id = YOUR_AWS_ACCESS_KEY_ID aws_secret_access_key = YOUR_AWS_SECRET_ACCESS_KEY aws_default_region = the region – phoenix Dec 11 '15 at 14:39
  • 1
    If you are running your code on linux. you just need to do 'export AWS_ACCESS_KEY_ID=value' on the shell. do it for all 3 env vars – phoenix Dec 11 '15 at 14:44

1 Answers1

1

I can see a couple of issues with your options, I'll list them accordingly below:

  1. Environment variable names are case-sensitive which means you are exporting for example aws_access_key_id, but from you carrierwave code you are referencing ENV['AWS_ACCESS_KEY_ID'].

    Read more about assigning environment variables here.

  2. Your capistrano is trying to link secrets.yml, what you actually need is config/secrets.yml, try changing that line to:

    set :linked_files, %w{config/database.yml config/application.yml config/secrets.yml}

  3. Assuming you have installed the figaro gem as mentioned in the post referred, I assume (based on the details you provided) the issue will most likely be related to case-sensitivity (goto 1.)

Pick one and give it another go!

Community
  • 1
  • 1
Herk
  • 181
  • 1
  • 8
  • Like you say 1. was the right way. I was getting confused because the error mentions them in lower case, but of course the carrierwave initializer takes them into lower case variables from the upper case environment vars. I thought I had tried them capitalized them anyway, but I just gave it another go and now it works! Really appreciate this. – Dennis Dec 11 '15 at 14:58