Search code examples
ruby-on-railsherokucarrierwavefog

Rails]Initializing for Fog in development environment


I deploy my rails app onto Heroku, and I've set aws access keys on the server as environment variables. However, to test my application in development environment, I need to initialize them somewhere on my local machine. So I decided to the following.

/config/initailizers/init_aws_locally.rb

ENV['AWS_ACCESS_KEY_ID'] = 'my key'
ENV['AWS_SECRET_ACCESS_KEY'] = 'my secret key'

This file is added in .gitignore

However, when I upload in development environment, I get this error message:

Missing required arguments: aws_access_key_id, aws_secret_access_key

I think somehow I overlooked a simple step to include my aws keys in my development environment. But I'm not sure why the reason for the error when I already initialized the keys.

For your reference, I'm using carrierwave, S3, and Fog.

config/initializers/fog.rb

CarrierWave.configure do |config|
  config.fog_credentials = {
    :provider               => 'AWS',                        # required
    :aws_access_key_id      => ENV['AWS_ACCESS_KEY_ID'],       # required
    :aws_secret_access_key  => ENV['AWS_SECRET_ACCESS_KEY'], # required
    :region                 => 'us-east-1',                  # optional, defaults to 'us-east-1'
  }
  config.fog_directory  = 'd'                         # required
  config.fog_public     = true                                # optional, defaults to true
end

Thank you. I appreciate your help!


Solution

  • I'd avoid putting any credentials in code. It's such a terrible idea and Heroku has the right idea. So what I do is use RVM and put a file .rvmrc in my project folder. I put .rvmrc in .gitignore as well.

    Then you edit .rvmrc to have

    export AWS_ACCESS_KEY_ID="BLAH"

    so on and so forth. Anytime I "cd" into this directory my env is setup for me for that project by RVM. If you're not using RVM there is other alternatives out there.

    rails s

    and it will have all the environment variables setup that you put in your .rvmrc script. No need for initializer or development only yaml config files that you keep out of source control. From my experience this is the simplest solution.