こんにちは、CX 事業本部 Delivery 部の若槻です。
AWS Cloud9 のドキュメントでは EC2 インスタンスのボリュームサイズ(ディスク容量)を簡単に拡張できるスクリプトが提供されています。Cloud9 環境作成後の既定のボリュームサイズである 10GB では不足する場合も多いので、私もよく利用しています。
そのスクリプトですが、久しぶりに使おうとしたらなんと Amazon Linux 2023 対応版になっていました。下記がそのスクリプトです。
resize.sh
#!/bin/bash
# Specify the desired volume size in GiB as a command line argument. If not specified, default to 20 GiB.
SIZE=${1:-20}
# Get the ID of the environment host Amazon EC2 instance.
TOKEN=$(curl -s -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 60")
INSTANCEID=$(curl -s -H "X-aws-ec2-metadata-token: $TOKEN" -v http://169.254.169.254/latest/meta-data/instance-id 2> /dev/null)
REGION=$(curl -s -H "X-aws-ec2-metadata-token: $TOKEN" -v http://169.254.169.254/latest/meta-data/placement/region 2> /dev/null)
# Get the ID of the Amazon EBS volume associated with the instance.
VOLUMEID=$(aws ec2 describe-instances \
--instance-id $INSTANCEID \
--query "Reservations[0].Instances[0].BlockDeviceMappings[0].Ebs.VolumeId" \
--output text \
--region $REGION)
# Resize the EBS volume.
aws ec2 modify-volume --volume-id $VOLUMEID --size $SIZE
# Wait for the resize to finish.
while [ \
"$(aws ec2 describe-volumes-modifications \
--volume-id $VOLUMEID \
--filters Name=modification-state,Values="optimizing","completed" \
--query "length(VolumesModifications)"\
--output text)" != "1" ]; do
sleep 1
done
# Check if we're on an NVMe filesystem
if [[ -e "/dev/xvda" && $(readlink -f /dev/xvda) = "/dev/xvda" ]]
then
# Rewrite the partition table so that the partition takes up all the space that it can.
sudo growpart /dev/xvda 1
# Expand the size of the file system.
# Check if we're on AL2 or AL2023
STR=$(cat /etc/os-release)
SUBAL2="VERSION_ID=\"2\""
SUBAL2023="VERSION_ID=\"2023\""
if [[ "$STR" == *"$SUBAL2"* || "$STR" == *"$SUBAL2023"* ]]
then
sudo xfs_growfs -d /
else
sudo resize2fs /dev/xvda1
fi
else
# Rewrite the partition table so that the partition takes up all the space that it can.
sudo growpart /dev/nvme0n1 1
# Expand the size of the file system.
# Check if we're on AL2 or AL2023
STR=$(cat /etc/os-release)
SUBAL2="VERSION_ID=\"2\""
SUBAL2023="VERSION_ID=\"2023\""
if [[ "$STR" == *"$SUBAL2"* || "$STR" == *"$SUBAL2023"* ]]
then
sudo xfs_growfs -d /
else
sudo resize2fs /dev/nvme0n1p1
fi
fi
使い方は以前と同じで、引数に拡張後のサイズ(GiB)を指定して実行するだけです。未指定の場合は既定で 20 GiB になります。
bash resize.sh 20
# or
chmod +x resize.sh
./resize.sh 20
試してみた
Cloud9 のターミナルでスクリプトを実行してボリュームサイズの拡張を試してみます。
Cloud9 環境の OS は Amazon Linux 2023 です。
$ cat /etc/os-release
NAME="Amazon Linux"
VERSION="2023"
ID="amzn"
ID_LIKE="fedora"
VERSION_ID="2023"
PLATFORM_ID="platform:al2023"
PRETTY_NAME="Amazon Linux 2023"
ANSI_COLOR="0;33"
CPE_NAME="cpe:2.3:o:amazon:amazon_linux:2023"
HOME_URL="https://aws.amazon.com/linux/"
BUG_REPORT_URL="https://github.com/amazonlinux/amazon-linux-2023"
SUPPORT_END="2028-03-15"
スクリプト実行前のサイズ確認
スクリプト実行前の /dev/xvda1
のサイズは 10 GB となっています。
$ df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 4.0M 0 4.0M 0% /dev
tmpfs 475M 0 475M 0% /dev/shm
tmpfs 190M 2.9M 188M 2% /run
/dev/xvda1 10G 6.9G 3.1G 70% /
tmpfs 475M 0 475M 0% /tmp
/dev/xvda128 10M 1.3M 8.7M 13% /boot/efi
tmpfs 95M 0 95M 0% /run/user/1000
スクリプト実行
スクリプト resize.sh
を実行します。拡張後のサイズは 20 GiB とします。
$ bash resize.sh 20
{
"VolumeModification": {
"VolumeId": "vol-0bcc3bdf822d7a82a",
"ModificationState": "modifying",
"TargetSize": 20,
"TargetIops": 3000,
"TargetVolumeType": "gp3",
"TargetThroughput": 125,
"TargetMultiAttachEnabled": false,
"OriginalSize": 10,
"OriginalIops": 3000,
"OriginalVolumeType": "gp3",
"OriginalThroughput": 125,
"OriginalMultiAttachEnabled": false,
"Progress": 0,
"StartTime": "2023-12-17T15:49:05+00:00"
}
}
CHANGED: partition=1 start=24576 old: size=20946911 end=20971487 new: size=41918431 end=41943007
meta-data=/dev/xvda1 isize=512 agcount=3, agsize=1047040 blks
= sectsz=4096 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=1, rmapbt=0
= reflink=1 bigtime=1 inobtcount=1
data = bsize=4096 blocks=2618363, imaxpct=25
= sunit=128 swidth=128 blks
naming =version 2 bsize=16384 ascii-ci=0, ftype=1
log =internal log bsize=4096 blocks=16384, version=2
= sectsz=4096 sunit=4 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 2618363 to 5239803
スクリプト実行後のサイズ確認
スクリプト実行後の /dev/xvda1
のサイズは 20 GB となっています。ボリュームサイズが指定通りに拡張されています!
$ df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 4.0M 0 4.0M 0% /dev
tmpfs 475M 0 475M 0% /dev/shm
tmpfs 190M 2.9M 188M 2% /run
/dev/xvda1 20G 7.0G 13G 35% /
tmpfs 475M 0 475M 0% /tmp
/dev/xvda128 10M 1.3M 8.7M 13% /boot/efi
tmpfs 95M 0 95M 0% /run/user/1000
スクリプトの主な変更点
順番が前後してしまいますが、スクリプトの主な変更点は次の 2 点となっていました。
- インスタンス ID の取得リクエストにトークンを付与するようになった
/etc/os-release
を使用した OS バージョン判定で Amazon Linux 2023 に対応した
1. インスタンス ID の取得リクエストにトークンを付与するようになった
まず、インスタンスのボリューム ID を取得するために必要な情報であるインスタンス ID の取得リクエストに、トークンを付与するようになっていました。
TOKEN=$(curl -s -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 60")
INSTANCEID=$(curl -s -H "X-aws-ec2-metadata-token: $TOKEN" -v http://169.254.169.254/latest/meta-data/instance-id 2> /dev/null)
これは Amazon Linux 2023 以降の EC2 インスタンスでは、インスタンスメタデータサービス(IMDS)が v2 となり、トークンを付与しないとリクエストが拒否されるようになったためです。
インスタンスメタデータを取得してみると、HttpTokens
が required
となっており、IMDS v2 が有効になっていることが確認できます。
$ TOKEN=$(curl -s -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 60")
$ INSTANCEID=$(curl -s -H "X-aws-ec2-metadata-token: $TOKEN" -v http://169.254.169.254/latest/meta-data/instance-id 2> /dev/null)
$ aws ec2 describe-instances \
> --instance-id $INSTANCEID \
> --query "Reservations[*].Instances[*].MetadataOptions"
[
[
{
"State": "applied",
"HttpTokens": "required",
"HttpPutResponseHopLimit": 2,
"HttpEndpoint": "enabled",
"HttpProtocolIpv6": "disabled",
"InstanceMetadataTags": "disabled"
}
]
]
なお IMDS バージョンの判定方法はこちらが参考になりました。
2. /etc/os-release
を使用した OS バージョン判定で Amazon Linux 2023 に対応した
もう一つは OS バージョンの判定に /etc/os-release
を使用しているため、Amazon Linux 2023 の場合にも対応可能となっていた点です。
STR=$(cat /etc/os-release)
SUBAL2="VERSION_ID=\"2\""
SUBAL2023="VERSION_ID=\"2023\""
if [[ "$STR" == *"$SUBAL2"* || "$STR" == *"$SUBAL2023"* ]]
ここは意外と泥臭い方法で判別しているんだなという印象です。
その他
Amazon Linux 2 までのスクリプトのダウンロードができなくなった
下記で紹介している通り、以前までは Amazon Linux 2 までのスクリプトは https://sehyul-us-east-1.s3.amazonaws.com/scripts/resize.sh で公開されていましたが、現在はダウンロードできなくなっています。
Cloud9 環境では Amazon Linux 2023 が推奨されるようになった
最後に、そもそも Amazon Linux 2023 (AL2023) は AWS の次世代の OS として March 15, 2023 に一般提供開始された Amazon Linux となります。
現在、AWS サービス上での Amazon Linux OS の利用では、基本的に AL 2023 の選択が推奨されています。
これは AWS Cloud9 の場合も同様です。次のようにドキュメントでも明示的に記載されています。(一方で日本語版ドキュメントの方は記述が更新がされておらず Amazon Linux 2 のままなのでご注意下さい。)
We recommend that you choose the Amazon Linux 2023 option for your EC2 environment. In addition to providing a secure, stable, and high-performance runtime environment, Amazon Linux 2023 AMI includes long-term support through 2024.
またコンソールから Cloud9 環境を作成する際にも AL2023 の選択がメニューで推奨されています。
よって今後基本的には AL 2023 を使うことになると思うので、今までの古いスクリプトではなく、今回ご紹介した新しいスクリプトを使うようにしましょう。
参考
以上