47

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"

2

13 Answers 13

60

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

2
  • 3
    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
    Feb 3, 2015 at 1:10
  • see my answer: it should solve all problems: stackoverflow.com/a/60585463/3090068
    – Yuki Inoue
    Mar 8, 2020 at 7:45
45

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

sudo su
bundle exec rails c production
3
  • 4
    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.
    – Adam
    Jun 27, 2016 at 11:45
  • 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
    – Yuki Inoue
    Mar 8, 2020 at 7:47
  • sudo su uses 2 commands. You can just use sudo -ithat runs login shell. Jan 20, 2022 at 14:21
14

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.

0
9

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>
3
  • 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.
    – Ka Mok
    Jan 23, 2018 at 4:08
  • @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.
    – Jeremiah
    Jan 24, 2018 at 4:16
  • You don't need myinstancename if you're cd into the directory.
    – Ka Mok
    Jan 24, 2018 at 17:12
8

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
1
  • 1
    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
    Nov 2, 2021 at 15:35
4

Create a .ebextension file named setvars.config and add those lines to it

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

Then deploy your code again it should work. reference: https://aws.amazon.com/ar/premiumsupport/knowledge-center/elastic-beanstalk-env-variables-shell/

1
  • This is the only thing that worked for me with a Rails 7 app on Elastic Beanstalk Jul 19, 2022 at 16:05
3
#!/bin/sh

shell_join () {
  ruby -r shellwords -e 'puts Shellwords.join(ARGV)' "$@"
}


command_str () {
  printf 'set -e; . /etc/profile.d/eb_envvars.sh; . /etc/profile.d/use-app-ruby.sh; set -x; exec %s\n' "$(shell_join "$@")"
}

exec sudo su webapp -c "$(command_str "$@")"

Put above file somewhere in your source code, deploy, eb ssh into the eb instance, cd /var/app/current, and then execute path/to/above/script bin/rails whatever argumeents you usually use.

Reason why I have written above script is:

  1. When using sudo, it drops some environment variables which might actually be needed for your rails app; so manually load the profiles which the Elastic Beanstalk platform provides.
  2. Current Beanstalk ruby platform assumes you run rails application on user webapp, a non-login-able user, so it would be wise to run your command in this user.
6
  • How do I use this to run my rake tasks as webapp? As it is, it seems to run as root.
    – ptoinson
    Apr 27, 2020 at 16:17
  • @ptoinson I forgot to add to specify the user for su command.. Just edited and added webapp to su argument.
    – Yuki Inoue
    May 2, 2020 at 5:39
  • 1
    > /etc/profile.d/eb_envvars.sh: No such file or directory Do you have an updated version ? maybe based on /opt/elasticbeanstalk/deployment/env ?
    – systho
    Aug 19, 2022 at 10:16
  • This answer was written in 2020. I think Elastic Beanstalk (platform) has been updated and maybe this answer is no longer valid.
    – Yuki Inoue
    Aug 21, 2022 at 7:34
  • 1
    Sorry, currently I don't work on projects which use Elastic Beanstalk; so, no, I don't have updated script.
    – Yuki Inoue
    Aug 24, 2022 at 2:12
3

For Ruby 2.7:

As someone said, if you don't need env vars, run the following

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

However, if you need ENV, I recommend doing this as per AWS doc: https://aws.amazon.com/premiumsupport/knowledge-center/elastic-beanstalk-env-variables-linux2/

tl;dr

On Amazon Linux 2, all environment properties are centralised into a single file called /opt/elasticbeanstalk/deployment/env. No user can access these outside the app. So, they recommend to add some hook scripts after deploy to basically create a copy.

#!/bin/bash

#Create a copy of the environment variable file.
cp /opt/elasticbeanstalk/deployment/env /opt/elasticbeanstalk/deployment/custom_env_var

#Set permissions to the custom_env_var file so this file can be accessed by any user on the instance. You can restrict permissions as per your requirements.
chmod 644 /opt/elasticbeanstalk/deployment/custom_env_var

#Remove duplicate files upon deployment.
rm -f /opt/elasticbeanstalk/deployment/*.bak

If because of some reason you don't want to run as root, do the following to pass env vars from root into new user environment:

sudo -u <user> -E env "PATH=$PATH" bash -c 'cd /var/app/current/ && <wtv you want to run>'
1
  • 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
    Nov 2, 2021 at 15:36
2

I like to create an eb_console file at the root of my rails app, then chmod u+x it. It contains the following:

ssh -t ec2-user@YOUR_EC2_STATION.compute.amazonaws.com  'cd /var/app/current && bin/rails c'

This way, I just have to run:

./eb_console

like I would have run heroku run bundle exec rails c.

1

For the latest ruby version, please use the following command:

BUNDLE_PATH=/opt/rubies/ruby-2.6.3/lib/ruby/gems/2.6.0/ bundle exec rails c production

Running it with sudo is not needed.

0

add an eb extension shortcut:

# .ebextensions/irb.config
files:
  "/home/ec2-user/irb":
    mode: "000777"
    owner: root
    group: root
    content: |
      sudo su - -c 'cd /var/app/current; bundle exec rails c'

then:

$ eb ssh
$ ./irb
irb(main):001:0>
0
-1

None of these were working for me, including the aws-console script. I finally ended up creating a script directory in /var/app/current and then creating a rails file in that directory as outline by this answer on another SO question.

eb ssh myEnv
cd /var/app/current
sudo mkdir script
sudo vim script/rails

Add this to file and save:

echo #!/usr/bin/env ruby
# This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.

APP_PATH = File.expand_path('../../config/application',  __FILE__)
require File.expand_path('../../config/boot',  __FILE__)
require 'rails/commands'

Then make it executable and run it:

sudo chmod +x script/rails
sudo script/rails console

And it worked.

-2

You have to find the folder with your Gemfile :p.

To do that, I would take a look in you web server config there should be a config that tells you where your app directory is.

Maybe you know where your app is.

But in case you don't know, I would give a try to:

grep -i your_app_name /etc/apache/*
grep -i your_app_name /etc/apache/sites-enabled/*

To search files containing your_app_name in Apache config.

Or if you are using nginx, replace apache above by nginx.

after you find application folder, cd into it and run RAILS_ENV=production bundle exec rails c.

Making sure that your application is configured to run in production in Apache or nginx configuration.

3
  • 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.
    – gitb
    Oct 30, 2013 at 15:28
  • usually, I use passenger on top of nginx or Apache... so Apache or nginx should be there... weird...
    – sites
    Oct 30, 2013 at 19:46
  • In Beanstalk, the gemfile is in /var/app/current. The $EB_CONFIG_APP_CURRENT environment variable points there.
    – Mark Berry
    Feb 3, 2015 at 1:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.