Can you run a rails console or rake command in the elastic beanstalk environment?
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
Related videos on Youtube
gitb
Updated on January 13, 2022Comments
-
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
"-
김진언 over 8 yearsfinally... I found the solution. look at this gist.github.com/tarom/e99fa97c3916c6717baa it worked for me.
-
-
gitb over 10 yearsThere 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 over 10 yearsusually, I use passenger on top of nginx or Apache... so Apache or nginx should be there... weird...
-
Mark Berry over 9 years
cd $EB_CONFIG_APP_CURRENT
goes to /var/app/current.sudo RAILS_ENV=production bundle exec rails c
givesbundler: command not found: rails
. If I specify the path to rails,sudo RAILS_ENV=production bundle exec /usr/local/bin/rails c
, I getCould 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 over 9 yearsIn Beanstalk, the gemfile is in
/var/app/current
. The$EB_CONFIG_APP_CURRENT
environment variable points there. -
Zakir Jaafar almost 8 yearsYep, 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 over 6 yearsWhere 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 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 over 6 yearsYou don't need
myinstancename
if you'recd
into the directory. -
Yuki Inoue about 4 yearssee my answer: it should solve all problems: stackoverflow.com/a/60585463/3090068
-
Yuki Inoue about 4 yearsThis actually runs your rails in
root
user; but current Elastic Beanstalk platform assumes you run rails application inwebapp
user. So, I'd argue we should usewebapp
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 about 4 yearsHow do I use this to run my rake tasks as webapp? As it is, it seems to run as root.
-
Yuki Inoue about 4 years@ptoinson I forgot to add to specify the user for
su
command.. Just edited and addedwebapp
tosu
argument. -
Nuno Silva over 2 yearsFor 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 over 2 yearsFor 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 over 2 years
sudo su
uses 2 commands. You can just usesudo -i
that runs login shell.