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

后端 未结 11 1023
一整个雨季
一整个雨季 2020-12-12 21:03

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 effective

相关标签:
11条回答
  • 2020-12-12 21:07

    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
    
    0 讨论(0)
  • 2020-12-12 21:09

    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 讨论(0)
  • 2020-12-12 21:14

    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.

    0 讨论(0)
  • 2020-12-12 21:15

    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.

    0 讨论(0)
  • 2020-12-12 21:22

    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 讨论(0)
  • 2020-12-12 21:24

    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>
    
    0 讨论(0)
提交回复
热议问题