[アップデート] Ruby3.1と3.2をサポートしたCodeDeploy Agent 1.6.0がリリースされました

Amazon Linux 2023でもCodeDeploy Agentが使えるようになりました
2023.04.30

Amazon Linux 2023にCodeDeploy Agentをインストールしたいな

こんにちは、のんピ(@non____97)です。

皆さんはAmazon Linux 2023にCodeDeploy Agentをインストールしたいなと思ったことはありますか? 私はあります。

Amazon Linux 2023にデフォルトでインストールされているRubyのバージョンは3.2です。しかし、CodeDeploy Agentの1.4.1以前はRubyの2系を使用していました。

「じゃあAmazon Linux 2023にRuby 2.7をインストールすれば良いじゃないか」と思われるかもしれませんが、Ruby 2.7のEOLは2023/3/31です。

じゃあ、どうすれば... と思っていると、2023/3/30にRuby3.1と3.2をサポートしたCodeDeploy Agent 1.6.0がリリースされました。

CodeDeploy Agentのインストールスクリプトを見ると、確かにRuby 3.2をサポートしていることが分かります。

aws-codedeploy-agent/bin/install

  def supported_ruby_versions
    ['3.2','3.1','3.0', '2.7', '2.6', '2.5', '2.4', '2.3', '2.2', '2.1', '2.0']
  end

これによりAmazon Linux 2023に心置きなくインストールできるようになりました。また、1.6.0からはWindows Server 2022もサポートするようになったようです。

実際に試してみたので紹介します。

やってみた

AWS CDKによるリソースのデプロイ

EC2インスタンスへのCodeDeploy AgentのインストールやCodeDeployなどの設定はAWS CDKで行います。

使用したコードは以下リポジトリに保存しています。

cdk deploy後、EC2インスタンスのユーザーデータのログを確認します。

$ cat /var/log/user-data.log
+ sudo dnf install ruby -y
Amazon Linux 2023 repository                     20 MB/s |  13 MB     00:00
Amazon Linux 2023 Kernel Livepatch repository   339 kB/s | 155 kB     00:00
Dependencies resolved.
================================================================================
 Package                    Arch   Version                    Repository   Size
================================================================================
Installing:
 ruby3.2                    x86_64 3.2.2-180.amzn2023.0.1     amazonlinux  45 k
Installing dependencies:
 ruby3.2-default-gems       noarch 3.2.2-180.amzn2023.0.1     amazonlinux  36 k
 ruby3.2-libs               x86_64 3.2.2-180.amzn2023.0.1     amazonlinux 4.0 M
 ruby3.2-rubygem-io-console x86_64 0.6.0-180.amzn2023.0.1     amazonlinux  28 k
 ruby3.2-rubygem-json       x86_64 2.6.3-180.amzn2023.0.1     amazonlinux  56 k
 ruby3.2-rubygem-psych      x86_64 5.0.1-180.amzn2023.0.1     amazonlinux  54 k
Installing weak dependencies:
 ruby3.2-rubygem-bigdecimal x86_64 3.1.3-180.amzn2023.0.1     amazonlinux  71 k
 ruby3.2-rubygem-bundler    noarch 2.4.10-180.amzn2023.0.1    amazonlinux 387 k
 ruby3.2-rubygem-rdoc       noarch 6.5.0-180.amzn2023.0.1     amazonlinux 463 k
 ruby3.2-rubygems           noarch 3.4.10-180.amzn2023.0.1    amazonlinux 259 k

Transaction Summary
================================================================================
Install  10 Packages

