Sub topic 11
Persistent storage with EBS volumes
This chapter assumes you can launch an instance with the AWS CLI and know what an Availability Zone is. It covers Amazon Elastic Block Store (EBS), the storage that keeps your data when an instance stops.
What an EBS volume is
An EBS volume is a network-attached block device. The instance sees it as a disk, such as /dev/nvme1n1. The volume lives outside the instance, so it survives a stop and start. EBS replicates data within one Availability Zone.
Three facts shape most decisions:
- A volume exists in one Availability Zone. It can attach only to instances in that same zone.
- A volume attaches to one instance at a time. Only Multi-Attach on certain io1 and io2 volumes is an exception.
- The root volume holds the operating system. You can add more volumes for data.
Volume types
Pick a type by the performance the workload needs.
| Type | Backing | Typical use |
|---|---|---|
| gp3 | SSD | Default for most workloads, boot volumes |
| io2 | SSD | Databases that need high, steady IOPS |
| st1 | HDD | Large sequential reads, such as logs |
| sc1 | HDD | Cold data that is rarely read |
gp3 lets you set size, IOPS, and throughput separately. Start there unless you have a measured reason to choose another type.
Create, attach, and mount a volume
Create a volume in the same zone as the instance, then attach it.
aws ec2 create-volume \
--availability-zone us-east-1a \
--size 20 \
--volume-type gp3 \
--tag-specifications 'ResourceType=volume,Tags=[{Key=Name,Value=tutorial-data}]'
aws ec2 attach-volume \
--volume-id vol-0123456789abcdef0 \
--instance-id i-0123456789abcdef0 \
--device /dev/sdfA new volume has no file system. Run these commands on the instance. The device name may appear as nvme1n1 instead of sdf, so check with lsblk first.
lsblk
sudo mkfs -t xfs /dev/nvme1n1
sudo mkdir /data
sudo mount /dev/nvme1n1 /dataRun mkfs only on a new, empty volume. It erases any existing data.
Volumes and the instance lifecycle
The root volume has DeleteOnTermination set to true by default, so terminating the instance deletes it. Extra volumes you attach keep the default of false, so they remain after termination and keep costing money.
Common errors
- The volume will not attach. The volume and instance are in different Availability Zones.
- The mount disappears after a reboot. A
mountcommand does not persist. Add an entry to/etc/fstab, preferably using the file system UUID fromblkid. - The disk is full after you resized the volume. Resizing the volume does not resize the file system. Grow the file system as well, for example with
xfs_growfs.
You can change a volume's size, type, or IOPS while it is in use with aws ec2 modify-volume. You can increase size but not decrease it.
Exercise
You attached a new 20 GiB volume to your instance, but mount /dev/nvme1n1 /data fails with a message about a missing file system. What is the cause and what is the fix?
Show answer
A new EBS volume is a raw block device with no file system. Create one with sudo mkfs -t xfs /dev/nvme1n1, then run the mount command again. Do this only on a new volume, because mkfs erases existing data.
Exercise
You terminate an instance that has a root volume and one extra data volume, both with default settings. Which volume remains, and why does it matter?
Show answer
The data volume remains, because only the root volume has DeleteOnTermination set to true by default. The remaining volume keeps billing until you delete it.