Resize EC2 Root EBS Volumes with CloudFormation, Without Replacing the Instance
A step-by-step guide to growing the root EBS volume of a CloudFormation-managed EC2 instance in place, with no instance replacement and no downtime, including the cleanup, import, and reference patterns that make it repeatable, validated end to end on Windows and Linux.
Introduction
You can grow the root EBS volume of a CloudFormation-managed EC2 instance in place, with no instance replacement and no downtime. This guide shows how, and why the default approach fails. AWS already has the building blocks; this guide combines them into a structured, validated procedure.
Teams that manage EC2 with CloudFormation encounter this limitation: they need to grow a server's root disk, they bump the volume size in the template, they deploy the update, and CloudFormation replaces the instance. A routine "give this box more C:/root space" turns into a destroyed and recreated server, a new instance ID, lost instance-store data, and an unplanned outage.
This hits organizations that adopted infrastructure-as-code after a migration especially hard. A frequent pattern looks like this: a migration tool lifts on-premises servers into AWS and generates a CloudFormation template per instance. Because it seeds each instance from a temporary image that it deletes once cutover completes, it leaves behind running instances whose source AMI no longer exists. The generated templates define the root volume inline in the instance's
BlockDeviceMappings , and sometimes redundantly manage the same physical volumes a second time as standalone resources with hardcoded IDs. When an operator later tries to enlarge the root volume through the template, CloudFormation plans a replacement, and with no source AMI to relaunch from, that update can wedge the stack in a failed rollback. At fleet scale (hundreds of migrated instances), this is a real operational risk.Two AWS features make the in-place approach work:
- EBS Elastic Volumes let you modify a volume's size on a running instance, with no stop, no detach, and no reboot.
- CloudFormation resource import lets you bring an existing volume under stack management as a standalone
AWS::EC2::Volume, whoseSizeproperty supports in-place updates.
In this blog we reproduce the replacement behavior, then walk through the validated procedure on both Windows Server and Linux:
By the end, you will have:
- A clear demonstration of why changing
VolumeSizeinside an instance'sBlockDeviceMappingsforces a replacement
- A safe cleanup procedure for stacks that dual-manage the same volumes (inline plus standalone with hardcoded IDs)
- A repeatable, in-place root-volume resize that keeps the instance running, validated across two consecutive resizes (30 → 45 → 55 GiB) with the OS filesystem extended in the guest
- A recommended reference template for new deployments, plus a recovery path for instances whose source AMI no longer exists
Note: Everything here was validated in a lab that scripts each stack transition and asserts the expected outcome, capturing every command and result into an evidence log. The commands and figures below are drawn from that run.
The core problem: inline BlockDeviceMappings and replacement
When a root volume is defined inline on the instance:
Instance: Type: AWS::EC2::Instance Properties: BlockDeviceMappings: - DeviceName: /dev/sda1 # /dev/xvda on Linux Ebs: VolumeType: gp3 VolumeSize: 30 # changing this forces replacement DeleteOnTermination: falseCloudFormation's update behavior for the instance's block device mappings is unambiguous: on a running instance, the only
Ebs property you can change without interruption is DeleteOnTermination . Changing VolumeSize , Iops , Throughput , VolumeType , Encrypted , KmsKeyId , or SnapshotId inline results in instance replacement.You can see this before executing anything, by inspecting a change set:
$ aws cloudformation describe-change-set --stack-name rvlab \ --change-set-name size-bump \ --query 'Changes[].ResourceChange.[LogicalResourceId,Action,Replacement]' --output table---------------------------------------------------| DescribeChangeSet |+-----------------------+---------+---------------+| Instance | Modify | Conditional |+-----------------------+---------+---------------+
$ aws cloudformation describe-change-set --stack-name rvlab \ --change-set-name size-bump \ --query "Changes[?ResourceChange.LogicalResourceId=='Instance'].ResourceChange.Details[].Target.[Attribute,Name,RequiresRecreation]" --output table--------------------------------------------------------| DescribeChangeSet |+------------+-----------------------+-----------------+| Properties| BlockDeviceMappings | Conditionally |+------------+-----------------------+-----------------+The
Replacement: Conditional / RequiresRecreation: Conditionally flag on BlockDeviceMappings is the signal to watch for. For a running instance the condition resolves to replacement, and no inline configuration avoids it. On instances with no recoverable source AMI, this is what turns a routine resize into a stuck stack.The approach at a glance
The root device itself must always come from the AMI at launch. It cannot be modeled as a standalone
AWS::EC2::Volume + AWS::EC2::VolumeAttachment (a root volume can't be attached or detached on a running instance, and suppressing the AMI's root mapping breaks health checks). So the procedure is:- Keep the root volume inline in
BlockDeviceMappings, but treat that block as immutable after deployment.
- Import the live root volume into the stack a second time, as a standalone
AWS::EC2::Volume(importable by volume ID). ItsSizeproperty supports in-place updates.
- Resize by changing
Sizeon that standalone resource. CloudFormation callsModifyVolume(EBS Elastic Volumes) under the hood, and the instance never stops.
- Extend the OS filesystem inside the guest.
- Reconcile the instance's now-stale inline
VolumeSizewith a retain/remove/re-import cycle, so the stack state matches reality and drift stays clean.
The rest of this guide walks each of these in order, plus the cleanup for stacks that were generated with duplicate volume management.
Environment at a glance
The lab uses a single Windows Server instance (a Nitro-based
t3.medium ) so the OS-side steps can be shown concretely. The CloudFormation mechanics are identical on Linux, and the Linux filesystem commands are given alongside. | |
|---|---|
Instance | t3.medium (Nitro, required for online root Elastic Volumes) |
OS | Windows Server 2022 (Linux notes provided throughout) |
Root device | /dev/sda1 (Windows) · /dev/xvda or /dev/nvme0n1 (Linux) |
Volume type | gp3, encrypted optional |
Access | AWS Systems Manager (Session Manager / Run Command), no SSH key required |
Stack policies | DeletionPolicy: Retain + UpdateReplacePolicy: Retain on instance and volumes |
Why Nitro matters: EBS Elastic Volumes modifications apply to a running instance without a stop/start on Nitro-based instance types. The root volume grows online; only the OS-level partition/filesystem extension remains, and that is also done live.
Prerequisites
- Account: an AWS account with permissions to manage CloudFormation, EC2, EBS, SSM, and IAM.
- A CloudFormation-managed EC2 instance whose root volume is defined inline in
BlockDeviceMappings(the situation this guide addresses).
- Retain policies: the ability to set
DeletionPolicy: RetainandUpdateReplacePolicy: Retainon the instance and volume resources.
- SSM access to the instance (managed instance role with
AmazonSSMManagedInstanceCore) for the OS-side filesystem extension, with no SSH/RDP required.
- A backup: an EBS snapshot of the root volume before any resize. If the fleet already uses AWS Backup, those AMIs/snapshots serve as both operational recovery and, for the no-AMI case, golden images.
- Pre-checks (details in Step 3): partition style (MBR vs GPT), and whether the instance is in an Auto Scaling Group.
Step 1: Pre-flight checks and a snapshot
Before touching anything, capture the current state and a rollback point.
Identify the root volume and confirm the instance is running:
ROOT_VOL=$(aws ec2 describe-instances --instance-ids i-0123456789abcdef0 \ --query "Reservations[0].Instances[0].BlockDeviceMappings[?DeviceName=='/dev/sda1'].Ebs.VolumeId | [0]" \ --output text)echo "$ROOT_VOL"Take a snapshot and wait for completion. This is your rollback path:
SNAP=$(aws ec2 create-snapshot --volume-id "$ROOT_VOL" \ --description "pre-resize backup" --query SnapshotId --output text)aws ec2 wait snapshot-completed --snapshot-ids "$SNAP"Partition-style pre-check (critical above 2 TiB). MBR-partitioned disks cannot be grown beyond 2 TiB. Confirm the layout first:
- Windows (PowerShell):
Get-Disk | Select-Object Number, PartitionStyle, Size
- Linux:
sudo parted /dev/nvme0n1 print | grep "Partition Table"
If the disk is MBR and your target exceeds 2 TiB, a GPT conversion is required first (a separate, stop/start procedure). Below 2 TiB, MBR is fine.
Auto Scaling Group check. If the instance is part of an ASG, this guide does not apply. Resize is driven through the Launch Template (new version + instance refresh) for future instances, and
aws ec2 modify-volume directly for running members. Confirm the instance is standalone before proceeding.Step 2: Clean up dual-managed stacks (if applicable)
Migration-generated templates often manage the same physical volumes twice: once inline in the instance's
BlockDeviceMappings , and again as standalone AWS::EC2::Volume resources, with AWS::EC2::VolumeAttachment resources that hardcode physical volume IDs, sometimes including an invalid attachment for the root device. Clean this up before resizing.2a. Add Retain policies to everything. This is a policy-only update with no property changes, so it completes with no replacement or interruption:
Instance: Type: AWS::EC2::Instance DeletionPolicy: Retain UpdateReplacePolicy: Retain # ...DataVolume: Type: AWS::EC2::Volume DeletionPolicy: Retain UpdateReplacePolicy: Retain # ...DataVolumeAttachment: Type: AWS::EC2::VolumeAttachment DeletionPolicy: Retain UpdateReplacePolicy: Retain # ...2b. Leave hardcoded volume IDs alone; do not rewrite them to
!Ref . CloudFormation tracks the reference source, not just the resolved value, so swapping a hardcoded ID for an equivalent !Ref is detected as a change:$ aws cloudformation describe-change-set --stack-name rvlab \ --change-set-name ref-swap \ --query 'Changes[].ResourceChange.[LogicalResourceId,Action,Replacement]' --output table--------------------------------------------| DescribeChangeSet |+-----------------------+---------+--------+| DataVolumeAttachment | Modify | True |+-----------------------+---------+--------+Because all
VolumeAttachment properties are immutable, executing that change attempts a replacement, which means detaching and re-attaching. For an already-attached volume the attach fails with VolumeInUse , and the stack rolls back. Leave hardcoded IDs as they are.2c. Remove the invalid root-device VolumeAttachment (and any attachments you want to stop managing). With
DeletionPolicy: Retain , deleting the resource from the template drops it from stack state without calling DetachVolume , so the volume stays attached:# after removing the VolumeAttachment resource from the template:aws cloudformation update-stack --stack-name rvlab \ --template-body file://template-without-va.yaml --capabilities CAPABILITY_IAM# verify the volume is still attached afterward:aws ec2 describe-volumes --volume-ids "$ROOT_VOL" \ --query 'Volumes[0].Attachments[0].[InstanceId,State]'This removal is a one-way door: a
VolumeAttachment cannot be re-added for an already-attached volume (the attach fails with VolumeInUse ) and the resource type is not importable. That is exactly why the root-device attachment, which can never be lifecycle-managed on a running instance, must go.2d. Run drift detection. After cleanup, expect the volume resources to be
IN_SYNC . The instance itself may show one cosmetic difference: any volume that is attached but no longer modeled in the template appears as an ADD on the instance's BlockDeviceMappings . This reflects the intended state and requires no action; only a mismatch on the root VolumeSize would indicate a real problem.Step 3: Bring the root volume under management and resize it in place
Import the live root volume into the stack as a standalone
AWS::EC2::Volume . Add the resource to the template (omit SnapshotId / Iops / Throughput you don't want drift-checked; import only checks properties you specify):RootVolume: Type: AWS::EC2::Volume DeletionPolicy: Retain UpdateReplacePolicy: Retain Properties: AvailabilityZone: ap-southeast-2a # a fixed value, not GetAtt on the instance Size: 30 VolumeType: gp3Import it by volume ID:
aws cloudformation create-change-set --stack-name rvlab \ --change-set-name import-root --change-set-type IMPORT \ --resources-to-import '[{"ResourceType":"AWS::EC2::Volume","LogicalResourceId":"RootVolume","ResourceIdentifier":{"VolumeId":"'"$ROOT_VOL"'"}}]' \ --template-body file://template-with-rootvolume.yaml --capabilities CAPABILITY_IAMaws cloudformation execute-change-set --stack-name rvlab --change-set-name import-rootNow resize by changing the
Size property and updating the stack. CloudFormation issues ModifyVolume , an online Elastic Volumes operation:# RootVolume Size: 30 -> 45 in the template, then:aws cloudformation update-stack --stack-name rvlab \ --template-body file://template-rootvolume-45.yaml --capabilities CAPABILITY_IAM
aws ec2 describe-volumes-modifications --volume-ids "$ROOT_VOL" \ --query 'VolumesModifications[0].[ModificationState,OriginalSize,TargetSize]' --output table------------------------------|DescribeVolumesModifications|+----------------------------+| optimizing || 30 || 45 |+----------------------------+The instance stays running throughout, with no stop, no reboot, and no replacement. Once the modification reaches
optimizing or completed , the new size is visible to the OS.Important: do the resize through CloudFormation as shown, not out-of-band. If you resize the volume with the CLI first and only later update the template's
Size , CloudFormation will call ModifyVolume with the volume's current size on the next update and fail with InvalidParameterValue: No modifications were requested .Step 4: Extend the OS filesystem
Growing the EBS volume gives the OS more block device, but the partition and filesystem must still be extended. This runs live on the running instance, in a single SSM Run Command call (no SSH/RDP).
Windows. Windows requires a disk rescan before it sees the new size (
Update-HostStorageCache alone is not sufficient). Windows Server 2022 also frequently places a Recovery partition at the end of the disk, which sits between C: and the new free space and blocks extension. Detect it and handle it per your organization's WinRE policy:# via AWS-RunPowerShellScript'rescan' | diskpart | Out-Null$c = Get-Partition -DriveLetter C# If a Recovery partition trails C:, disable WinRE and remove it before extending.$max = (Get-PartitionSupportedSize -DriveLetter C).SizeMaxResize-Partition -DriveLetter C -Size $max(Get-Volume -DriveLetter C).SizeLinux. Grow the partition, then the filesystem:
# NVMe root, partition 1sudo growpart /dev/nvme0n1 1# ext4:sudo resize2fs /dev/nvme0n1p1# xfs:sudo xfs_growfs -d /Either way, an automated option with a built-in backup is the AWS Systems Manager Automation runbook
AWSPremiumSupport-ExtendVolumesOnWindows (Business/Enterprise Support), which snapshots, modifies, waits, and extends end to end.In the lab, the guest C: drive grew as expected after each resize:
Resize | EBS volume | Guest C: drive (reported) |
|---|---|---|
First (30 → 45 GiB) | 45 GiB | 48,316,280,832 bytes (~45 GiB) |
Second (45 → 55 GiB) | 55 GiB | 59,053,699,072 bytes (~55 GiB) |
Step 5: Reconcile stale instance state
Do this step to keep drift detection clean. After the resize, the standalone
RootVolume resource is correct, but the instance's inline BlockDeviceMappings.VolumeSize still shows the old value. CloudFormation drift-checks inline BDM VolumeSize against the live volume, so the instance reports MODIFIED drift until reconciled. This has no effect on instance behavior as long as the BDM block is never edited, but it pollutes drift reporting. Reconcile it with a retain/remove/re-import cycle:# 1. Instance already has DeletionPolicy: Retain. Remove the Instance resource# from the template and update; the physical instance keeps running:aws cloudformation update-stack --stack-name rvlab \ --template-body file://template-instance-removed.yaml --capabilities CAPABILITY_IAM
# 2. Re-import the instance with BDM VolumeSize set to the ACTUAL size (45),# and ImageId as a plain String (see note below):aws cloudformation create-change-set --stack-name rvlab \ --change-set-name reimport --change-set-type IMPORT \ --resources-to-import '[{"ResourceType":"AWS::EC2::Instance","LogicalResourceId":"Instance","ResourceIdentifier":{"InstanceId":"i-0123456789abcdef0"}}]' \ --template-body file://template-reconciled.yaml --capabilities CAPABILITY_IAMaws cloudformation execute-change-set --stack-name rvlab --change-set-name reimportTwo things that matter for the import template:
- IMPORT change sets cannot add or modify
Outputs. The import template's Outputs section must match the currently deployed template. Add outputs back in a normal follow-up update.
- For instances whose source AMI was deleted, declare the
ImageIdparameter as a plainString, notAWS::EC2::Image::Id. CloudFormation validates the AWS-specific type against live AMIs and will reject a deregistered ID, blocking the import. A running instance never needs its AMI to exist; the import just records the ID as state.
The import performs no volume API calls; it is pure state reconciliation. After it, drift returns to
IN_SYNC (aside from the cosmetic BDM ADD for any unmanaged attachment).Once the root volume is managed as a standalone resource, every future resize is a one-line change to
Size , validated across a second resize (45 → 55 GiB) with no replacement.The recommended pattern for new deployments
For instances you build fresh, structure the template so this problem never arises:
Resources: Instance: Type: AWS::EC2::Instance DeletionPolicy: Retain UpdateReplacePolicy: Retain Properties: ImageId: !Ref pImage BlockDeviceMappings: - DeviceName: /dev/sda1 # /dev/xvda on Linux Ebs: VolumeType: gp3 VolumeSize: !Ref pRootSize # set once at launch; treat as immutable DeleteOnTermination: false # ...
DataVolume: # every data volume: standalone + !Ref Type: AWS::EC2::Volume Properties: AvailabilityZone: !GetAtt Instance.AvailabilityZone Size: !Ref pDataSize # resizable in place, forever VolumeType: gp3
DataVolumeAttachment: Type: AWS::EC2::VolumeAttachment Properties: InstanceId: !Ref Instance # ALWAYS !Ref, never hardcode IDs VolumeId: !Ref DataVolume Device: xvdfTwo rules:
- Root volume: inline, and written once. It must come from the AMI at launch; treat the BDM block as immutable and, when you must grow it, use the import + standalone-Volume pattern above.
- Data volumes: standalone
AWS::EC2::Volume+VolumeAttachmentwith!Reffrom day one. TheirSizeis then an in-place update forever, with no import gymnastics needed. In the lab, growing a data volume from 20 → 30 GiB completed in place with no instance interruption.
Recovery for instances with no source AMI. If a migration deleted the source images, create a golden AMI from the running instance (
aws ec2 create-image ), or use your existing AWS Backup AMIs, which serve double duty. One caveat validated in the lab: create-image bakes every attached volume into the AMI, so an AMI of a multi-volume instance carries the data volume too. Launching that AMI through a template that also attaches a standalone data volume at the same device fails with "Attachment point already in use." Exclude data volumes at image-creation time so the golden AMI carries only the root:aws ec2 create-image --instance-id i-0123456789abcdef0 --name "golden-$(date +%Y%m%d)" \ --block-device-mappings '[{"DeviceName":"xvdf","NoDevice":""}]'And when launching from a golden AMI, omit the root
VolumeSize so EC2 defaults to the AMI's snapshot size. Specifying a value smaller than the snapshot fails at launch, and hardcoding the snapshot size breaks the next time the source is resized.Conclusion
Growing the root EBS volume of a CloudFormation-managed instance does not require replacing the instance, but the default inline
BlockDeviceMappings approach forces exactly that. By keeping the root volume inline yet immutable, importing it as a standalone AWS::EC2::Volume , and resizing through that resource, the volume grows online via EBS Elastic Volumes while the instance keeps running. We validated the full lifecycle end to end: reproducing the replacement behavior, cleaning up dual-managed stacks, resizing twice in place (30 → 45 → 55 GiB) with the guest filesystem extended each time, reconciling instance state via import, and a reference pattern for new builds, all on a single instance, with every step asserted and logged.The same mechanics apply whether the instance runs Windows or Linux, whether its source AMI still exists or was deleted during a migration, and whether you are resizing one server or hundreds.
The solution is one architectural change: manage the root volume as a standalone
AWS::EC2::Volume resource, not an inline instance property.Need guidance on migrating or operating EC2 at scale with CloudFormation? Contact your AWS account team to discuss your infrastructure-as-code and operations requirements.
To learn more:
Disclaimer: This article demonstrates a CloudFormation and Amazon EBS operational pattern validated in a lab environment on a single EC2 instance. Behavior can vary with instance type, operating system, volume configuration, and workload. Always test in a non-production environment and take a snapshot before resizing. Any opinions in this article are those of the author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article