Total download size: 5.3 M
Installed size: 19 M
Downloading Packages:
(1/10): ruby3.2-rubygem-psych-5.0.1-180.amzn202 441 kB/s |  54 kB     00:00
(2/10): ruby3.2-3.2.2-180.amzn2023.0.1.x86_64.r 973 kB/s |  45 kB     00:00
(3/10): ruby3.2-rubygem-bigdecimal-3.1.3-180.am 381 kB/s |  71 kB     00:00
(4/10): ruby3.2-rubygem-json-2.6.3-180.amzn2023 1.7 MB/s |  56 kB     00:00
(5/10): ruby3.2-rubygem-io-console-0.6.0-180.am 803 kB/s |  28 kB     00:00
(6/10): ruby3.2-default-gems-3.2.2-180.amzn2023 1.0 MB/s |  36 kB     00:00
(7/10): ruby3.2-libs-3.2.2-180.amzn2023.0.1.x86  13 MB/s | 4.0 MB     00:00
(8/10): ruby3.2-rubygem-rdoc-6.5.0-180.amzn2023 5.9 MB/s | 463 kB     00:00
(9/10): ruby3.2-rubygems-3.4.10-180.amzn2023.0. 2.5 MB/s | 259 kB     00:00
(10/10): ruby3.2-rubygem-bundler-2.4.10-180.amz 3.5 MB/s | 387 kB     00:00
--------------------------------------------------------------------------------
Total                                           9.5 MB/s | 5.3 MB     00:00
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                        1/1
  Installing       : ruby3.2-libs-3.2.2-180.amzn2023.0.1.x86_64            1/10
  Installing       : ruby3.2-rubygem-bigdecimal-3.1.3-180.amzn2023.0.1.    2/10
  Installing       : ruby3.2-3.2.2-180.amzn2023.0.1.x86_64                 3/10
  Running scriptlet: ruby3.2-3.2.2-180.amzn2023.0.1.x86_64                 3/10
  Installing       : ruby3.2-rubygem-json-2.6.3-180.amzn2023.0.1.x86_64    4/10
  Installing       : ruby3.2-rubygem-io-console-0.6.0-180.amzn2023.0.1.    5/10
  Installing       : ruby3.2-default-gems-3.2.2-180.amzn2023.0.1.noarch    6/10
  Installing       : ruby3.2-rubygem-bundler-2.4.10-180.amzn2023.0.1.no    7/10
  Running scriptlet: ruby3.2-rubygem-bundler-2.4.10-180.amzn2023.0.1.no    7/10
================================================================================

Bundler and $GEM_HOME

To get started with Bundler, you may need to restart your current shell or run:

source /etc/profile.d/bundler.sh

For non-root users, this sets the GEM_HOME environment variable which specifies
the default directory where RubyGems outside of the system path are installed.

By default, $GEM_HOME will point to ~/.local/share/gem/ruby

================================================================================

  Installing       : ruby3.2-rubygems-3.4.10-180.amzn2023.0.1.noarch       8/10
  Installing       : ruby3.2-rubygem-psych-5.0.1-180.amzn2023.0.1.x86_6    9/10
  Installing       : ruby3.2-rubygem-rdoc-6.5.0-180.amzn2023.0.1.noarch   10/10
  Running scriptlet: ruby3.2-rubygem-bundler-2.4.10-180.amzn2023.0.1.no   10/10
  Running scriptlet: ruby3.2-rubygems-3.4.10-180.amzn2023.0.1.noarch      10/10
  Running scriptlet: ruby3.2-rubygem-rdoc-6.5.0-180.amzn2023.0.1.noarch   10/10
  Verifying        : ruby3.2-rubygem-psych-5.0.1-180.amzn2023.0.1.x86_6    1/10
  Verifying        : ruby3.2-rubygem-bigdecimal-3.1.3-180.amzn2023.0.1.    2/10
  Verifying        : ruby3.2-libs-3.2.2-180.amzn2023.0.1.x86_64            3/10
  Verifying        : ruby3.2-3.2.2-180.amzn2023.0.1.x86_64                 4/10
  Verifying        : ruby3.2-rubygem-json-2.6.3-180.amzn2023.0.1.x86_64    5/10
  Verifying        : ruby3.2-rubygem-io-console-0.6.0-180.amzn2023.0.1.    6/10
  Verifying        : ruby3.2-default-gems-3.2.2-180.amzn2023.0.1.noarch    7/10
  Verifying        : ruby3.2-rubygems-3.4.10-180.amzn2023.0.1.noarch       8/10
  Verifying        : ruby3.2-rubygem-rdoc-6.5.0-180.amzn2023.0.1.noarch    9/10
  Verifying        : ruby3.2-rubygem-bundler-2.4.10-180.amzn2023.0.1.no   10/10

