Amazon EBS volumes: How to Shrink ’em Down to Size

Have you ever launched an EC2 instance with Amazon EBS (Elastic Block Store) storage only to realize that your EBS storage is way over-allocated and you have no idea on how to reduce it?

Amazon’s Elastic Block Store Volumes are easy to use and expanding them is no problem at all, but for some reason, there’s no obvious way to reduce them. This is especially problematic when the EBS volume is mounted on the root.

It turns out that there’s nothing to worry about. I’ll take you through an easy way to reduce your volumes so you can hopefully save yourself some money.

Reducing AWS EBS Volumes

EC2 Instance with 20GB Amazon EBS Volume

For the purpose of this exercise, I created an instance and launched it with a 20GB Amazon EBS volume:

Assuming we want to reduce this to 8GB, the first thing we will need to do is to make a note of the root volume’s block device name and our instance’s availability zone
Instance availability zone
So in my case, the details are
Block Device Name = /dev/sda1
Availability Zone = ap-southeast-2b

Stop the Instance:

Instance state drop down (Start, Stop, Reboot, Terminate)

Create a snapshot of the root volume:

AWS EBS - Create a first snapshot of the root volume

Create a second Amazon EBS volume:

Using the snapshot, create a second volume – of the original size – in the same availability zone as your instance.
Amazon EBS - Create a second snapshot of the root volume
Hopefully, you will have something like this:
AWS EBS - Summary of Availability Zones

Create an empty 8GB Amazon EBS volume in the same availability zone

AWS EBS - Attaching both volumes to the instance
attach both volumes to the instance and again note all device name details.

Create 8GB Amazon EBS volume
Now, attach both volumes to the instance and again note all device name details.

Block Device Name Big Volume = /dev/sda1
Block Device Name Big Volume Snapshot = /dev/sdg
Block Device Name Small Volume = /dev/sdf

Restart the Instance and SSH in

Login:

ssh -i <Private-key> ec2-user@ip-address

Create a file system for the 2 volumes you have created (Note: In Ubuntu I had to do a cat/proc/partitions to work out which device was which).

sudo mkfs -t ext4 /dev/xvdf
sudo mkfs -t ext4 /dev/xvdg

Create two mount directories and mount the new volumes.

sudo mkdir /mnt/small
sudo mount /dev/xvdf /mnt/small
sudo mkdir /mnt/snap
sudo mount /dev/xvdg1 /mnt/snap

Sync the files.

sudo rsync -aHAXxSP /mnt/snap/ /mnt/small

Unmount the smaller volume.

sudo umount /dev/xvdf

Stop the instance

Detach all volumes.
AWS EBS - Detaching all volumes
Attach the small volume to the Block Device Name from the first step.
Block Device Name = /dev/sda1
AWS EBS - Attaching the small volume to the Block Device Name from the first step
Now you can restart your instance and verify that it is working correctly.

I recommend taking a look at Cloud Academy’s Managing Instance Volumes Using EBS Hands-on Lab if you want to learn to create an EC2 instance with an additional EBS volume.

IMPORTANT Don’t forget to delete your snapshot and the two other volumes that are no longer needed. That can save you some big bucks.

Cloud Academy