Rails production console unable to retrieve env variables on AWS Elastic Beanstalk Amazon Linux 2

Viewed 810

I have been trying to access my production console on my ec2 instance as part of my elastic beanstalk environment. I am able to actually access the console, but when I query for an object, eg. "Wager.all", it gives me this error:

[root@ip-10-0-1-146 config]# bundle exec rails c -e production Loading production environment (Rails 5.2.4.2) irb(main):001:0> Wager.all Traceback (most recent call last): PG::ConnectionBad (fe_sendauth: no password supplied)

Here is my database.yml file (production configuration is the one to look at):

default: &default
  adapter: postgresql
  encoding: UTF-8
  host: 'localhost'
  
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 10 } %>

test:
  <<: *default

development:
  <<: *default
  url: <%= ENV['DATABASE_URL'] %>
  database: <%= ENV['RDS_DB_NAME'] %>
  username: <%= ENV['RDS_USERNAME'] %>
  password: <%= ENV['RDS_PASSWORD'] %>
  host: <%= ENV['RDS_HOSTNAME'] %>
  port: <%= ENV['RDS_PORT'] %>

production:
  <<: *default
  url: <%= ENV['DATABASE_URL'] %>
  database: <%= ENV['RDS_DB_NAME'] %>
  username: <%= ENV['RDS_USERNAME'] %>
  password: <%= ENV['RDS_PASSWORD'] %>
  host: database-1.c76xg56p4vkw.us-east-2.rds.amazonaws.com
  port: 5432

Before, I had the host and port option set to environment variables and it failed on that because it couldn't even connect to the host. Now that I have hardcoded that in, it fails on the part where it requires my password. I can get this to work by hardcoding everything, but I want to use env variables.

Even though I have set my environment variables in my elastic beanstalk console, it seems like the rails console is unable to retrieve them. However, it does appear that the application is accessing these variables during deployment, since it can connect to my database.

Things I've tried:

Running 'erb config/database.yml' to see if erb can recognize the values. It returns blank values for all the fields where I have something specified in <%= %>. So 'password: <%= ENV['RDS_PASSWORD']' %> would be output like 'password: '.

I've referred to this AWS doc to set up my database.yml file

Called AWS customer support, and we confirmed that the instance does seem to know about environment variables. This command '/opt/elasticbeanstalk/bin/get-config environment' outputs all the env variables correctly. However, the support associate did not know much about rails so he was unable to help me further.

ruby 2.5.8p224 (2020-03-31 revision 67882) [x86_64-linux]

1 Answers

I was running into this issue for a while as well. Turns out that Amazon Linux AMI platform versions preceding Amazon Linux 2 had sourced the environment variables into bash of users.

You can reinstate this process and allow yourself to enter the rails console by adding the following in an .ebextensions file:

commands:
  setvars:
    command: /opt/elasticbeanstalk/bin/get-config environment | jq -r 'to_entries | .[] | "export \(.key)=\"\(.value)\""' > /etc/profile.d/sh.local
packages:
  yum:
    jq: []

This will load your ENV variables into the bash of every user. You can then call these variables (like you could in previous AMI versions) by running the command echo $MY_ENV_VARIABLE

Related