Installed:
  ruby3.2-3.2.2-180.amzn2023.0.1.x86_64
  ruby3.2-default-gems-3.2.2-180.amzn2023.0.1.noarch
  ruby3.2-libs-3.2.2-180.amzn2023.0.1.x86_64
  ruby3.2-rubygem-bigdecimal-3.1.3-180.amzn2023.0.1.x86_64
  ruby3.2-rubygem-bundler-2.4.10-180.amzn2023.0.1.noarch
  ruby3.2-rubygem-io-console-0.6.0-180.amzn2023.0.1.x86_64
  ruby3.2-rubygem-json-2.6.3-180.amzn2023.0.1.x86_64
  ruby3.2-rubygem-psych-5.0.1-180.amzn2023.0.1.x86_64
  ruby3.2-rubygem-rdoc-6.5.0-180.amzn2023.0.1.noarch
  ruby3.2-rubygems-3.4.10-180.amzn2023.0.1.noarch

Complete!
+ cd /home/ec2-user
+ wget https://aws-codedeploy-us-east-1.s3.us-east-1.amazonaws.com/latest/install
--2023-04-30 06:15:04--  https://aws-codedeploy-us-east-1.s3.us-east-1.amazonaws.com/latest/install
Resolving aws-codedeploy-us-east-1.s3.us-east-1.amazonaws.com (aws-codedeploy-us-east-1.s3.us-east-1.amazonaws.com)... 52.216.82.173, 52.217.135.154, 54.231.232.42, ...
Connecting to aws-codedeploy-us-east-1.s3.us-east-1.amazonaws.com (aws-codedeploy-us-east-1.s3.us-east-1.amazonaws.com)|52.216.82.173|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 17892 (17K) []
Saving to: ‘install’

     0K .......... .......                                    100% 2.12M=0.008s

2023-04-30 06:15:05 (2.12 MB/s) - ‘install’ saved [17892/17892]

+ chmod +x ./install
+ ./install auto
+ systemctl status codedeploy-agent
● codedeploy-agent.service - AWS CodeDeploy Host Agent
     Loaded: loaded (/usr/lib/systemd/system/codedeploy-agent.service; enabled; preset: disabled)
     Active: active (running) since Sun 2023-04-30 06:15:11 UTC; 1s ago
   Main PID: 2117 (ruby)
      Tasks: 2 (limit: 1055)
     Memory: 62.8M
        CPU: 1.396s
     CGroup: /system.slice/codedeploy-agent.service
             ├─2117 "codedeploy-agent: master 2117"
             └─2122 "codedeploy-agent: booting child"

