How to Shrink Amazon EBS Volume Size: Step-by-Step Guide for Ubuntu Servers


2 views

Amazon's default Ubuntu Server images come with 15GB EBS volumes, which often exceeds actual requirements for lightweight deployments. When using separate volumes for data storage, this default allocation becomes particularly wasteful.

Before proceeding, ensure you have:

1. A recent backup of your EBS volume

2. AWS CLI configured with proper permissions

3. Enough free space on another volume for temporary storage

4. Downtime window (this process requires instance stop)

First, connect to your instance and check current disk usage:

df -h
sudo lsblk
sudo fdisk -l

Then resize the filesystem to the minimum required space (example for ext4):

sudo resize2fs /dev/xvda1 2G

Use AWS CLI to create a new, smaller volume in the same AZ:

aws ec2 create-volume \
--availability-zone us-east-1a \
--size 2 \
--volume-type gp2

Attach both volumes to a temporary instance:

aws ec2 attach-volume \
--volume-id vol-1234567890abcdef0 \
--instance-id i-01474ef662b89480 \
--device /dev/sdf

aws ec2 attach-volume \
--volume-id vol-0987654321abcdef0 \
--instance-id i-01474ef662b89480 \
--device /dev/sdg

Then copy the data:

sudo dd if=/dev/xvdf of=/dev/xvdg bs=1M

After verifying the new volume works correctly:

sudo fsck -f /dev/xvdg1
sudo mount /dev/xvdg1 /mnt
ls -la /mnt

Don't forget to update your instance's launch configuration or Terraform scripts to use the new volume size for future deployments.

For a more streamlined process:

aws ec2 create-snapshot --volume-id vol-1234567890abcdef0
aws ec2 create-volume \
--snapshot-id snap-1234567890abcdef0 \
--size 2 \
--volume-type gp2

While expanding EBS volumes is straightforward through AWS console or CLI, shrinking them presents unique technical challenges. The Ubuntu Server's default 15GB root volume often contains more space than needed, especially when using separate volumes for data storage. Here's how to safely reduce your EBS volume size to just 2GB.

Before beginning the shrinking process, ensure you have:

  • A current backup of all important data
  • SSH access to your instance
  • AWS CLI configured with proper permissions
  • Basic understanding of Linux filesystem operations

First check actual disk usage to determine the minimum required size:

df -h
sudo du -sh / --exclude=/mnt/* --exclude=/media/* --exclude=/proc/* --exclude=/sys/*

Shrink the filesystem to its minimum size first:

sudo resize2fs -M /dev/xvda1
sudo e2fsck -f /dev/xvda1

Use these AWS CLI commands to create a snapshot and a new smaller volume:

aws ec2 create-snapshot --volume-id vol-1234567890abcdef0
aws ec2 create-volume --availability-zone us-east-1a --size 2 --snapshot-id snap-1234567890abcdef0

For more control, you can use dd to copy only used blocks:

sudo dd if=/dev/xvda1 of=/mnt/temp/volume.img bs=4M
sudo truncate -s 2G /mnt/temp/volume.img

Always test the new volume before putting it into production:

sudo mount /dev/xvdf1 /mnt/test
ls -la /mnt/test
df -h /mnt/test
  • Bootloader issues when shrinking the root volume
  • Filesystem corruption if process is interrupted
  • Performance impact from using 100% of a smaller volume