【アップデート】Amazon ECS-optimized Amazon Linux 2022 AMIがプレビューとして利用可能になりました

2022.04.29

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

コンサル部のとばち(@toda_kk)です。

Amazon ECS on EC2向けに最適化されたAMIであるAmazon ECS-optimized AMIについて、Amazon Linux 2022がプレビューとして利用可能になりました。

早速、Amazon Linux 2022を利用してAmazon ECS on EC2環境を構築してみました。

Amazon Linux 2022とは

Amazon Linux 2022は、AWSが提供するAmazon Linux 2の後継にあたるLinux OSです。2021年11月にプレビューが発表されました。

OSの定期的なリリースサイクルが計画されている他、Amazon Linux 2と比べて高いセキュリティ標準で設定されています。

詳細は下記ページをご参照ください。

ECS-optimized Amazon Linux 2022 AMIを使ってみる

最新のECS-optimized AMI IDは、パブリックに公開されているSystems Manager Parameter Storeに登録されています。Amazon Linux 2022の場合は、下記コマンドで取得できます。

aws ssm get-parameters --names /aws/service/ecs/optimized-ami/amazon-linux-2022/recommended

ちなみに、過去のものも含めたバージョン情報については、下記ページに記載されています。

上記コマンドを実行して取得したAMI IDを指定して、EC2インスタンスを作成します。

EC2インスタンスをECSクラスターのコンテナインスタンスとして登録する際の手順は、下記のAWS公式ドキュメントに記載されています。

EC2インスタンスをECSクラスターに登録するためには、IAMインスタンスプロファイルで権限を付与する必要があります。通常はecsInstanceRoleを指定すれば問題ありません。

また、ECS-optimized AMIでは設定ファイル/etc/ecs/ecs.configに項目を追加することでECS環境の設定値を指定できます。EC2インスタンスをECSクラスターに登録するためには、ECS_CLUSTERにECSクラスター名を指定する必要があります。

こうした初期化の作業は、通常はEC2インスタンスを作成する際にユーザーデータとして登録しておきます。

例えば、test-ecsというECSクラスターにEC2インスタンスを登録したい場合は、ユーザーデータに下記のスクリプトを登録します。

ユーザーデータ

#!/bin/bash
echo "ECS_CLUSTER=test-ecs" >> /etc/ecs/ecs.config

問題なくEC2インスタンスが起動されると、ECSクラスター側でコンテナインスタンスとして登録されていることが確認できます。

もしEC2インスタンスは起動しているもののECSクラスターに正常に登録されていない場合など、トラブルシューティングの調査の際は、/var/log/ecs/ecs-agent.log/var/log/ecs/ecs-init.logといったログファイルをご参照ください。

ECS-optimized Amazon Linux 2022 AMIの動作環境を確認してみる

実際に起動してみたECS-optimized Amazon Linux 2022の環境を確認してみます。

※2022年4月現在のプレビュー段階での確認となりますので、ご注意ください。

Kernelバージョン

[ec2-user@ip-10-1-0-133 ~]$ uname -a
Linux ip-10-1-0-133.ap-northeast-1.compute.internal 5.15.25-14.106.amzn2022.x86_64 #1 SMP Wed Mar 2 19:27:42 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

Linux Kernelのバージョンは5.15.25になっています。

SELinux

[ec2-user@ip-10-1-0-133 ~]$ getenforce
Enforcing

SELinuxがデフォルトで有効になっています。

Service一覧

