Environment variables not loading

Report
Question

Please briefly explain why you feel this question should be reported .

Report Cancel

I just set up a server on ec2 and installed all that was needed.
Trying to start my rails app from the app’s root dir in the ec2 instance using

RAILS_ENV=production rails s

gives me an error which suggests that my environment variables aren’t loading

`validate_options’: Missing required arguments: aws_access_key_id, aws_secret_access_key (ArgumentError)

I have this in my ~/.bashrc

export S3_KEY="XXXXXXXXXXX"
export S3_SECRET="XXXXXXXXX"
export S3_REGION="XXXXX"
export S3_BUCKET="XXXXX"

Doing source ~/.bashrc
makes the RAILS_ENV=production rails s command work
and I can curl localhost:3000 to see my homepage html

all the commands have been run on the server after ssh-ing into it.

Shouldn’t these environment variables be loaded automatically when I connect to the server using ssh?
Am I correct in assuming that since they don’t seem to be loaded unless I manually source ~/.bashrc, my web server will not have access to them either?

solved 0
2 Answer 18 views 0

Answers ( 2 )

    0
    December 21, 2016 at 2:10 pm

    Please briefly explain why you feel this answer should be reported .

    Report Cancel

    If you don’t find a way to make it work, I’d recommend you to use dotenv. It’s very easy to configure.

    In your gemfile:

    gem 'dotenv-rails', :groups => [:development, :test]
    

    In database.yml:

    username: <%= ENV['DB_USER'] %>
    password: <%= ENV['DB_PASS'] %>
    etc ...
    

    Then you just create in the main folder a file named .env with your configuration:

    DB_USER = XXXX
    DB_PASS = XXXX
    etc ...
    

    There is also something optional to add in application.rb.

    Full documentation:
    https://github.com/bkeepers/dotenv

    An useful article:
    http://blog.honeybadger.io/ruby-guide-environment-variables/

    Best answer
    0
    December 21, 2016 at 2:10 pm

    Please briefly explain why you feel this answer should be reported .

    Report Cancel

    Figured it out.

    # if running bash
    if [ -n "$BASH_VERSION" ]; then
        # include .bashrc if it exists
        if [ -f "$HOME/.bashrc" ]; then
            . "$HOME/.bashrc"
        fi
    fi
    

    This bit was missing in ~/.profile on this newly setup server for some reason. Usually this is there by default.

    This was causing my ~/.bashrc to not load up at all.

Leave an answer

Browse

What is the capital of Egypt ? ( Cairo )

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>