Sub topic 9
Resizing and retuning volumes with Elastic Volumes
This chapter assumes you can create, attach, format, and mount an EBS volume, and that you know the gp3 and io2 volume types and their IOPS and throughput settings. It covers changing a volume's size, type, IOPS, and throughput while it stays attached and in use. It also covers extending the file system afterward and the limits you will hit.
What Elastic Volumes does
Elastic Volumes is the EBS feature that lets you change these attributes of an existing volume without detaching it or stopping the instance:
- Size, which can only increase.
- Volume type, for example gp2 to gp3, or gp3 to io2.
- Provisioned IOPS, on types that have it (gp3, io1, io2).
- Provisioned throughput, on gp3.
The change happens at the block layer. EBS gives the instance a larger or faster device, but the file system on it is unchanged. Growing a volume is therefore two separate steps: modify the volume with AWS, then extend the partition and file system with Linux tools.
The modification state machine
Each modification moves through states that you can read with describe-volumes-modifications.
| State | What it means | Can you extend the file system? |
|---|---|---|
| modifying | EBS is applying the change. | Not yet. |
| optimizing | The new size and configuration are available. Data is being reorganized in the background. | Yes. |
| completed | The change is fully applied. | Yes. |
| failed | The request could not be applied. The volume keeps its old configuration. | Not applicable. |
During optimizing, performance sits between the old and new configurations. It can take from minutes to many hours, depending on volume size. You are billed at the new rate from the moment the modification begins.
Modifying a volume
Take a snapshot first. A modification rarely loses data, but a snapshot costs little and gives you a way back, because you cannot shrink a volume in place.
aws ec2 create-snapshot \
--volume-id vol-0123456789abcdef0 \
--description 'before resize'Then request the change. This example grows the volume to 200 GiB and raises its IOPS to 6000. You can combine any of --size, --volume-type, --iops, and --throughput in one call.
aws ec2 modify-volume \
--volume-id vol-0123456789abcdef0 \
--size 200 \
--iops 6000Check progress with a query that returns only the fields you care about:
aws ec2 describe-volumes-modifications \
--volume-ids vol-0123456789abcdef0 \
--query 'VolumesModifications[0].[ModificationState,Progress,TargetSize,TargetIops]' \
--output textTo script the wait, poll until the volume leaves modifying:
VOL=vol-0123456789abcdef0
while true; do
STATE=$(aws ec2 describe-volumes-modifications \
--volume-ids "$VOL" \
--query 'VolumesModifications[0].ModificationState' \
--output text)
echo "state: $STATE"
case "$STATE" in
optimizing|completed) break ;;
failed) echo "modification failed" >&2; exit 1 ;;
esac
sleep 15
doneExtending the file system
After the state reaches optimizing or completed, the operating system sees the larger device, but the file system still fills only the old space. Run lsblk to compare the device size with the mount size.
lsblkIf the file system sits directly on the device (as in the /data example from the mounting chapter), you extend it directly. If it sits on a partition, as root volumes do, you must grow the partition first with growpart. Note the space between the device name and the partition number.
| Layout | Example | Commands |
|---|---|---|
| XFS on a whole device | `/dev/nvme1n1` mounted at `/data` | `sudo xfs_growfs -d /data` |
| ext4 on a whole device | `/dev/nvme1n1` | `sudo resize2fs /dev/nvme1n1` |
| XFS on a partition | `/dev/nvme0n1p1` mounted at `/` | `sudo growpart /dev/nvme0n1 1`, then `sudo xfs_growfs -d /` |
| ext4 on a partition | `/dev/nvme0n1p1` | `sudo growpart /dev/nvme0n1 1`, then `sudo resize2fs /dev/nvme0n1p1` |
The XFS tool takes the mount point. The ext4 tool takes the device. A complete example for an XFS root volume:
lsblk
sudo growpart /dev/nvme0n1 1
sudo xfs_growfs -d /
df -hT /Use df -hT to confirm both the new size and the file system type, which tells you which tool applied.
Changing volume type and performance
Elastic Volumes is the standard way to move off gp2. A gp2 volume ties its baseline IOPS to its size, at 3 IOPS per GiB. Converting to gp3 decouples the two, so you set size, IOPS, and throughput separately:
aws ec2 modify-volume \
--volume-id vol-0123456789abcdef0 \
--volume-type gp3 \
--iops 3000 \
--throughput 125For many workloads this lowers the price per GiB and sets a known baseline. Check the current limits for your volume type before you pick values. In general, gp3 allows 3,000 to 16,000 IOPS and 125 to 1,000 MiB/s, and both are capped by ratios to volume size. io2 allows far higher IOPS, again capped by a ratio to size. A request outside the allowed ratio fails with an error that names the violated limit.
Limits and gotchas
- Size can only grow. There is no way to shrink a volume in place. To get a smaller volume, create a new one, mount both, copy the data (for example with
rsync -aHAX), and swap them. This also means an oversized grow is a one-way decision. - Cooldown. After you modify a volume, you must wait at least 6 hours, and until the modification is out of
modifyingandoptimizing, before modifying that same volume again. Plan changes so you do not need to correct them the same day. - Large growth may be gradual. Even when you can use the new space, background optimization can leave performance below the target for a while. Schedule big changes outside peak load.
- Some volumes are restricted. Previous-generation magnetic (
standard) volumes cannot be modified this way. Some changes to Multi-Attach volumes and some type transitions have extra rules, so read the current EBS documentation for your case. - The file system is your job. The API does not touch your partition table or file system. A volume that shows 200 GiB in the console but 100 GiB in
dfsimply has not been extended yet. - Shrinking IOPS or throughput works. You can lower provisioned performance later, which is how you reduce cost after a spike. The cooldown still applies.
- Failures keep the old state. If a modification fails, the volume continues with its previous configuration and you can retry after fixing the cause, often a limit or ratio violation.
A safe resize routine
- Snapshot the volume.
- Run
modify-volumewith the new values. - Poll until the state is
optimizingorcompleted. - Run
lsblkand extend the partition if one exists. - Extend the file system with
xfs_growfsorresize2fs. - Confirm with
df -hT, and watch the CloudWatch metrics until the state iscompleted.
Exercise
A 100 GiB gp2 volume mounted at /data (XFS, no partition) needs to become a 150 GiB gp3 volume, and you want the instance to see the extra space. Write the modify command, say which state you wait for before touching the file system, and give the Linux command that extends it. Then explain why you cannot later return the volume to 100 GiB.
Show answer
Run aws ec2 modify-volume --volume-id vol-0123456789abcdef0 --size 150 --volume-type gp3. Wait until describe-volumes-modifications reports optimizing or completed, then run sudo xfs_growfs -d /data. Because the file system is on the whole device, no growpart is needed. You cannot go back to 100 GiB because Elastic Volumes only increases size. To shrink, you must create a new 100 GiB volume, copy the data across, and replace the old one.
Exercise
You raised a gp3 volume's IOPS to 16000, but the application's throughput barely changed. Name two things to check, and say what restriction applies if you want to change the volume again an hour later.
Show answer
Check the instance's EBS-optimized limits, because the instance type may cap IOPS and throughput below what the volume allows. Also check the modification state, because the volume may still be in optimizing and not yet at the new performance. Another possible cause is that the throughput setting, not IOPS, is the limit. A second modification on the same volume must wait at least 6 hours after the first, and until the first has finished modifying and optimizing.