【小ネタ】 AWS CLI コマンドを利用して AMI のブロックデバイスマッピング情報を引き継がないでインスタンスを作成する方法
はじめに
テクニカルサポートの 片方 です。
今回は小ネタとして、AWS CLI コマンドを利用して AMI のブロックデバイスマッピング情報を引き継がないでインスタンスを作成する方法を紹介します。
補足
Windows AMI に付属する xvdca=ephemeral0 といったブロックデバイスは、エフェメラルディスク(インスタンスストアボリューム)用の仮想デバイス名です。
AWS で提供している Windows AMI のブロックデバイスには、ephemeral が付属しているため、EC2 インスタンスのデバイス情報として ephemeral が付属した状態になります。
インスタンスストアボリュームの仮想デバイスにはephemeral0 から ephemeral23 の順序で仮想デバイス名が付けられます。例えば、1 つのインスタンスストアボリュームをサポートするインスタンスタイプではその 1 つのボリュームの仮想デバイス名は ephemeral0 になります。4 つのインスタンスストアボリュームをサポートするインスタンスタイプではその 4 つのボリュームの仮想デバイス名は ephemeral0、ephemeral1、ephemeral2、および ephemeral3 になります。
やってみた
起動元 AMI のブロックデバイスマッピング情報を引き継がないでインスタンスを作成する場合は、AWS CLI コマンドを利用します。具体的には "--block-device-mappings" オプションにて NoDevice を明示的に指定した状態でインスタンスを作成する必要がございます。
NoDevice -> (string)
To omit the device from the block device mapping, specify an empty string. When this property is specified, the device is removed from the block device mapping regardless of the assigned value.
DeviceName -> (string)
The device name (for example, /dev/sdh or xvdh ).
VirtualName -> (string)
The virtual device name (ephemeral N). Instance store volumes are numbered starting from 0. An instance type with 2 available instance store volumes can specify mappings for ephemeral0 and ephemeral1 . The number of available instance store volumes depends on the instance type. After you connect to the instance, you must mount the volume.
NVMe instance store volumes are automatically enumerated and assigned a device name. Including them in your block device mapping has no effect.
Constraints: For M3 instances, you must specify instance store volumes in the block device mapping for the instance. When you launch an M3 instance, we ignore any instance store volumes specified in the block device mapping for the AMI.
ルートボリューム以外の、削除したい全てのブロックデバイスに対して "NoDevice" パラメータを指定する必要があるため、以下のような AWS CLI コマンドが想定されます。
$ aws ec2 run-instances \
--image-id "ami-xxxxxxxxxxxxxxx" \
--instance-type "t3.micro" \
--key-name "xxxxxxxxxxxx" \
--block-device-mappings '[
{"DeviceName":"/dev/sda1","Ebs":{"Encrypted":true,"DeleteOnTermination":true,"KmsKeyId":"arn:aws:kms:ap-northeast-1:123456789012:key/fae2884d-e9c3-1234-aaaa-xxxxxxxxxxxx","SnapshotId":"snap-0cc51234567890xxxxx","VolumeSize":1400,"VolumeType":"gp3"}},
{"DeviceName":"xvdca","NoDevice":""},
{"DeviceName":"xvdcb","NoDevice":""},
{"DeviceName":"xvdcc","NoDevice":""},
{"DeviceName":"xvdcd","NoDevice":""},
{"DeviceName":"xvdce","NoDevice":""},
{"DeviceName":"xvdcf","NoDevice":""},
{"DeviceName":"xvdcg","NoDevice":""},
{"DeviceName":"xvdch","NoDevice":""},
{"DeviceName":"xvdci","NoDevice":""},
{"DeviceName":"xvdcj","NoDevice":""},
{"DeviceName":"xvdck","NoDevice":""},
{"DeviceName":"xvdcl","NoDevice":""},
{"DeviceName":"xvdcm","NoDevice":""},
{"DeviceName":"xvdcn","NoDevice":""},
{"DeviceName":"xvdco","NoDevice":""},
{"DeviceName":"xvdcp","NoDevice":""},
{"DeviceName":"xvdcq","NoDevice":""},
{"DeviceName":"xvdcr","NoDevice":""},
{"DeviceName":"xvdcs","NoDevice":""},
{"DeviceName":"xvdct","NoDevice":""},
{"DeviceName":"xvdcu","NoDevice":""},
{"DeviceName":"xvdcv","NoDevice":""},
{"DeviceName":"xvdcw","NoDevice":""},
{"DeviceName":"xvdcx","NoDevice":""},
{"DeviceName":"xvdcy","NoDevice":""},
{"DeviceName":"xvdcz","NoDevice":""}
]' \
(下略)
※ 一部マスクします
インスタンスが作成されたので、AMI を取得します。
作成した ami-01aa58bc6d7b61793 よりブロックデバイスマッピング情報を引き継いでいないか確認します。
成功です!
まとめ
ブロックデバイスはエフェメラルディスク (インスタンスストアボリューム) を使用する EC2 インスタンスを利用する際に割り当てられるので、特に理由がなければ削除する必要はございません。
本ブログが誰かの参考になれば幸いです。
参考資料
- EC2 インスタンス用のインスタンスストアの一時ブロックストレージ - Amazon Elastic Compute Cloud
- run-instances — AWS CLI 2.27.26 Command Reference
アノテーション株式会社について
アノテーション株式会社は、クラスメソッド社のグループ企業として「オペレーション・エクセレンス」を担える企業を目指してチャレンジを続けています。「らしく働く、らしく生きる」のスローガンを掲げ、様々な背景をもつ多様なメンバーが自由度の高い働き方を通してお客様へサービスを提供し続けてきました。現在当社では一緒に会社を盛り上げていただけるメンバーを募集中です。少しでもご興味あれば、アノテーション株式会社WEBサイトをご覧ください。