Apr 30 06:15:10 ip-10-1-1-26.ec2.internal systemd[1]: Starting codedeploy-agent.service - AWS CodeDeploy Host Agent...
Apr 30 06:15:11 ip-10-1-1-26.ec2.internal bash[1990]: /opt/codedeploy-agent/vendor/gems/gli-2.11.0/lib/gli/commands/help_modules/global_help_format.rb:37:warning: Passing safe_level with the 2nd argument of ERB.new is deprecated. Do not use it, and specify other arguments as keyword arguments.
Apr 30 06:15:11 ip-10-1-1-26.ec2.internal bash[1990]: /opt/codedeploy-agent/vendor/gems/gli-2.11.0/lib/gli/commands/help_modules/global_help_format.rb:37:warning: Passing trim_mode with the 3rd argument of ERB.new is deprecated. Use keyword argument like ERB.new(str, trim_mode: ...) instead.
Apr 30 06:15:11 ip-10-1-1-26.ec2.internal bash[1990]: /opt/codedeploy-agent/vendor/gems/gli-2.11.0/lib/gli/commands/help_modules/command_help_format.rb:27: warning: Passing safe_level with the 2nd argument of ERB.new is deprecated. Do not use it, and specify other arguments as keyword arguments.
Apr 30 06:15:11 ip-10-1-1-26.ec2.internal bash[1990]: /opt/codedeploy-agent/vendor/gems/gli-2.11.0/lib/gli/commands/help_modules/command_help_format.rb:27: warning: Passing trim_mode with the 3rd argument of ERB.new is deprecated. Use keyword argument like ERB.new(str, trim_mode: ...) instead.
Apr 30 06:15:11 ip-10-1-1-26.ec2.internal systemd[1]: Started codedeploy-agent.service - AWS CodeDeploy Host Agent.
+ cat /opt/codedeploy-agent/.version
agent_version: OFFICIAL_1.6.0-49_rpm+ sudo dnf install httpd -y
Last metadata expiration check: 0:00:16 ago on Sun Apr 30 06:14:56 2023.
Dependencies resolved.
================================================================================
 Package               Arch     Version                     Repository     Size
================================================================================
Installing:
 httpd                 x86_64   2.4.56-1.amzn2023           amazonlinux    48 k
Installing dependencies:
 apr                   x86_64   1.7.2-2.amzn2023.0.2        amazonlinux   129 k
 apr-util              x86_64   1.6.3-1.amzn2023.0.1        amazonlinux    98 k
 generic-logos-httpd   noarch   18.0.0-12.amzn2023.0.3      amazonlinux    19 k
 httpd-core            x86_64   2.4.56-1.amzn2023           amazonlinux   1.4 M
 httpd-filesystem      noarch   2.4.56-1.amzn2023           amazonlinux    15 k
 httpd-tools           x86_64   2.4.56-1.amzn2023           amazonlinux    82 k
 libbrotli             x86_64   1.0.9-4.amzn2023.0.2        amazonlinux   315 k
 mailcap               noarch   2.1.49-3.amzn2023.0.3       amazonlinux    33 k
Installing weak dependencies:
 apr-util-openssl      x86_64   1.6.3-1.amzn2023.0.1        amazonlinux    17 k
 mod_http2             x86_64   2.0.11-2.amzn2023           amazonlinux   150 k
 mod_lua               x86_64   2.4.56-1.amzn2023           amazonlinux    62 k

Transaction Summary
================================================================================
Install  12 Packages

