Can you run a rails console or rake command in the elastic beanstalk environment?

24,237

Solution 1

For rails, jump to /var/app/current then as @juanpastas said, run RAILS_ENV=production bundle exec rails c

Solution 2

Don't know why, but since EBS runs everything as root, this worked for me:

sudo su
bundle exec rails c production

Solution 3

None of these solutions mentioned here worked for me, so I cooked up a little script that I put in script/aws-console.

You can run it from the /var/app/current directory as root:

eb ssh
cd /var/app/current
sudo script/aws-console

My script can be found as a Gist here.

Solution 4

None of the other answers worked for me so I went looking - this is working for me now on an elastic beanstalk 64bit amazon linux 2016.03 V2.1.2 ruby 2.2 (puma) stack

cd /var/app/current
sudo su
rake rails:update:bin
bundle exec rails console

that returns me the expected console

Loading production environment (Rails 4.2.6)
irb(main):001:0>

Solution 5

For Ruby 2.7:

if you don't need environment variables:

BUNDLE_PATH=/var/app/current/vendor/bundle/ bundle exec rails c

It looks like environment variables are not loaded automatically anymore, which might prevent rails console from starting. I solved it by creating this .ebextensions file:

# Simply call `sudo /var/app/scripts/rails_c`

commands:
  create_script_dir:
    command: "mkdir -p /var/app/scripts"
    ignoreErrors: true
files:
  "/var/app/scripts/export_envvars":
    mode: "000755"
    owner: root
    group: root
    content: |
      #!/opt/elasticbeanstalk/.rbenv/shims/ruby

      if __FILE__ == $0
          require 'json'
          env_file = '/var/app/scripts/envvars'
          env_vars = env_vars = JSON.parse(`/opt/elasticbeanstalk/bin/get-config environment`)

          str = ''
          env_vars.each do |key, value|
              new_key = key.gsub(/\s/, '_')
              str << "export #{new_key}=\"#{value}\"\n"
          end

          File.open(env_file, 'w') { |f| f.write(str) }
      end
  "/var/app/scripts/rails_c":
    mode: "000755"
    owner: root
    group: root
    content: |
      . ~/.bashrc
      /var/app/scripts/export_envvars
      . /var/app/scripts/envvars
      cd /var/app/current
      /opt/elasticbeanstalk/.rbenv/shims/bundle exec rails c
Share:
24,237

Related videos on Youtube

gitb
Author by

gitb

Updated on January 13, 2022

Comments

  • gitb
    gitb over 2 years

    I have set up a RoR environement on AWS' elastic beanstalk. I am able to ssh into my EC2 instance. My home directory is /home/ec2-user, which is effectively empty. If I move up a directory, there is also a /home/webapp directory that i do not have access to.

    Is there a way to run a rake command or rails console on my elastic beanstalk instance?

    If I type rails console I get Usage: rails new APP_PATH [options] If I type RAILS_ENV=production bundle exec rails console, I get "Could not locate Gemfile"

  • gitb
    gitb over 10 years
    There is no directory /etc/apache or /etc/nginx. As far as I can tell, elastic beanstalk uses passenger. And there was no /etc/passenger directory either.
  • sites
    sites over 10 years
    usually, I use passenger on top of nginx or Apache... so Apache or nginx should be there... weird...
  • Mark Berry
    Mark Berry over 9 years
    cd $EB_CONFIG_APP_CURRENT goes to /var/app/current. sudo RAILS_ENV=production bundle exec rails c gives bundler: command not found: rails. If I specify the path to rails, sudo RAILS_ENV=production bundle exec /usr/local/bin/rails c, I get Could not find addressable-2.3.6 in any of the sources. There is something strange going on with how Beanstalk and/or Passenger bundles gems, maybe related to the shared gems described in this answer.
  • Mark Berry
    Mark Berry over 9 years
    In Beanstalk, the gemfile is in /var/app/current. The $EB_CONFIG_APP_CURRENT environment variable points there.
  • Zakir Jaafar
    Zakir Jaafar almost 8 years
    Yep, this worked for me too. cd /var/app current; sudo su; bundle exec rails c Not sure why but production at the end wasn't needed.
  • Ka Mok
    Ka Mok over 6 years
    Where are you typing cd/var/app/current? Is it on your local machine? It says -bash: cd: /var/app/current: No such file or directory for me.
  • Jeremiah
    Jeremiah over 6 years
    @KaMok - nah, you need to open an ssh connection to your EB instance - something like eb ssh myinstancename - type 'yes' for the auth key and then you'll get dumped to a console where you can type the above.
  • Ka Mok
    Ka Mok over 6 years
    You don't need myinstancename if you're cd into the directory.
  • Yuki Inoue
    Yuki Inoue about 4 years
    see my answer: it should solve all problems: stackoverflow.com/a/60585463/3090068
  • Yuki Inoue
    Yuki Inoue about 4 years
    This actually runs your rails in root user; but current Elastic Beanstalk platform assumes you run rails application in webapp user. So, I'd argue we should use webapp for what ever command we are using, and doing so is explained in my answer see my answer: it should solve all problems: stackoverflow.com/a/60585463/3090068
  • ptoinson
    ptoinson about 4 years
    How do I use this to run my rake tasks as webapp? As it is, it seems to run as root.
  • Yuki Inoue
    Yuki Inoue about 4 years
    @ptoinson I forgot to add to specify the user for su command.. Just edited and added webapp to su argument.
  • Nuno Silva
    Nuno Silva over 2 years
    For those using dotenv, this is simpler: sudo cp -f /opt/elasticbeanstalk/deployment/env /var/app/current/.env cd /var/app/current && rails console
  • Nuno Silva
    Nuno Silva over 2 years
    For those using dotenv, this is simpler: sudo cp -f /opt/elasticbeanstalk/deployment/env /var/app/current/.env cd /var/app/current && rails console
  • Fernando Kosh
    Fernando Kosh over 2 years
    sudo su uses 2 commands. You can just use sudo -ithat runs login shell.