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 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.

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

Amazon EBS

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

So in my case the details are

Block Device Name = /dev/sda1
Availability Zone = ap-southeast-2b

Stop the Instance:

 

Create a 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

Hopefully you will have something like this:

 

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

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:

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).

Create two mount directories and mount the new volumes.

Sync the files.

Unmount the smaller volume.

Stop the instance

Detach all volumes.

Attach the small volume to the Block Device Name from the first step.

Block Device Name = /dev/sda1

Now you can restart your instance and verify that it is working correctly.

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.