AWS CloudFormation テンプレートを使用して Amazon Elastic Compute Cloud (Amazon EC2) インスタンスを作成しました。インスタンスのルートボリュームにプロパティを設定したいです。
解決策
注: デフォルトでは、EC2 インスタンスは Amazon マシンイメージ (AMI) のブロックデバイスマッピングで指定されたブロックデバイスを使用します。AMI ブロックデバイスマッピングを無効にするには、インスタンスブロックデバイスマッピングを使用します。ルートボリュームでは、ボリュームサイズ、ボリュームタイプ、DeleteOnTermination 設定のみを上書きできます。インスタンスが実行中になった後は、アタッチされている Amazon Elastic Block Store (Amazon EBS) ボリュームの DeleteOnTermination 設定のみを変更できます。
AMI のルートボリュームのデバイス名を特定する
デバイス名を特定するには、Amazon EC2 コンソール、または AWS コマンドラインインターフェイス (AWS CLI) を使用します。
**注:**AWS CLI のコマンドの実行時にエラーが発生する場合は、「AWS CLI エラーのトラブルシューティング」を参照してください。また、AWS CLI の最新バージョンを使用していることを確認してください。
Amazon EC2 コンソール
デバイス名を特定するには、次の手順を実行します。
- Amazon EC2 コンソールを開きます。
- ナビゲーションバーから、インスタンスを起動する AWS リージョンを選択します。
- ナビゲーションペインで [AMI] を選択します。
- フィルターを使用して AMI を特定したら、AMI を選択します。
- [詳細] タブで、[ルートデバイス名] の値をコピーします。
AWS CLI
デバイス名を確認するには、describe-images コマンドを実行します。
aws ec2 describe-images \ --region us-east-1 \
--image-ids ami-1234567890AWSEXAMPLE
注: お使いのものでそれぞれ、us-east-1 をリージョン、ami-1234567890AWSEXAMPLE を AMI に置き換えます。
コマンドの出力では、デバイス名が RootDeviceName の値として返されます。
EC2 インスタンス用のルートボリュームのプロパティを設定する
AWS::EC2::Instance リソースの BlockDeviceMapping プロパティを使用して、EC2 インスタンス用ルートボリュームのプロパティを設定します。
次の JSON と YAML の例では、CloudFormation は次の設定で EC2 インスタンスを作成します。
- ルートボリュームのサイズは 30 GB に設定されています。
- ルートボリュームの DeleteOnTermination プロパティは true です。
- 指定された AMI は Amazon Linux 2 AMI であるため、DeviceName は /dev/xvda です。
- Encrypted プロパティは true です。この設定では、ルートボリュームでデフォルトの暗号化を行えます。
**注:**テンプレートの /dev/xvda は、ルートデバイス名の値に置き換えます。必要に応じて、テンプレートの Ebs プロパティを変更します。
JSON テンプレートの例:
{ "AWSTemplateFormatVersion": "2010-09-09",
"Description": "AWS CloudFormation Sample Template that shows how to increase the size of the root volume. **WARNING** This template creates an Amazon EC2 instance. You will be billed for the AWS resource used if you create a stack from this template.",
"Parameters": {
"KeyName": {
"Type": "AWS::EC2::KeyPair::KeyName",
"Description": "Name of an existing EC2 KeyPair to enable SSH access to the EC2 instance."
},
"InstanceType": {
"Description": "EC2 instance type",
"Type": "String",
"Default": "t2.micro",
"ConstraintDescription": "Please choose a valid instance type."
},
"AMIID": {
"Description": "The Latest Amazon Linux 2 AMI taken from the public AWS Systems Manager Parameter Store",
"Type": "AWS::SSM::Parameter::Value<String>",
"Default": "/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2"
}
},
"Resources": {
"LinuxInstance": {
"Type": "AWS::EC2::Instance",
"Properties": {
"ImageId": {
"Ref": "AMIID"
},
"InstanceType": {
"Ref": "InstanceType"
},
"KeyName": {
"Ref": "KeyName"
},
"BlockDeviceMappings": [
{
"DeviceName": "/dev/xvda",
"Ebs": {
"VolumeType": "gp2",
"VolumeSize": "30",
"DeleteOnTermination":"false",
"Encrypted": "true"
}
}
]
}
}
}
}
YAML テンプレートの例:
AWSTemplateFormatVersion: 2010-09-09Description: >-
AWS CloudFormation Sample Template that shows how to increase the size of the root volume. **WARNING** This template creates an Amazon EC2 instance. You will be billed for the AWS resource used if you create a stack from this template.
Parameters:
KeyName:
Type: 'AWS::EC2::KeyPair::KeyName'
Description: Name of an existing EC2 KeyPair to enable SSH access to the EC2 instance.
InstanceType:
Description: EC2 instance type
Type: String
Default: t2.micro
ConstraintDescription: Please choose a valid instance type.
AMIID:
Description: >-
The Latest Amazon Linux 2 AMI taken from the public Systems Manager
Parameter Store
Type: 'AWS::SSM::Parameter::Value<String>'
Default: /aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2
Resources:
LinuxInstance:
Type: 'AWS::EC2::Instance'
Properties:
ImageId: !Ref AMIID
InstanceType: !Ref InstanceType
KeyName: !Ref KeyName
BlockDeviceMappings:
- DeviceName: /dev/xvda
Ebs:
VolumeType: gp2
VolumeSize: '30'
DeleteOnTermination: 'false'
Encrypted: 'true'