跳至内容

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

3 分钟阅读
0

我使用 AWS CloudFormation 模板创建了 Amazon Elastic Compute Cloud (Amazon EC2) 实例。我想为实例设置根卷的属性。

解决方法

**注意:**默认情况下,EC2 实例使用块设备映射中为亚马逊机器映像 (AMI) 指定的块设备。要覆盖 AMI 块设备映射,请使用实例块设备映射。对于根卷,您只能覆盖卷大小、卷类型和 DeleteOnTermination 设置。实例运行后,您只能修改附加的 Amazon Elastic Block Store (Amazon EBS) 卷的 DeleteOnTermination 设置。

识别您的 AMI 根卷的设备名称

要查找设备名称,请使用 Amazon EC2 控制台或 AWS 命令行界面 (AWS CLI)。

注意:如果在运行 AWS CLI 命令时收到错误,请参阅排查 AWS CLI 错误。此外,请确保您使用的是最新版本的 AWS CLI

Amazon EC2 控制台

要识别设备名称,请完成以下步骤:

  1. 打开 Amazon EC2 控制台
  2. 在导航栏中,选择要启动实例的 AWS 区域。
  3. 在导航窗格中,选择 AMI
  4. 使用 Filter(筛选器)查找您的 AMI,然后选择您的 AMI。
  5. Details(详细信息)选项卡中,复制 Root Device Name(根设备名称)的值。

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
  • DeviceName/dev/xvda,因为指定的 AMI 为 Amazon Linux 2 AMI。
  • Encrypted 属性为 true。此配置允许对根卷进行默认加密

**注意:**在您的模板中,将 /dev/xvda 替换为 Root Device Name(根设备名称)值。如果需要,修改模板中的 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'
AWS 官方已更新 1 年前