Sub topic 8
Root volumes, AMIs and DeleteOnTermination
This chapter assumes you know how to launch an EC2 instance, what an AMI is, and how to run aws ec2 commands. It also assumes you have read the earlier chapters on volume lifecycle and snapshots. The chapter covers where an instance's boot disk comes from, how an AMI describes it, and which settings decide whether a volume survives instance termination.
What a root volume is
The root volume holds the operating system an instance boots from. For an EBS-backed instance it is an ordinary EBS volume, so it has a volume type, size, IOPS and encryption setting like any other. It is attached at the instance's root device name, which comes from the AMI. Common names are /dev/xvda (Amazon Linux) and /dev/sda1 (Ubuntu, many other images). On Nitro instances the operating system sees it as an NVMe device such as /dev/nvme0n1, whatever name the API reports.
Every EBS-backed instance gets exactly one root volume. Any further volumes are data volumes. The two differ mainly in defaults, which is the main subject of this chapter.
EBS-backed and instance store-backed AMIs
An AMI is a template that records how to build an instance's disks. The root device type is one of two kinds.
| Property | EBS-backed AMI | Instance store-backed AMI |
|---|---|---|
| Root device | EBS volume created from a snapshot | Image copied from S3 to local disk |
| Survives stop | Yes. The instance can be stopped and started. | No stop. It can only run or terminate. |
| Boot time | Fast | Slower, because the image is copied first |
| Root size limit | Up to the EBS volume limit | 10 GiB |
| Data after termination | Controlled by DeleteOnTermination | Always lost |
| Current use | Standard | Rare and legacy |
Nearly every AMI you will use is EBS-backed. The rest of this chapter assumes that.
How an AMI points at snapshots
An EBS-backed AMI is not a copy of a disk. It is metadata plus a block device mapping. The mapping lists each device, and for each EBS device it names a snapshot, a size, a volume type and flags such as DeleteOnTermination. When you launch the AMI, EC2 creates a new volume from each mapped snapshot and attaches it.
The diagram below shows that flow.
Two consequences follow from this design.
- The launch-time mapping can override the AMI. You can make the root volume larger, change its type, or encrypt it without editing the AMI.
- A new volume made from a snapshot is lazily loaded. Blocks are fetched from S3 the first time they are read, so the first read of each block is slower. The restore chapter covers initialization in detail.
Overriding the root volume at launch
To change the root volume, supply a block device mapping for the root device name. If you give the wrong device name, EC2 adds a second volume and leaves the root unchanged. Check the AMI's root device name first.
aws ec2 describe-images \
--image-ids ami-0123456789abcdef0 \
--query 'Images[0].[RootDeviceName,BlockDeviceMappings]'Then launch with the matching name. This example gives a 30 GiB encrypted gp3 root volume that is deleted on termination.
aws ec2 run-instances \
--image-id ami-0123456789abcdef0 \
--instance-type t3.micro \
--subnet-id subnet-0123456789abcdef0 \
--block-device-mappings '[{"DeviceName":"/dev/xvda","Ebs":{"VolumeSize":30,"VolumeType":"gp3","Encrypted":true,"DeleteOnTermination":true}}]'The size cannot be smaller than the snapshot the AMI references. Enlarging the volume does not enlarge the file system by itself. Many Linux AMIs run cloud-init growpart on first boot, which expands the root partition and file system. If yours does not, extend them manually as described in the Elastic Volumes chapter.
DeleteOnTermination
DeleteOnTermination is a per-attachment flag. It controls what happens to the volume when the instance terminates. It has no effect when you stop an instance, because stopping keeps all EBS volumes attached.
The defaults are a common source of surprise.
- The root volume defaults to
true. Terminate the instance and the volume is gone. - A volume you attach to a running instance with
attach-volumedefaults tofalse, so it survives. - A data volume defined in a launch block device mapping or an AMI takes the value in the mapping. The default differs between the API, the CLI and the console, so do not rely on it.
State the flag explicitly whenever the data matters.
Inspecting and changing the flag
Read the current value for every volume on an instance:
aws ec2 describe-instances \
--instance-ids i-0123456789abcdef0 \
--query 'Reservations[].Instances[].BlockDeviceMappings[].[DeviceName,Ebs.VolumeId,Ebs.DeleteOnTermination]' \
--output tableChange it on a running instance:
aws ec2 modify-instance-attribute \
--instance-id i-0123456789abcdef0 \
--block-device-mappings '[{"DeviceName":"/dev/xvda","Ebs":{"DeleteOnTermination":false}}]'With the flag set to false, terminating the instance leaves the root volume in the available state. You can then attach it to another instance as a data volume, which is a standard way to recover files from an instance that will not boot. A kept volume keeps billing until you delete it.
Termination protection is a different control
Termination protection (DisableApiTermination) blocks the terminate API call. DeleteOnTermination decides what happens after termination is allowed. They are independent. Use protection to prevent accidents. Use the flag to decide whether data outlives the instance.
Creating an AMI from an instance
You can capture a running or stopped instance as a new AMI. EC2 snapshots every attached EBS volume and registers an AMI whose block device mapping points at those snapshots.
aws ec2 create-image \
--instance-id i-0123456789abcdef0 \
--name "web-golden-2026-11-26" \
--description "Patched web server base image"By default EC2 reboots the instance first, so the file systems are quiesced and the image is consistent. Adding --no-reboot skips the reboot. The snapshot is then crash-consistent only, like pulling the power cord, and data still in memory buffers may be missing. Prefer the default for anything that writes to disk. For databases, stop the service or flush and freeze the file system before imaging.
The new AMI keeps the DeleteOnTermination values the source instance had at that moment. An AMI built from an instance where you changed the flag carries that change into every launch.
Cleaning up an AMI
Deregistering an AMI does not delete its snapshots. Snapshots keep costing storage until you remove them. The safe order is:
- Note the snapshot IDs from the AMI's block device mappings.
- Deregister the AMI.
- Delete each snapshot.
aws ec2 deregister-image --image-id ami-0123456789abcdef0
aws ec2 delete-snapshot --snapshot-id snap-0123456789abcdef0EC2 refuses to delete a snapshot that a registered AMI still uses, which is why deregistering comes first.
Replacing a root volume
If a boot disk is corrupted or you want to return to a clean image, you can swap the root volume in place. create-replace-root-volume-task restores the root volume to its launch state, or to a snapshot or AMI you choose, without changing the instance ID, IP addresses or other volumes.
aws ec2 create-replace-root-volume-task \
--instance-id i-0123456789abcdef0 \
--delete-replaced-root-volumeWithout --delete-replaced-root-volume, the old root volume is kept as an available volume, and you pay for it until you delete it. Everything on the old root volume is gone from the instance after the swap, so copy anything you need first.
Common errors
- Wrong device name in the mapping. The override creates an extra volume instead of changing the root. Check
RootDeviceNamefirst. - Assuming data volumes survive. A volume created from a launch mapping may have the flag set to
true. Verify before you rely on it. - Imaging without a reboot.
--no-rebootcan produce an inconsistent image. - Leaving snapshots behind. Deregistering does not free snapshot storage.
- Encryption mismatch. An AMI built from an unencrypted snapshot can still launch with an encrypted root volume through the mapping. Going the other way, from encrypted to unencrypted, is not possible.
Exercise
You launch an instance with a root volume and a second data volume that you attach later with attach-volume. You terminate the instance. Without changing any settings, which volumes remain, and what should you do afterwards?
Show answer
The root volume is deleted because its DeleteOnTermination flag defaults to true. The volume attached with attach-volume remains in the available state because that flag defaults to false. You should either reattach it to another instance or delete it, because it keeps billing until removed.
Exercise
You deregistered an AMI last month, yet your EBS snapshot costs did not fall. Why, and how do you fix it?
Show answer
Deregistering an AMI does not delete the snapshots that back it. Find the snapshot IDs, which should be recorded before deregistering, and remove them with delete-snapshot. Check that no other AMI or volume workflow still needs them first.