如何为我使用 AWS CloudFormation 模板创建的 Amazon EC2 实例设置根卷属性?

上次更新时间:2020 年 5 月 22 日

我使用 AWS CloudFormation 模板创建了一个 Amazon Elastic Compute Cloud (Amazon EC2) 实例,我想设置它的根卷属性。例如,我想更改根卷的大小或启用根卷加密。

简短描述

要设置 EC2 实例的根卷属性,您首先需要确定 Amazon 系统映像 (AMI) 的根卷设备名称。然后,您可以使用 AWS::EC2::Instance 资源的 BlockDeviceMapping 属性来设置根卷属性。

注意:默认情况下,EC2 实例将使用 AMI 的块储存设备映射中指定的块储存设备。要覆盖 AMI 块储存设备映射,请使用实例块储存设备映射。对于根卷,您仅可以覆盖卷大小、卷类型和 DeleteOnTermination 设置。实例开始运行后,您仅可以修改附加的 Amazon Elastic Block Store (Amazon EBS) 卷的 DeleteOnTermination 设置。

注意:如果您在运行 AWS 命令行界面 (AWS CLI) 命令时收到错误,请确保您运行的是最新版本的 AWS CLI。

解决方法 确定 AMI 的根卷设备名称

要查找设备名称,请通过 Amazon EC2 控制台或 AWS CLI 完成以下步骤。

使用 Amazon EC2 控制台:

1.打开 Amazon EC2 控制台。

2.从导航栏中,选择您要在其中启动实例的 AWS 区域。

3.从导航窗格中,选择 AMI

4.使用筛选条件选项来查找您的 AMI,然后选中您的 AMI。

5.在详细信息选项卡中,找到根设备名称。此处将列出您的根设备名称。

使用 AWS CLI 命令:

在 AWS CLI 中,运行以下命令:

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 示例中,AWS CloudFormation 创建了一个 EC2 实例并将根卷大小设置为 30GB。

在 JSON 和 YAML 模板中,根卷的 DeleteOnTermination 属性设置为 true。DeviceName 设置为 /dev/xvda,因为指定的 AMI 是 Amazon Linux 2 AMI。最后,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": 
      }
    }
  }
}

YAML 模板:

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 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'