Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resizing root volume size of ec2 instance with cloudformation

I have an instance created with cloudformation like below:

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 }

My root volume in this case is created at 30GB. If I try increase this root volume size by setting the VolumeSize value then my ec2 instance is terminated and recreated.

Yet in the console I am able to increase the size of my root volume without recreation of my instance.

Is there any work around for this in order to prevent ec2 instance from being terminated when trying to increase root volume size via cloudformation?

Edit: Here is a small test stack I'm using to test this again. Deployed once, then change VolumeSize and redeploy - it wants to replace the instance:

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'  }
like image 657
WarrenG Avatar asked Sep 06 '25 16:09

WarrenG


1 Answers

Unfortunately, I don't believe you can - per the CloudFormation documentation:

After the instance is running, you can modify only the DeleteOnTermination parameter for the attached volumes without interrupting the instance. Modifying any other parameter results in instance replacement.

like image 75
alex Avatar answered Sep 11 '25 08:09

alex