使用 cloudformation 调整 ec2 实例的根卷大小

问题描述 投票:0回答:2

我有一个使用 cloudformation 创建的实例,如下所示:

EC2Instance:
    Type: AWS::EC2::Instance
    Properties:
      ImageId: !Ref ServerAMI
      InstanceType: !Ref ServerInstanceType
      KeyName: !Ref KeyName
      BlockDeviceMappings:
      - DeviceName: /dev/xvda
        Ebs:
          VolumeSize: 30
      NetworkInterfaces:
      - AssociatePublicIpAddress: 'false'
        DeleteOnTermination: 'true'
        DeviceIndex: '0'
        GroupSet:
        - Ref: ServerSecurityGroup
        SubnetId: !Ref SubnetID
      Tags:
      - { Key: Name, Value: !Ref AWS::StackName }

在本例中,我的根卷创建为 30GB。如果我尝试通过设置

VolumeSize
值来增加此根卷大小,那么我的 ec2 实例将被终止并重新创建。

但是在控制台中,我可以增加根卷的大小,而无需重新创建实例。

是否有任何解决办法,以防止 ec2 实例在尝试通过 cloudformation 增加根卷大小时被终止?

编辑: 这是我用来再次测试的一个小测试堆栈。部署一次,然后更改 VolumeSize 并重新部署 - 它想要替换实例:

AWSTemplateFormatVersion: '2010-09-09'

Description: Test stack for a single ec2 instance

Parameters:

  ServerAMI:
    Type: String
    Default: ami-096f43ef67d75e998

  ServerInstanceType:
    Type: String
    Default: t2.small

  DefaultVPCID:
    Type: String

  SubnetID:
    Type: String

  KeyName:
    Type: AWS::EC2::KeyPair::KeyName

Resources:
  EC2Instance:
    Type: AWS::EC2::Instance
    Properties:
      ImageId: !Ref ServerAMI
      InstanceType: !Ref ServerInstanceType
      KeyName: !Ref KeyName
      BlockDeviceMappings:
      - DeviceName: /dev/xvda #Linux
        Ebs:
          VolumeSize: 30
      NetworkInterfaces:
      - AssociatePublicIpAddress: 'false'
        DeleteOnTermination: 'true'
        DeviceIndex: '0'
        GroupSet:
        - Ref: ServerSecurityGroup
        SubnetId: !Ref SubnetID
      Tags:
      - { Key: Name, Value: !Ref AWS::StackName }

  ServerSecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: Webserver security group
      VpcId: !Ref DefaultVPCID
      SecurityGroupIngress:
      - { IpProtocol: tcp, FromPort: '22',   ToPort: '22',   CidrIp: '127.0.0.1/32',   Description: 'Test Instance'  }
amazon-web-services amazon-ec2 aws-cloudformation
2个回答
1
投票

不幸的是,我不相信你可以 - 根据 CloudFormation 文档

实例运行后,您只能修改附加卷的DeleteOnTermination参数,而无需中断实例。修改任何其他参数都会导致实例替换。


0
投票

我认为 Cloudformation 文档还没有解决这个问题。我们最近已切换到 terraform,这似乎没有此类问题。

© www.soinside.com 2019 - 2024. All rights reserved.