Total download size: 2.3 M
Installed size: 6.9 M
Downloading Packages:
(1/12): httpd-tools-2.4.56-1.amzn2023.x86_64.rp 887 kB/s |  82 kB     00:00
(2/12): mod_lua-2.4.56-1.amzn2023.x86_64.rpm    642 kB/s |  62 kB     00:00
(3/12): apr-util-1.6.3-1.amzn2023.0.1.x86_64.rp 5.7 MB/s |  98 kB     00:00
(4/12): mod_http2-2.0.11-2.amzn2023.x86_64.rpm  1.3 MB/s | 150 kB     00:00
(5/12): apr-util-openssl-1.6.3-1.amzn2023.0.1.x 721 kB/s |  17 kB     00:00
(6/12): apr-1.7.2-2.amzn2023.0.2.x86_64.rpm     6.8 MB/s | 129 kB     00:00
(7/12): httpd-2.4.56-1.amzn2023.x86_64.rpm      2.6 MB/s |  48 kB     00:00
(8/12): httpd-core-2.4.56-1.amzn2023.x86_64.rpm  42 MB/s | 1.4 MB     00:00
(9/12): libbrotli-1.0.9-4.amzn2023.0.2.x86_64.r  10 MB/s | 315 kB     00:00
(10/12): httpd-filesystem-2.4.56-1.amzn2023.noa 481 kB/s |  15 kB     00:00
(11/12): generic-logos-httpd-18.0.0-12.amzn2023 1.4 MB/s |  19 kB     00:00
(12/12): mailcap-2.1.49-3.amzn2023.0.3.noarch.r 2.4 MB/s |  33 kB     00:00
--------------------------------------------------------------------------------
Total                                           7.5 MB/s | 2.3 MB     00:00
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                        1/1
  Installing       : apr-1.7.2-2.amzn2023.0.2.x86_64                       1/12
  Installing       : apr-util-openssl-1.6.3-1.amzn2023.0.1.x86_64          2/12
  Installing       : apr-util-1.6.3-1.amzn2023.0.1.x86_64                  3/12
  Installing       : mailcap-2.1.49-3.amzn2023.0.3.noarch                  4/12
  Installing       : httpd-tools-2.4.56-1.amzn2023.x86_64                  5/12
  Installing       : generic-logos-httpd-18.0.0-12.amzn2023.0.3.noarch     6/12
  Running scriptlet: httpd-filesystem-2.4.56-1.amzn2023.noarch             7/12
  Installing       : httpd-filesystem-2.4.56-1.amzn2023.noarch             7/12
  Installing       : httpd-core-2.4.56-1.amzn2023.x86_64                   8/12
  Installing       : mod_lua-2.4.56-1.amzn2023.x86_64                      9/12
  Installing       : mod_http2-2.0.11-2.amzn2023.x86_64                   10/12
  Installing       : libbrotli-1.0.9-4.amzn2023.0.2.x86_64                11/12
  Installing       : httpd-2.4.56-1.amzn2023.x86_64                       12/12
  Running scriptlet: httpd-2.4.56-1.amzn2023.x86_64                       12/12
  Verifying        : mod_lua-2.4.56-1.amzn2023.x86_64                      1/12
  Verifying        : httpd-tools-2.4.56-1.amzn2023.x86_64                  2/12
  Verifying        : mod_http2-2.0.11-2.amzn2023.x86_64                    3/12
  Verifying        : apr-util-1.6.3-1.amzn2023.0.1.x86_64                  4/12
  Verifying        : apr-util-openssl-1.6.3-1.amzn2023.0.1.x86_64          5/12
  Verifying        : apr-1.7.2-2.amzn2023.0.2.x86_64                       6/12
  Verifying        : httpd-2.4.56-1.amzn2023.x86_64                        7/12
  Verifying        : httpd-core-2.4.56-1.amzn2023.x86_64                   8/12
  Verifying        : libbrotli-1.0.9-4.amzn2023.0.2.x86_64                 9/12
  Verifying        : httpd-filesystem-2.4.56-1.amzn2023.noarch            10/12
  Verifying        : generic-logos-httpd-18.0.0-12.amzn2023.0.3.noarch    11/12
  Verifying        : mailcap-2.1.49-3.amzn2023.0.3.noarch                 12/12

Installed:
  apr-1.7.2-2.amzn2023.0.2.x86_64
  apr-util-1.6.3-1.amzn2023.0.1.x86_64
  apr-util-openssl-1.6.3-1.amzn2023.0.1.x86_64
  generic-logos-httpd-18.0.0-12.amzn2023.0.3.noarch
  httpd-2.4.56-1.amzn2023.x86_64
  httpd-core-2.4.56-1.amzn2023.x86_64
  httpd-filesystem-2.4.56-1.amzn2023.noarch
  httpd-tools-2.4.56-1.amzn2023.x86_64
  libbrotli-1.0.9-4.amzn2023.0.2.x86_64
  mailcap-2.1.49-3.amzn2023.0.3.noarch
  mod_http2-2.0.11-2.amzn2023.x86_64
  mod_lua-2.4.56-1.amzn2023.x86_64

Complete!
+ sudo systemctl start httpd
+ sudo systemctl enable httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.

