LoginSignup
0

More than 3 years have passed since last update.

bundle install --deploymentでAWS Lambda向けデプロイパッケージを作っていて嵌った罠

Posted at

背景

AWS lambdaにRubyスクリプトを配置するデプロイパッケージを作るとき、Rubyスクリプトが依存するgemファイルをzipファイルに含めるため、bundle install --deploymentのように、--deploymentオプションを指定していました。

--deploymentオプションを指定するとgemはvendor/bundleディレクトリにインストールされます。
次のコマンドで、vendorディレクトリをデプロイパッケージに含めます。

zip deploy_package lambda_function.rb -FSr vendor

--deploymentオプションを使っているのは、AWSのデプロイスクリプトのサンプルにbundle install --deploymentが使われていた時があり、その名残です。現在ではbundle install --path vendor/bundleが使われています。
https://docs.aws.amazon.com/ja_jp/lambda/latest/dg/ruby-package.html

bundle install --path vendor/bundle を使っていれば、この先を読む必要はありません。

物語

PCにインストールしているbundlerのバージョンを2.1.4に上げました。

bundler 2.1.4では--deploymentオプションは非推奨になりました。
代わりにbundle config deployment trueコマンドで設定を変更します。
https://github.com/rubygems/bundler/pull/7519

CI環境で使うデプロイスクリプトもbundle config deployment trueに修正しました。デプロイが失敗するようになりました。
gemがvender/bundleディレクトリにインストールされなくなったようです。

原因

CI環境ではRuby 2.7を使っています。
Ruby 2.7に同梱されているbundlerのバージョンは2.1.2でした。

bundler 2.1.2 では bundle config deployment true を設定しても、gemのインストール先が変わりません。

対策

bundle config set path 'vendor/bundle' を設定して、gemのインストール先をvender/bundleにしました。

これでbundler 2.1.2でも2.1.4でもgemはvendor/bundleディレクトリにインストールされます。

その後

CI環境でbundler 2.1.4を使うようになり、1ヶ月半くらいでbundle config deployment trueに戻しました。

--pathオプションも非推奨なので、bundle install --path vendor/bundleにはしませんでした。

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
0