LoginSignup
3
2

More than 3 years have passed since last update.

Elastic Beanstalk + Railsで、can't find gem bundlerが発生する問題

Posted at

EC2インスタンスが初期化or再構築された際に、発生する

Railsアプリをデプロイする際、EBは/opt/elasticbeanstalk/hooks/appdeploy/pre/10_bundle_install.shを呼び出しbundle installを実行する。
この際、Bundlerのバージョンがデフォルトは1.16.0という古いものになっており(何で?)、最近のバージョンのRailsでは互換性がなくエラーが発生する。

ERROR: [Instance: i-###############] Command failed on instance. Return code: 1 Output: (TRUNCATED)...:infind_spec_for_exe': can't find gem bundler (>= 0.a) with executable bundle (Gem::GemNotFoundException) from /opt/rubies/ruby-2.5.5/lib/ruby/site_ruby/2.5.0/rubygems.rb:308:in activate_bin_path'
    from /opt/rubies/ruby-2.5.5/bin/bundle:23:in'. Hook /opt/elasticbeanstalk/hooks/appdeploy/pre/10_bundle_install.sh failed. For more detail, check /var/log/eb-activity.log using console or EB CLI. INFO: Command execution completed on all instances. Summary: [Successful: 0, Failed: 1].`

解決するには対応するバージョンのBundlerをインストールすればいい。
これまではeb sshgem install bundler -v 2.0.2を叩いてからデプロイしていた。

しかしインスタンスがAuto Scalingに設定されていると、何度でもEC2が再構築されてしまい、予期せぬときにこのエラーが発生しサーバーダウンしてしまう。
なので、デプロイ時の10_bundle_install.shが実行される前に、自動でBundlerをインストールしてもらうシェルスクリプトを作成する。
注意点は、10より先の09以下の番号をファイル名の先頭に付けること。シェルスクリプトはアルファベット順に実行される。

09_gem_install_bundler
 files:
  "/opt/elasticbeanstalk/hooks/appdeploy/pre/09_reinstall_bundler.sh" :
    mode: "000775"
    owner: root
    group: root
    content: |
      #! /bin/bash

      EB_APP_STAGING_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k app_staging_dir)
      EB_SCRIPT_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k script_dir)
      . $EB_SCRIPT_DIR/use-app-ruby.sh

      cd $EB_APP_STAGING_DIR
      echo "Installing compatible bundler"
      gem install bundler -v 2.0.2

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2