Sub topic 6
Creating, attaching, detaching and deleting volumes
This chapter covers the four lifecycle operations for an EBS volume. It assumes you know what an instance, an AMI and an Availability Zone are, and that you can run AWS CLI commands. Formatting and mounting come in the next chapter.
The lifecycle at a glance
A volume moves through a small set of states. It is created as available, becomes in-use once attached, returns to available when detached, and is removed on delete.
Creating a volume
A volume lives in one Availability Zone. You choose the zone, type and size when you create it. This command creates an encrypted 100 GiB gp3 volume with the baseline performance stated explicitly.
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}]'The output includes a VolumeId. Tagging at creation time makes volumes easier to find later, and it helps when you hunt for unused ones.
Attaching a volume
The volume and the instance must be in the same Availability Zone. The volume must also be available.
aws ec2 attach-volume \
--volume-id vol-0123456789abcdef0 \
--instance-id i-0123456789abcdef0 \
--device /dev/sdfThe --device value is the name you request. On Nitro instances the operating system exposes the volume as an NVMe device such as /dev/nvme1n1, and the number does not necessarily match the name you asked for. Check with lsblk instead of assuming.
Detaching a volume
Unmount the file system first. Detaching a volume that is still mounted can leave cached writes unflushed and risks data corruption.
sudo umount /data
aws ec2 detach-volume --volume-id vol-0123456789abcdef0Wait for the state to return to available before reusing or deleting the volume. You can check it with aws ec2 describe-volumes --volume-ids vol-0123456789abcdef0.
A detached volume keeps its data. You can attach it to another instance in the same zone.
Deleting a volume
Deleting is permanent. Only an available volume can be deleted.
aws ec2 delete-volume --volume-id vol-0123456789abcdef0If you might need the data again, take a snapshot first. Snapshots are covered in a later chapter.
Common errors
| Symptom | Likely cause |
|---|---|
| `InvalidVolume.ZoneMismatch` on attach | Volume and instance are in different Availability Zones |
| `VolumeInUse` on delete | The volume is still attached or detaching |
| Device name not found in the OS | Nitro instances rename devices to `/dev/nvmeXn1`; use `lsblk` |
| Volume stuck in `busy` while detaching | The file system is still mounted or in use; unmount it and retry |
| Charges continue after a project ends | Detached `available` volumes are still billed until deleted |
Exercise
You created a volume in us-east-1a, but attaching it to an instance in us-east-1b fails. What are two ways to fix this?
Show answer
A volume can only attach to an instance in its own Availability Zone. Either launch or use an instance in us-east-1a, or take a snapshot of the volume and create a new volume from that snapshot in us-east-1b, then attach the new volume.
Exercise
Why should you unmount a file system before running detach-volume, and what state confirms the detach finished?
Show answer
Unmounting flushes cached writes so the file system is consistent. Detaching while mounted risks data corruption. The volume is fully detached when describe-volumes shows its state as available.