Sub topic 6
Creating, attaching, detaching and deleting volumes
This chapter covers the four lifecycle operations for an EBS volume and the states a volume passes through. It assumes you know what an Availability Zone is, can use the AWS CLI with credentials that can call EC2, and have read the earlier chapters on volume types. Formatting and mounting are in the next chapter.
The volume lifecycle
An EBS volume is a separate resource from the instance it serves. You can create it without an instance, move it between instances, and keep it after the instance is gone. Each volume has a state, and each operation is only valid in certain states.
| State | Meaning |
|---|---|
| creating | EBS is allocating the volume. |
| available | The volume exists and is not attached to any instance. |
| in-use | The volume is attached to at least one instance. |
| deleting, deleted | The volume is being removed, or is gone. |
| error | Creation failed. Delete the volume and create it again. |
Creating a volume
A volume lives in exactly one Availability Zone. You choose the zone at creation, and it cannot change later. To move data to another zone you use a snapshot, which a later chapter covers.
aws ec2 create-volume \
--availability-zone us-east-1a \
--volume-type gp3 \
--size 100 \
--iops 3000 \
--throughput 125 \
--encrypted \
--tag-specifications 'ResourceType=volume,Tags=[{Key=Name,Value=data-vol}]'Notes on this command:
--sizeis in GiB. If you create from a snapshot (--snapshot-id), the size can be omitted. It defaults to the snapshot size, and you can only request the same size or larger.- For gp3, 3,000 IOPS and 125 MiB/s are the baseline, so stating them is optional. They document your intent and make the command explicit. For io1 and io2 you must give
--iops. --encrypteduses the default KMS key for EBS unless you add--kms-key-id. If you create from an encrypted snapshot, the volume is always encrypted. If your account has encryption by default turned on, new volumes are encrypted whether or not you pass the flag.- Tagging at creation with
--tag-specificationsavoids a window in which an untagged volume exists. This matters if your IAM policies or cost reports depend on tags.
The response includes the VolumeId. In scripts, capture it directly and wait for the volume to become usable:
VOLUME_ID=$(aws ec2 create-volume \
--availability-zone us-east-1a \
--volume-type gp3 \
--size 100 \
--query VolumeId \
--output text)
aws ec2 wait volume-available --volume-ids "$VOLUME_ID"
echo "Volume $VOLUME_ID is available"The wait subcommand polls describe-volumes until the state matches. It exits with an error if the wait times out, so a script using set -e stops instead of continuing with a volume that is not ready.
Attaching a volume
Attach the volume to an instance in the same Availability Zone. The instance must be running or stopped. You do not need to stop it.
aws ec2 attach-volume \
--volume-id vol-0123456789abcdef0 \
--instance-id i-0123456789abcdef0 \
--device /dev/sdfThe command returns while the volume is still in the attaching attachment state. Wait for in-use before you work on the instance:
aws ec2 wait volume-in-use --volume-ids vol-0123456789abcdef0Device names
The --device value is the name you request, and the operating system may not use it. On Nitro-based instances, EBS volumes appear as NVMe block devices such as /dev/nvme1n1. The kernel numbers NVMe devices in the order it discovers them, so the number does not reliably match your attachment order. Do not hard-code /dev/nvme1n1 in automation. Instead:
- Run
lsblkand match the device by size. - Look under
/dev/disk/by-id/. Entries there contain the volume ID without the hyphen, for examplenvme-Amazon_Elastic_Block_Store_vol0123456789abcdef0. - Mount by file system UUID, as described in the next chapter.
AWS recommends names in the range /dev/sd[f-p] for data volumes. The name /dev/sda1 is typically the root device.
Common attach errors
| Error | Cause and fix |
|---|---|
| `InvalidVolume.ZoneMismatch` | The volume and instance are in different Availability Zones. Create a volume in the instance's zone, or snapshot and restore into it. |
| `VolumeInUse` | The volume is attached elsewhere. Detach it first, unless Multi-Attach is enabled. |
| `IncorrectState` | The volume is not `available`, or the instance is in a state that does not allow attachment, such as terminated. |
| `InvalidParameterValue` for the device | The device name is already used by another attachment on this instance. |
| `AttachmentLimitExceeded` | The instance type has reached its limit on attached volumes. Check the limit for your instance family. |
Detaching a volume
Detaching removes the connection between the instance and the volume. The data stays on the volume. Do it in this order:
- Stop any application using the volume.
- Unmount the file system from inside the instance.
- Call
detach-volume. - Wait for the volume to return to
available.
sudo umount /data
aws ec2 detach-volume --volume-id vol-0123456789abcdef0
aws ec2 wait volume-available --volume-ids vol-0123456789abcdef0If you detach without unmounting, the operating system still holds cached writes and the file system may be left in an inconsistent state. If the instance then fails to boot or mount it cleanly, you must run a file system check. For the same reason, remove or comment out the volume's line in /etc/fstab if you will not reattach it. The nofail option from the next chapter keeps boot from blocking, but a stale entry is still clutter.
You cannot detach the root volume of a running instance. Stop the instance first. Detaching a root volume from a stopped instance and attaching it to a second instance as a data volume is a standard rescue technique for fixing a broken boot configuration.
Force detach
If a volume is stuck in the busy state because the instance is not responding, --force is available:
aws ec2 detach-volume --volume-id vol-0123456789abcdef0 --forceUse it only as a last resort. The instance gets no chance to flush buffers, so the volume can lose recent writes or be left with a corrupted file system. Forcing a detach does not fix the underlying problem. Check the instance first.
Deleting a volume
You can delete a volume only when it is available. Deletion is permanent, and the data cannot be recovered afterward. Snapshots of the volume are separate resources and are not deleted with it.
aws ec2 create-snapshot \
--volume-id vol-0123456789abcdef0 \
--description 'final copy before delete'
aws ec2 delete-volume --volume-id vol-0123456789abcdef0
aws ec2 wait volume-deleted --volume-ids vol-0123456789abcdef0If you are not certain the data is disposable, take a final snapshot first. Wait until the snapshot is completed before you delete the volume, so you do not lose the only copy if something goes wrong.
DeleteOnTermination
Every attachment has a DeleteOnTermination flag. It decides what happens to the volume when the instance is terminated.
- For the root volume, it defaults to
true. Terminating the instance deletes the root volume. - For volumes attached after launch with
attach-volume, it defaults tofalse. They survive termination and stayavailable. - For data volumes defined in a launch block device mapping, the default is
false.
You can change the flag on a running instance:
aws ec2 modify-instance-attribute \
--instance-id i-0123456789abcdef0 \
--block-device-mappings '[{"DeviceName":"/dev/sdf","Ebs":{"DeleteOnTermination":true}}]'The default of false for data volumes protects data, but it also leaves orphaned volumes behind. An available volume still bills at its full provisioned rate. A periodic check for volumes in that state is a cheap way to find waste:
aws ec2 describe-volumes \
--filters Name=status,Values=available \
--query 'Volumes[].{Id:VolumeId,Size:Size,Created:CreateTime}' \
--output tableReview the list before deleting anything. A volume may be available on purpose, for example while it is being moved between instances.
Summary of ordering rules
- Create the volume in the same zone as the instance that will use it.
- Wait for
availablebefore attaching, andin-usebefore using. - Unmount before detaching, and wait for
availablebefore deleting. - Snapshot before deleting anything you might need.
Exercise
You run aws ec2 attach-volume for a new volume and get InvalidVolume.ZoneMismatch. Name the cause and two ways to resolve it. Also explain why /dev/nvme1n1 is a risky device name to hard-code in a startup script.
Show answer
The volume and the instance are in different Availability Zones, and a volume can only attach within its own zone. You can create a new volume in the instance's zone, or create a snapshot of the existing volume and restore it into the instance's zone with create-volume --snapshot-id ... --availability-zone .... NVMe device numbers depend on the order in which the kernel discovers devices, so the number does not reliably match the volume you attached. Use the /dev/disk/by-id/ link that contains the volume ID, or mount by file system UUID.
Exercise
A volume holding a database has been detached and is in the available state. A teammate wants to delete it to save money. What do you check and do first, and what is the cost of leaving it as it is?
Show answer
Confirm that the data is no longer needed, and take a snapshot and wait for it to reach completed, because deleting a volume is permanent and cannot be undone. Snapshots of a volume are not deleted with it, so the snapshot preserves the data. While the volume stays available it still bills at its full provisioned size and performance rates, even though no instance uses it.