既存環境の設定を引き継いでElasticBeanstalkのプラットフォームのメジャーバージョンアップをする

Elastic Beanstalkプラットフォームのメジャーバージョンアップを既存の環境設定を再利用してやってみました。
2020.02.21

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

はじめに

全国20万人のElastic Beanstalkファンの皆様こんにちは!

Elastic Beasntalkでは2020年3月1日にいくつかのプラットフォームのサポート終了が予定されています。 この記事はEOLに伴ってElastic Beanstalkプラットフォームのメジャーバージョンアップをしてみた時のメモです。

Elastic Beanstalkにおけるプラットフォームのメジャーバージョンアップ

EOLになるプラットフォームから新しいバージョンへのアップグレードはメジャーバージョンアップになり、既存の環境とは別に新しい環境を作成する必要があります

既存の環境の設定を再利用して新しい環境を作成する

既存環境のバージョンアップなので新しい環境を作成する場合でも設定は再利用したいです。

設定を再利用するためには保存された設定(saved configuration)を使用します。

具体的には次のようなステップになります。

  1. 現在の環境の設定を保存する
  2. eb cliで設定ファイル(yaml)をローカルにコピーする
  3. ローカルの設定ファイルのプラットフォーム設定を新しいプラットフォームへ修正する
  4. 新たな設定から環境を起動する

続いて詳しくみていきます。

現在の環境の設定を保存する

環境の保存はeb cliから行います。コマンドを実行するとS3上に設定が保存され、実行したディレクトリの.elasticbeanstalk/saved_configs/ 以下にも設定ファイルが保存されます。マネジメントコンソールからもS3上に設定を保存することができますが、手元にコピーするときにはCLIを使用するためこのほうが手っ取り早いと思います。

> eb config save --cfg old-config
Configuration saved at: /Users/sasaki.kazuhiro/tmp/eb_blog/.elasticbeanstalk/saved_configs/old-config.cfg.yml

プラットフォーム設定を修正する

保存された設定ファイルをコピー、編集して新たなプラットフォーム設定を指定する設定ファイルを作成します。ファイルの差分は以下のようになります。この例では新しい設定ファイルをnew_config.cfg.ymlという名前で作成しました。設定ファイル名のサフィックスはcfg.ymlにする必要があります。

# Ruby 2.5 -> 2.6へアップデート
> diff .elasticbeanstalk/saved_configs/old-config.cfg.yml .elasticbeanstalk/saved_configs/new_config.cfg.yml
6c6
<   PlatformArn: arn:aws:elasticbeanstalk:ap-northeast-1::platform/Puma with Ruby 2.5 running on 64bit Amazon Linux/2.11.2
---
>   PlatformArn: arn:aws:elasticbeanstalk:ap-northeast-1::platform/Puma with Ruby 2.6 running on 64bit Amazon Linux/2.11.2

PlatformArn に指定する値はawscliで参照できます。