[ec2-user@ip-10-1-0-133 ~]$ systemctl list-unit-files --type=service 
UNIT FILE                              STATE           VENDOR PRESET
acpid.service                          disabled        disabled
amazon-ecs-volume-plugin.service       enabled         disabled
amazon-efs-mount-watchdog.service      disabled        disabled
amazon-ssm-agent.service               enabled         disabled
arp-ethers.service                     disabled        disabled
auditd.service                         enabled         enabled
auth-rpcgss-module.service             static          -
autovt@.service                        alias           -
chrony-wait.service                    disabled        disabled
chronyd.service                        enabled         enabled
cloud-config.service                   enabled         disabled
cloud-final.service                    enabled         disabled
cloud-init-local.service               enabled         disabled
cloud-init.service                     enabled         disabled
console-getty.service                  disabled        disabled
container-getty@.service               static          -
containerd.service                     disabled        disabled
dbus-broker.service                    enabled         enabled
dbus-org.freedesktop.hostname1.service alias           -
dbus-org.freedesktop.locale1.service   alias           -
dbus-org.freedesktop.login1.service    alias           -
dbus-org.freedesktop.network1.service  alias           -
dbus-org.freedesktop.oom1.service      alias           -
dbus-org.freedesktop.portable1.service alias           -
dbus-org.freedesktop.resolve1.service  alias           -
dbus-org.freedesktop.timedate1.service alias           -
dbus.service                           alias           -
debug-shell.service                    disabled        disabled
dnf-makecache.service                  static          -
docker.service                         disabled        disabled
dracut-cmdline.service                 static          -
dracut-initqueue.service               static          -
dracut-mount.service                   static          -
dracut-pre-mount.service               static          -
dracut-pre-pivot.service               static          -
dracut-pre-trigger.service             static          -
dracut-pre-udev.service                static          -
dracut-shutdown.service                static          -
ecs.service                            enabled         disabled
emergency.service                      static          -
fstrim.service                         static          -
getty@.service                         enabled         enabled
grub-boot-indeterminate.service        static          -
grub2-systemd-integration.service      static          -
gssproxy.service                       disabled        disabled
import-state.service                   enabled         enabled
initrd-cleanup.service                 static          -
initrd-parse-etc.service               static          -
initrd-switch-root.service             static          -
initrd-udevadm-cleanup-db.service      static          -
irqbalance.service                     enabled         enabled
kmod-static-nodes.service              static          -
ldconfig.service                       static          -
loadmodules.service                    disabled        disabled
logrotate.service                      static          -
man-db-cache-update.service            static          -
man-db-restart-cache-update.service    disabled        disabled
modprobe@.service                      static          -
nfs-blkmap.service                     disabled        disabled
nfs-convert.service                    enabled         disabled
nfs-idmapd.service                     static          -
nfs-mountd.service                     static          -
nfs-server.service                     disabled        disabled
nfs-utils.service                      static          -
nfsdcld.service                        static          -
nis-domainname.service                 disabled        disabled
pam_namespace.service                  static          -
policy-routes@.service                 static          -
quotaon.service                        static          -
rc-local.service                       static          -
rdisc.service                          disabled        disabled
refresh-policy-routes@.service         static          -
rescue.service                         static          -
rngd.service                           enabled         enabled
rpc-gssd.service                       static          -
rpc-statd-notify.service               static          -
rpc-statd.service                      static          -
rpcbind.service                        disabled        disabled
rpmdb-rebuild.service                  disabled        enabled
rsyslog.service                        enabled         enabled
selinux-autorelabel-mark.service       enabled         enabled
selinux-autorelabel.service            static          -
selinux-check-proper-disable.service   disabled        disabled
serial-getty@.service                  indirect        disabled
sshd-keygen@.service                   disabled        disabled
sshd.service                           enabled         enabled
sshd@.service                          static          -
stunnel.service                        disabled        disabled
stunnel@.service                       disabled        disabled
system-update-cleanup.service          static          -
systemd-ask-password-console.service   static          -
systemd-ask-password-wall.service      static          -
systemd-backlight@.service             static          -
systemd-binfmt.service                 static          -
systemd-bless-boot.service             static          -
systemd-boot-check-no-failures.service disabled        disabled
systemd-boot-system-token.service      static          -
systemd-coredump@.service              static          -
systemd-exit.service                   static          -
systemd-firstboot.service              static          -
systemd-fsck-root.service              enabled-runtime disabled
systemd-fsck@.service                  static          -
systemd-halt.service                   static          -
systemd-hibernate-resume@.service      static          -
systemd-hibernate.service              static          -
systemd-homed-activate.service         disabled        disabled
systemd-homed.service                  disabled        enabled
systemd-hostnamed.service              static          -
systemd-hwdb-update.service            static          -
systemd-hybrid-sleep.service           static          -
systemd-initctl.service                static          -
systemd-journal-catalog-update.service static          -
systemd-journal-flush.service          static          -
systemd-journald.service               static          -
systemd-journald@.service              static          -
systemd-kexec.service                  static          -
systemd-localed.service                static          -
systemd-logind.service                 static          -
systemd-machine-id-commit.service      static          -
systemd-modules-load.service           static          -
systemd-network-generator.service      enabled         enabled
systemd-networkd-wait-online.service   enabled         disabled
systemd-networkd.service               enabled         disabled
systemd-oomd.service                   disabled        disabled
systemd-portabled.service              static          -
systemd-poweroff.service               static          -
systemd-pstore.service                 disabled        enabled
systemd-quotacheck.service             static          -
systemd-random-seed.service            static          -
systemd-reboot.service                 static          -
systemd-remount-fs.service             enabled-runtime disabled
systemd-repart.service                 static          -
systemd-resolved.service               enabled         enabled
systemd-rfkill.service                 static          -
systemd-suspend-then-hibernate.service static          -
systemd-suspend.service                static          -
systemd-sysctl.service                 static          -
systemd-sysext.service                 disabled        disabled
systemd-sysusers.service               static          -
systemd-time-wait-sync.service         disabled        disabled
systemd-timedated.service              static          -
systemd-timesyncd.service              disabled        disabled
systemd-tmpfiles-clean.service         static          -
systemd-tmpfiles-setup-dev.service     static          -
systemd-tmpfiles-setup.service         static          -
systemd-udev-settle.service            static          -
systemd-udev-trigger.service           static          -
systemd-udevd.service                  static          -
systemd-update-done.service            static          -
systemd-update-utmp-runlevel.service   static          -
systemd-update-utmp.service            static          -
systemd-user-sessions.service          static          -
systemd-userdbd.service                indirect        disabled
systemd-vconsole-setup.service         static          -
systemd-volatile-root.service          static          -
update-motd.service                    enabled         enabled
user-runtime-dir@.service              static          -
user@.service                          static          -

