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.
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
So in my case, the details are
Block Device Name = /dev/sda1
Availability Zone = ap-southeast-2b
Using the snapshot, create a second volume – of the original size – in the same availability zone as your instance.
Hopefully, you will have something like this:
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
Login:
ssh -i 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
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.