In EC2 the disk is not tied to the size of the machine: it can be enlarged whenever you want, even with the machine running, without reinstalling or moving anything. There are two parts: telling AWS the disk is bigger, and telling the operating system inside to make use of it. The first is one click; the second, two commands that are scary the first time and then get typed from memory.
The only thing you can't do is shrink: a disk only grows. So grow it sensibly.
Before you start: what each thing means
- EBS
- The EC2 disk service. Each disk is called a volume.
- Root volume
- The disk where the system is installed. It is the one that usually fills up.
- Partition
- A division inside the disk. When you enlarge the disk, the partition stays at the old size until you grow it.
- File system
- The way the system organises the data inside the partition (ext4 or xfs on Linux). It has to be grown too.
- gp3
- The normal EBS disk type. If your disk is the older type (gp2), you can change it to gp3 in the same step and it usually works out cheaper.
- Snapshot
- A copy of the disk. It is wise to take one before touching the disk, just in case.
Step by step
Take a snapshot first
EC2 console, Volumes menu, select the machine's disk (the "Attached to" column tells you which one), Actions, Create snapshot. It is the safety net; in practice the process is safe, but it costs nothing... well, a few cents of storage.
Grow the volume from the console
With the volume selected, Actions, Modify volume. Enter the new size in GB (for example from 20 to 40) and, if the type is gp2, take the chance to change it to gp3. Modify. The volume state goes to "modifying" and then "optimizing"; you don't need to wait for the optimisation to finish before going on; as soon as the new size appears in the list you can continue. Bear in mind that AWS only lets you modify a volume once every six hours.
See how the disk looks from inside
Connect to the machine via SSH and type:
lsblk df -h /The first command shows the disk (
xvdaornvme0n1) already at the new size, and below it its partition (xvda1ornvme0n1p1) still at the old size. The second shows how much space the system sees: the old amount. Write down the exact names of the disk and partition.Grow the partition
sudo growpart /dev/xvda 1Mind the syntax: the disk, a space, and the partition number. With NVMe disks it would be
sudo growpart /dev/nvme0n1 1. If it saysgrowpartdoesn't exist, install it (sudo apt install cloud-guest-utilson Ubuntu/Debian). Runlsblkagain: the partition now fills the whole disk.Grow the file system
It depends on the type, which
df -T /tells you. If it is ext4 (Ubuntu, Debian):sudo resize2fs /dev/xvda1If it is xfs (Amazon Linux, Red Hat):
sudo xfs_growfs -d /Both work with the disk in use, without rebooting. Check with
df -h /: the system now sees the new size.If the disk is not the root one
For an additional data disk, steps 4 and 5 are the same with the device name changed. If the disk has no partition (you formatted it directly on
/dev/xvdf), skipgrowpartand runresize2fs /dev/xvdforxfs_growfson its mount point directly.
What can go wrong
growpart says "NOCHANGE"
Either the partition already fills the whole disk (go to step 5), or the system hasn't yet seen the disk's new size. Wait a minute, repeat lsblk and, if it still doesn't see it, reboot the machine.
I don't even have space to run the commands
With the disk at 100 % some commands fail. Free up a few megabytes by deleting old logs (sudo journalctl --vacuum-size=50M) or the package cache (sudo apt clean) and try again.
I made a mistake and set a huge size
It can't be shrunk. The only way out is to create a new machine with a smaller disk and migrate the data. If the excess is bearable, leave it and learn for next time: set what you need for a year.
I want to grow it again and it won't let me
You have to wait six hours from the last modification of the volume. It is an AWS limitation.
In Simplio
In Simplio every machine shows its disk usage and emails you when it goes past 85 %, before the website stops working. Growing it is a button: you choose the new size, Simplio takes a snapshot first, modifies the volume in AWS and runs the commands on the machine so the system uses the space, and shows you the result. No terminal.
The same task, in AWS and in Simplio
| In the AWS console | In Simplio | |
|---|---|---|
| Enlarging the disk | EC2 console, Volumes, Modify, SSH, lsblk, growpart, resize2fs: 7 steps and a terminal | "Grow disk" button on the machine: you choose the size and Simplio does the AWS part and the inside part |
| Finding out it is filling up | Nothing warns you until something fails | Email warning at 85 % usage |
| Snapshot beforehand | Remembering to | Done automatically before growing |