158 unit files listed.

登録されているServiceの一覧です。

SSMエージェント

[ec2-user@ip-10-1-0-133 ~]$ sudo systemctl status amazon-ssm-agent
● amazon-ssm-agent.service - amazon-ssm-agent
     Loaded: loaded (/etc/systemd/system/amazon-ssm-agent.service; enabled; vendor preset: disabled)
     Active: active (running) since Fri 2022-04-29 03:29:57 UTC; 1h 25min ago
   Main PID: 2137 (amazon-ssm-agen)
      Tasks: 33 (limit: 4672)
     Memory: 108.9M
        CPU: 14.858s
     CGroup: /system.slice/amazon-ssm-agent.service
             ├─2137 /usr/bin/amazon-ssm-agent
             ├─2184 /usr/bin/ssm-agent-worker
             ├─5809 /usr/bin/ssm-session-worker xxxxxxxxxx-1234567890xxxxxxx
             └─5822 sh

Apr 29 03:29:58 ip-10-1-0-133.ap-northeast-1.compute.internal amazon-ssm-agent[2137]: 2022-04-29 03:29:57 INFO [amazon-ssm-agent] using named pipe channel for IPC
Apr 29 03:29:58 ip-10-1-0-133.ap-northeast-1.compute.internal amazon-ssm-agent[2137]: 2022-04-29 03:29:57 INFO [amazon-ssm-agent] amazon-ssm-agent - v3.1.1260.0
Apr 29 03:29:58 ip-10-1-0-133.ap-northeast-1.compute.internal amazon-ssm-agent[2137]: 2022-04-29 03:29:57 INFO [amazon-ssm-agent] OS: linux, Arch: amd64
Apr 29 03:29:58 ip-10-1-0-133.ap-northeast-1.compute.internal amazon-ssm-agent[2137]: 2022-04-29 03:29:57 INFO [CredentialRefresher] Identity does not require credential refresher
Apr 29 03:29:58 ip-10-1-0-133.ap-northeast-1.compute.internal amazon-ssm-agent[2137]: 2022-04-29 03:29:58 INFO [amazon-ssm-agent] [LongRunningWorkerContainer] [WorkerProvider] Worker ssm-agent-worker is not running, starting worker process
Apr 29 03:29:58 ip-10-1-0-133.ap-northeast-1.compute.internal amazon-ssm-agent[2137]: 2022-04-29 03:29:58 INFO [amazon-ssm-agent] [LongRunningWorkerContainer] [WorkerProvider] Worker ssm-agent-worker (pid:2184) started
Apr 29 03:29:58 ip-10-1-0-133.ap-northeast-1.compute.internal amazon-ssm-agent[2137]: 2022-04-29 03:29:58 INFO [amazon-ssm-agent] [LongRunningWorkerContainer] Monitor long running worker health every 60 seconds
Apr 29 03:32:43 ip-10-1-0-133.ap-northeast-1.compute.internal sudo[2689]: ssm-user : TTY=pts/0 ; PWD=/usr/bin ; USER=root ; COMMAND=/usr/bin/su ec2-user -
Apr 29 04:18:34 ip-10-1-0-133.ap-northeast-1.compute.internal sudo[5823]: ssm-user : TTY=pts/1 ; PWD=/usr/bin ; USER=root ; COMMAND=/usr/bin/view /etc/ecs/ecs.config
Apr 29 04:28:32 ip-10-1-0-133.ap-northeast-1.compute.internal sudo[7613]: ssm-user : TTY=pts/1 ; PWD=/usr/bin ; USER=root ; COMMAND=/usr/bin/su

