Sub topic 11
Persistent storage with EBS volumes
This chapter assumes you can launch an instance with the AWS CLI, you know what an Availability Zone is, and you are comfortable with basic Linux disk commands such as lsblk and mount. It covers how Amazon EBS volumes work, how to create and attach one, how to make it usable, and the errors learners meet most often.
What an EBS volume is
Amazon Elastic Block Store (EBS) provides block storage volumes that you attach to an EC2 instance. A volume looks like a raw disk to the operating system. You create a file system on it, mount it, and use it like any other disk.
The volume lives separately from the instance. If you stop the instance, the volume keeps its data. If you detach the volume, you can attach it to another instance. EBS replicates data within a single Availability Zone, so one hardware failure does not lose the volume.
Three properties shape how you use EBS:
- A volume exists in one Availability Zone. It can attach only to instances in that same zone.
- Most volumes attach to one instance at a time. Multi-Attach is a limited exception for certain Provisioned IOPS SSD volumes on supported Nitro instance types.
- An instance can have several volumes attached. One of them is usually the root volume that holds the operating system.
The network sits between the instance and the volume, so EBS is not a physical disk inside the host. That is why a volume can outlive the instance and move between instances.
Volume types
You choose a volume type when you create the volume. The types fall into SSD-backed and HDD-backed groups.
| Type | Group | Typical use |
|---|---|---|
| gp3 | General Purpose SSD | Default choice for boot volumes, small databases, development |
| gp2 | General Purpose SSD | Older general purpose type, performance tied to size |
| io2 | Provisioned IOPS SSD | Latency-sensitive databases that need guaranteed IOPS |
| st1 | Throughput Optimized HDD | Large sequential reads and writes such as logs and data warehouses |
| sc1 | Cold HDD | Infrequently accessed data at the lowest cost |
For most workloads, start with gp3. It includes a baseline of 3,000 IOPS and 125 MiB/s of throughput regardless of size, and you can raise IOPS and throughput independently of capacity. With gp2, performance depends on the volume size, so people sometimes over-provision space just to get speed. Check the current EBS documentation for exact limits and prices, because they change.
HDD types (st1 and sc1) cannot be used as boot volumes.
Create and attach a volume
Create the volume in the same Availability Zone as the instance. Look up the zone first so you do not guess.
AZ=$(aws ec2 describe-instances \
--instance-ids i-0123456789abcdef0 \
--query 'Reservations[0].Instances[0].Placement.AvailabilityZone' \
--output text)
VOL_ID=$(aws ec2 create-volume \
--availability-zone "$AZ" \
--size 20 \
--volume-type gp3 \
--encrypted \
--tag-specifications 'ResourceType=volume,Tags=[{Key=Name,Value=tutorial-web-data}]' \
--query VolumeId --output text)
aws ec2 wait volume-available --volume-ids "$VOL_ID"
aws ec2 attach-volume \
--volume-id "$VOL_ID" \
--instance-id i-0123456789abcdef0 \
--device /dev/sdfThe wait command blocks until the volume reaches the available state, so the attach call does not fail on a volume that is still being created.
Attaching only connects the device. The operating system sees a new empty disk, and you still have to format and mount it.
Format and mount the volume
Connect to the instance over SSH. On Nitro-based instance types, the device name you gave (/dev/sdf) appears inside Linux as an NVMe device such as /dev/nvme1n1. The name you see can differ from the one you requested, so do not assume it.
lsblk
sudo file -s /dev/nvme1n1If file -s prints just data, the disk has no file system. Create one, mount it, and check the result.
sudo mkfs -t xfs /dev/nvme1n1
sudo mkdir -p /data
sudo mount /dev/nvme1n1 /data
df -h /dataRun mkfs only on a new, empty volume. It erases whatever file system was there before. If you attach a volume that already holds data, skip mkfs and go straight to mount.
Make the mount survive a reboot
A manual mount does not persist. Add an entry to /etc/fstab, and identify the disk by UUID because device names can change between boots.
UUID=$(sudo blkid -s UUID -o value /dev/nvme1n1)
echo "UUID=$UUID /data xfs defaults,nofail 0 2" | sudo tee -a /etc/fstab
sudo mount -aThe nofail option lets the instance finish booting even if the volume is missing. Without it, a missing volume can drop the boot into emergency mode, and you lose SSH access.
sudo mount -a tests the new entry right away. A typo in /etc/fstab is much cheaper to find now than at the next reboot.
Root volumes and Delete on Termination
Every EBS-backed instance has a root volume created from the AMI. A flag called DeleteOnTermination controls what happens to a volume when the instance is terminated.
- For the root volume, the default is
true. Terminating the instance deletes the volume. - For additional volumes attached at launch or afterward, the default is
false. The volume remains and keeps costing money.
You can check and change the flag:
aws ec2 describe-instances \
--instance-ids i-0123456789abcdef0 \
--query 'Reservations[0].Instances[0].BlockDeviceMappings[].[DeviceName,Ebs.VolumeId,Ebs.DeleteOnTermination]' \
--output table
aws ec2 modify-instance-attribute \
--instance-id i-0123456789abcdef0 \
--block-device-mappings '[{"DeviceName":"/dev/sdf","Ebs":{"DeleteOnTermination":true}}]'Set the flag to false on a root volume if you want to keep it after termination. Set it to true on scratch volumes you never want to pay for once the instance is gone.
Resizing and changing a volume
Elastic Volumes lets you change size, type, and performance of a volume while it is attached and in use.
aws ec2 modify-volume --volume-id "$VOL_ID" --size 40
aws ec2 describe-volumes-modifications --volume-ids "$VOL_ID"Three rules apply:
- You can increase size but not decrease it. To shrink, create a smaller volume and copy the data.
- After a size increase, the file system does not grow by itself. For XFS, run
sudo xfs_growfs -d /data. For ext4, runsudo resize2fs /dev/nvme1n1. - After one modification, you must wait before changing the same volume again. The documented minimum is six hours, and the volume must have left the
optimizingstate.
Encryption
EBS encryption uses AWS KMS keys. Encrypted volumes encrypt data at rest, data moving between the instance and the volume, and any snapshots made from the volume. You can turn on encryption by default for a Region in your account, so new volumes are always encrypted. Encryption has no separate setup on the instance, and its performance effect is small on current instance types.
An unencrypted volume cannot be encrypted in place. You snapshot it, copy the snapshot with encryption enabled, and create a new volume from the copy.
Common errors
InvalidVolume.ZoneMismatch: the volume and instance are in different Availability Zones. Create the volume in the instance's zone, or restore a snapshot into the right zone.VolumeInUse: the volume is still attached somewhere. Unmount it, detach it, and wait for theavailablestate.- Mount fails with "wrong fs type": the volume has no file system yet, or you used the wrong device name. Check with
lsblkandfile -s. - Space did not grow after resizing: you extended the volume but not the file system. Run the grow command for your file system.
- Instance does not boot after editing
/etc/fstab: a bad entry withoutnofail. Stop the instance, attach its root volume to a rescue instance, and fix the file. - Unexpected charges after termination: extra volumes with
DeleteOnTerminationset tofalsestay billed. List them withaws ec2 describe-volumes --filters Name=status,Values=available.
Summary
EBS gives an instance durable block storage that exists independently of it. A volume is tied to one Availability Zone, needs a file system and a mount before use, and keeps billing until you delete it. Pick gp3 unless you have a specific need, mount by UUID with nofail, and check DeleteOnTermination so nothing is left behind.
Exercise
You attached a new 20 GiB volume to an instance and it shows up as /dev/nvme1n1 in lsblk, but df -h does not list it. List the steps, in order, that make it usable and keep it mounted after a reboot. Also say which step you must skip if the volume already contained data.
Show answer
1) Check for a file system with sudo file -s /dev/nvme1n1; the output data means none exists. 2) Create one with sudo mkfs -t xfs /dev/nvme1n1. Skip this step if the volume already held data, because mkfs erases it. 3) Create a mount point with sudo mkdir -p /data. 4) Mount it with sudo mount /dev/nvme1n1 /data. 5) Get the UUID with sudo blkid and add a line such as UUID=<uuid> /data xfs defaults,nofail 0 2 to /etc/fstab. 6) Run sudo mount -a to confirm the entry is valid.
Exercise
You grew a gp3 volume from 20 GiB to 40 GiB with modify-volume, but df -h /data still shows 20 GiB. Why, and what do you run? Then name one reason a volume could still be billed after you terminated its instance.
Show answer
modify-volume enlarges the block device only, and the file system keeps its old size until you grow it. For XFS run sudo xfs_growfs -d /data; for ext4 run sudo resize2fs /dev/nvme1n1. A volume stays billed after termination when DeleteOnTermination is false, which is the default for non-root volumes. It remains in the available state until you delete it with aws ec2 delete-volume.