CodeDeploy Agentが起動しており、CodeDeploy Agentのバージョンが1.6.0であることが分かります。

CodeDeployを使ったコードのデプロイ

せっかくなので、CodeDeployを使ったコードのデプロイを行います。

CodeDeployのコンソールからAWS CDKで作成したデプロイグループを選択し、デプロイの作成をクリックします。

デプロイの作成

リビジョンの場所にCodeDeployのサンプルアプリケーションであるs3://aws-codedeploy-us-east-1/samples/latest/SampleApp_Linux.zipを入力してデプロイの作成をクリックします。

デプロイ

デプロイのステータスが成功になったことを確認します。

デプロイの完了

サンプルアプリケーションはindex.htmlを配置して、httpdを起動をするだけの非常にシンプルなものなので、一瞬で完了しました。

デプロイの詳細

EC2インスタンス内からもログを確認しておきます。

$ cat /opt/codedeploy-agent/deployment-root/deployment-logs/codedeploy-agent-deployments.log
# Logfile created on 2023-04-30 06:22:01 +0000 by logger.rb/v1.5.3
[2023-04-30 06:22:01.852] [d-CQ75XX34O]LifecycleEvent - BeforeInstall
[2023-04-30 06:22:01.852] [d-CQ75XX34O]Script - scripts/install_dependencies
[2023-04-30 06:22:02.973] [d-CQ75XX34O][stdout]Last metadata expiration check: 0:07:06 ago on Sun Apr 30 06:14:56 2023.
[2023-04-30 06:22:03.014] [d-CQ75XX34O][stdout]Package httpd-2.4.56-1.amzn2023.x86_64 is already installed.
[2023-04-30 06:22:03.038] [d-CQ75XX34O][stdout]Dependencies resolved.
[2023-04-30 06:22:03.044] [d-CQ75XX34O][stdout]Nothing to do.
[2023-04-30 06:22:03.044] [d-CQ75XX34O][stdout]Complete!
[2023-04-30 06:22:03.194] [d-CQ75XX34O]Script - scripts/start_server
[2023-04-30 06:22:03.287] [d-CQ75XX34O][stderr]Redirecting to /bin/systemctl start httpd.service

動作確認として、EC2インスタンスにHTTPでアクセスします。

$ curl ec2-54-224-222-101.compute-1.amazonaws.com
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Sample Deployment</title>
  <style>
    body {
      color: #ffffff;
      background-color: #0188cc;
      font-family: Arial, sans-serif;
      font-size: 14px;
    }

    h1 {
      font-size: 500%;
      font-weight: normal;
      margin-bottom: 0;
    }

    h2 {
      font-size: 200%;
      font-weight: normal;
      margin-bottom: 0;
    }
  </style>
</head>
<body>
  <div align="center">
    <h1>Congratulations</h1>
    <h2>This application was deployed using AWS CodeDeploy.</h2>
    <p>For next steps, read the <a href="http://aws.amazon.com/documentation/codedeploy">AWS CodeDeploy Documentation</a>.</p>
  </div>
</body>
</html>

CodeDeployのサンプルアプリケーションのHTMLが返ってきました。

Amazon Linux 2023でもCodeDeploy Agentが使えるようになりました

Ruby3.1と3.2をサポートしたCodeDeploy Agent 1.6.0のリリースを紹介しました。

これで「Amazon Linux 2023を使いたいのにCodeDeploy AgentがRuby 3.2をサポートしていなくて使えない」というのは回避できますね。

なお、CodeDeploy Agent 1.6.0からはWindows Server 2016とWindows Server 2012 R2はサポートしなくなりました。こちらのOSを使っている場合は、CodeDeploy Agent 1.5.0をインストールするか、Windows Server 2019以降にアップデートしましょう。

この記事が誰かの助けになれば幸いです。

以上、AWS事業本部 コンサルティング部の のんピ(@non____97)でした!