SSMエージェントがインストール済みとなっています。デフォルトでSession ManagerやECS Execなども利用できそうです。

Docker

[ec2-user@ip-10-1-0-133 ~]$ sudo systemctl status docker
● docker.service - Docker Application Container Engine
     Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
     Active: active (running) since Fri 2022-04-29 03:29:59 UTC; 1h 28min ago
TriggeredBy: ● docker.socket
       Docs: https://docs.docker.com
    Process: 2139 ExecStartPre=/bin/mkdir -p /run/docker (code=exited, status=0/SUCCESS)
    Process: 2144 ExecStartPre=/usr/libexec/docker/docker-setup-runtimes.sh (code=exited, status=0/SUCCESS)
   Main PID: 2147 (dockerd)
      Tasks: 10 (limit: 4672)
     Memory: 447.7M
        CPU: 11.018s
     CGroup: /system.slice/docker.service
             └─2147 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock --default-ulimit nofile=32768:65536

Apr 29 03:29:59 ip-10-1-0-133.ap-northeast-1.compute.internal dockerd[2147]: time="2022-04-29T03:29:59.200134741Z" level=info msg="Loading containers: done."
Apr 29 03:29:59 ip-10-1-0-133.ap-northeast-1.compute.internal dockerd[2147]: time="2022-04-29T03:29:59.491759012Z" level=info msg="Docker daemon" commit=b0f5bc3 graphdriver(s)=overlay2 version=20.10.7
Apr 29 03:29:59 ip-10-1-0-133.ap-northeast-1.compute.internal dockerd[2147]: time="2022-04-29T03:29:59.500950049Z" level=info msg="Daemon has completed initialization"
Apr 29 03:29:59 ip-10-1-0-133.ap-northeast-1.compute.internal systemd[1]: Started Docker Application Container Engine.
Apr 29 03:29:59 ip-10-1-0-133.ap-northeast-1.compute.internal dockerd[2147]: time="2022-04-29T03:29:59.578671440Z" level=info msg="API listen on /run/docker.sock"
Apr 29 03:29:59 ip-10-1-0-133.ap-northeast-1.compute.internal dockerd[2147]: time="2022-04-29T03:29:59.878678952Z" level=warning msg="Your kernel does not support memory swappiness capabilities or the cgroup is not mounted. Memory swappiness discarded."
Apr 29 04:25:28 ip-10-1-0-133.ap-northeast-1.compute.internal dockerd[2147]: time="2022-04-29T04:25:28.514842411Z" level=info msg="ignoring event" container=cb087f85db1f28a7030af81941525e030a7c833780e831f5a9e3216772f1a499 module=libcontainerd namespace=moby topic=/tasks/delete type="*events.TaskDelete"
Apr 29 04:26:59 ip-10-1-0-133.ap-northeast-1.compute.internal dockerd[2147]: time="2022-04-29T04:26:59.030850035Z" level=info msg="ignoring event" container=fdfa5b0ddcea699ae483c4cd4b995c5126c167cbbdf737c7acdf89cbccc51105 module=libcontainerd namespace=moby topic=/tasks/delete type="*events.TaskDelete"
Apr 29 04:27:08 ip-10-1-0-133.ap-northeast-1.compute.internal dockerd[2147]: time="2022-04-29T04:27:08.963142959Z" level=info msg="ignoring event" container=2bf315e59ff1ebd9416174061eb2eff927f4386944534fd92a34a1caa0ae202f module=libcontainerd namespace=moby topic=/tasks/delete type="*events.TaskDelete"
Apr 29 04:27:19 ip-10-1-0-133.ap-northeast-1.compute.internal dockerd[2147]: time="2022-04-29T04:27:19.358866821Z" level=info msg="ignoring event" container=aec109a393eafa680196393d16e5130b2b8fb6923bf1cf611bd9c61173e1d855 module=libcontainerd namespace=moby topic=/tasks/delete type="*events.TaskDelete"

もちろん、Dockerもインストール済みとなっています。

Amazon Linux 2022のGAに向けた事前の検証目的などでご利用ください

2022年4月現在、Amazon Linux 2022自体がまだプレビューの段階です。

Amazon ECS on EC2環境でAmazon Linux 2などのECS-optimized AMIをご利用中の場合、将来的にAmazon Linux 2022に移行する可能性があるかと思いますので、現状ではGAに向けた事前の検証目的などでご利用ください。

また、Amazon Linuxの他に、コンテナをホストすることに特化した軽量OSとしてBottlerocketがあります。こちらもセキュリティ面などで利点が大きいので、併せてご検討してみてください。

以上、コンサル部のとばち(@toda_kk)でした。