Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I define environment variables locally and not change their definitions/push them to heroku?

I have many credentials that I have to handle in order to hook my app up to amazon s3 and other services.

I got my heroku app up and running with s3, and it works great. I defined my s3 access credentials following this example: http://devcenter.heroku.com/articles/config-vars

However, I want now to be able to have access to s3 from my local development environment. Obviously, the config vars that I defined on heroku aren't available on my localhost. How can I define these keys locally? Also, I'm looking in particular for a solution that is secure (for example if I define my keys in plain text in an intializer or something, I don't want that file to be pushed on heroku).

For background, here is what I add to my model to get paperclip running with s3

 has_attached_file :photo,
:storage => :s3,
:bucket => 'bucket_name',
:s3_credentials => {
  :access_key_id => ENV['S3_KEY'],
  :secret_access_key => ENV['S3_SECRET']
}
like image 815
jay Avatar asked Feb 23 '12 15:02

jay


People also ask

Can heroku use localhost?

Start your app locally using the heroku local command, which is installed as part of the Heroku CLI. Your app should now be running on http://localhost:5000/.

Does .ENV work in heroku?

Heroku Local is a command-line tool to run Procfile-backed apps. It is installed automatically as part of the Heroku CLI. Heroku Local reads configuration variables from a . env file.

Are heroku config variables safe?

Heroku config vars are designed to be safe for storing sensitive information. All config vars are stored in an encrypted form and safely stored. These are only decrypted and loaded when booting your app in a dyno itself.

How do I add an environment variable in heroku?

Once you are on the "Settings" page, scroll until you see the "Config Vars" section. To add config vars (environment variables) or modify the existing ones, click on "Reveal Config Vars". Since you do not have any config vars, you should see the same message as shown in Figure 4.


2 Answers

The best place to define stuff like this, if you don't want it shared, is probably an initializer.

# config/initializers/s3_constants.rb


if Rails.env.development?
  S3_KEY = "mys3key"
  S3_SECRET = "mys3secret"
end

Ensure this file is added to .gitignore so it won't be pushed along with the rest of your repository.

Realistically speaking, constants that differ on a per-environment basis should really be located in the file for that environment (say development.rb here)... but those files should also really be added to your version control system, and if you definitely, definitely want this data excluded from git, then a separate file that you do not commit is probably your best bet.

like image 51
Veraticus Avatar answered Sep 18 '22 23:09

Veraticus


Just define the environment variables in your .bash_profile file like any other environment variable. Maybe leave a comment to demarcate the section as Rails-specific environment variables.

#~/.bash_profile
# Rails constants
S3_KEY="blady"
S3_SECRET="bladybloo123"

Also, maybe you want to change the name to something more specific so that you can have more than one s3 connection defined...

like image 25
WattsInABox Avatar answered Sep 20 '22 23:09

WattsInABox