> aws elasticbeanstalk list-platform-versions | jq -r '.PlatformSummaryList[] | [.PlatformCategory,.PlatformArn] | @tsv' | grep Ruby
Ruby    arn:aws:elasticbeanstalk:ap-northeast-1::platform/Passenger with Ruby 1.9.3 running on 64bit Amazon Linux/2.11.2
Ruby    arn:aws:elasticbeanstalk:ap-northeast-1::platform/Passenger with Ruby 2.0 running on 64bit Amazon Linux/2.11.2
Ruby    arn:aws:elasticbeanstalk:ap-northeast-1::platform/Passenger with Ruby 2.1 running on 64bit Amazon Linux/2.11.2
Ruby    arn:aws:elasticbeanstalk:ap-northeast-1::platform/Passenger with Ruby 2.2 running on 64bit Amazon Linux/2.11.2
Ruby    arn:aws:elasticbeanstalk:ap-northeast-1::platform/Passenger with Ruby 2.3 running on 64bit Amazon Linux/2.11.2
Ruby    arn:aws:elasticbeanstalk:ap-northeast-1::platform/Passenger with Ruby 2.3 running on 64bit Amazon Linux/2.4.3
Ruby    arn:aws:elasticbeanstalk:ap-northeast-1::platform/Passenger with Ruby 2.4 running on 64bit Amazon Linux/2.11.2
Ruby    arn:aws:elasticbeanstalk:ap-northeast-1::platform/Passenger with Ruby 2.5 running on 64bit Amazon Linux/2.11.2
Ruby    arn:aws:elasticbeanstalk:ap-northeast-1::platform/Passenger with Ruby 2.6 running on 64bit Amazon Linux/2.11.2
Ruby    arn:aws:elasticbeanstalk:ap-northeast-1::platform/Puma with Ruby 2.0 running on 64bit Amazon Linux/2.11.2
Ruby    arn:aws:elasticbeanstalk:ap-northeast-1::platform/Puma with Ruby 2.1 running on 64bit Amazon Linux/2.11.2
Ruby    arn:aws:elasticbeanstalk:ap-northeast-1::platform/Puma with Ruby 2.2 running on 64bit Amazon Linux/2.11.2
Ruby    arn:aws:elasticbeanstalk:ap-northeast-1::platform/Puma with Ruby 2.3 running on 64bit Amazon Linux/2.11.2
Ruby    arn:aws:elasticbeanstalk:ap-northeast-1::platform/Puma with Ruby 2.3 running on 64bit Amazon Linux/2.4.3
Ruby    arn:aws:elasticbeanstalk:ap-northeast-1::platform/Puma with Ruby 2.3 running on 64bit Amazon Linux/2.4.2
Ruby    arn:aws:elasticbeanstalk:ap-northeast-1::platform/Puma with Ruby 2.4 running on 64bit Amazon Linux/2.11.2
Ruby    arn:aws:elasticbeanstalk:ap-northeast-1::platform/Puma with Ruby 2.5 running on 64bit Amazon Linux/2.11.2
Ruby    arn:aws:elasticbeanstalk:ap-northeast-1::platform/Puma with Ruby 2.6 running on 64bit Amazon Linux/2.11.2

設定を作成したらS3に保存します。

> eb config put new_config
> eb config list
new_config
old-config

新環境を作成する

保存した設定で新環境を作成します。新しいプラットフォームになっていることがわかります。

新環境の動作確認をしたらURLスワップでリリースしましょう!

> eb create new-platform-env --cfg new_config
NOTE: The current directory does not contain any source code. Elastic Beanstalk is launching the sample application instead.
Environment details for: new-platform-env
  Application name: eb-security-group
  Region: ap-northeast-1
  Deployed Version: Sample Application
  Environment ID: e-tkivtnsx9g
  Platform: arn:aws:elasticbeanstalk:ap-northeast-1::platform/Puma with Ruby 2.6 running on 64bit Amazon Linux/2.11.2
  Tier: WebServer-Standard-1.0
  CNAME: UNKNOWN
  Updated: 2020-02-21 09:11:21.598000+00:00
Printing Status:
2020-02-21 09:11:20    INFO    createEnvironment is starting.
2020-02-21 09:11:21    INFO    Using elasticbeanstalk-ap-northeast-1-331282901948 as Amazon S3 storage bucket for environment data.
2020-02-21 09:11:42    INFO    Created security group named: awseb-e-tkivtnsx9g-stack-AWSEBSecurityGroup-AWLO8DWAG2HM
2020-02-21 09:11:58    INFO    Created EIP: 52.193.77.56
2020-02-21 09:12:50    INFO    Waiting for EC2 instances to launch. This may take a few minutes.
2020-02-21 09:13:34    INFO    Application available at new-platform-env.ymp6fuayng.ap-northeast-1.elasticbeanstalk.com.
2020-02-21 09:13:36    INFO    Successfully launched environment: new-platform-env

まとめ

Elastic Beanstalkのプラットフォームのメジャーバージョンアップをするときには既存の環境の設定を再利用しましょう。(EOLまであと少しだけど頑張ろう)