Search code examples
ruby-on-railsruby-on-rails-4amazon-web-servicesnginxfog

Ruby on Rails + Michael Hartl tutorial + VPS = issues with AWS


I have finished Michael Hartl tutorial and I'm trying to deploy it with https://www.digitalocean.com/community/tutorials/how-to-deploy-a-rails-app-with-puma-and-nginx-on-ubuntu-14-04. Unfortunately I'm stuck with RAILS_ENV=production rake db:migrate because it's generating following error:

ArgumentError: Missing required arguments: aws_access_key_id, aws_secret_access_key

/home/me/simpleapp/config/initializers/carrier_wave.rb:3:in `block in <top (required)>'

/home/me/simpleapp/config/initializers/carrier_wave.rb:2:in `<top (required)>'

/home/me/simpleapp/config/environment.rb:5:in `<top (required)>

'

I have even copied those files 1:1 and still this error persists. Any idea how could I solve it? I really am tired with trying to fix it with different solutions from web. Thanks

edit: I am adding my carrier_wave file:

if Rails.env.production?
  CarrierWave.configure do |config|
    config.fog_credentials = {
      # Configuration for Amazon S3
      :provider              => 'AWS',
      :aws_access_key_id     => ENV['S3_ACCESS_KEY'],
      :aws_secret_access_key => ENV['S3_SECRET_KEY']
    }
    config.fog_directory     =  ENV['S3_BUCKET']
  end
end

Solution

  • The root issue is that Carrier Wave is expecting your AWS environment variables to be populated but they are not set in your production environment.

    I would recommend you look at something like the dotenv gem which can find here. Environment variable housekeeping in multiple environments can be a real pain so it helps to have a tool to facilitate. Dotenv (and others like it) provides more of a turn key approach to this.

    Note that the recently introduced secrets.yml for Rails secret management is a nice step in the right direction, but still requires a bit of code/know how to utilize.

    In any event, be very careful about how you manage any files with secrets. At a minimum, the file should be in your .gitignore so that you aren't potentially broadcasting your secrets.