How To Reduce AWS EBS Root Volume Size
In todays blog we will discuss how to reduce EBS root volume size. During instance creation you get the option to define your EBS root volume size. If you have provisioned an EBS volume that has more storage then needed, you might want to reduce or decrease your EBS root volume size.
AWS provides very good documentation to increase your root volume size but for some unknown reason did not provide any documentation to reduce the size. Below is the document to increase EBS root volume just in case if you want to increase the size instead of decrease.
How to reduce AWS EBS root volume size:
The steps mentioned below can be applied to any non-root volume as well. I have tested this with Amazon Linux 2 and it works. In a high level, we are going to spin up two new EC2 instance with exact configuration (AMI/instance type but EBS volume size will be what we want to have) and use the 1st one to perform the activity and second one root volume will be our new EBS volume where we will copy the existing root volume data.
Step 1: Backup your current EBS root volume that you want to reduce size.
Stop your EC2 instance and navigate to your EBS volume and create a new snapshot. It is always better to have a backup just in case.
Step 2: Detach your existing root volume from your instance.
After this step you should have an EBS volume (Old volume) in “Available” state.
Step 3: Create a new EC2 instance with exact configuration as your current EC2 instance is.
Particularly you need to use the same AMI and instance type. But choose the root volume size to what you want the target EBS volume to have. Say your current EBS root volume size is 30GB and you want to reduce it to 10GB then configure the new instance root volume size to be 10GB.
Once the EC2 instance is up and running (make sure both the status check passed), stop your EC2 instance and detach the EBS volume attached to the instance.
If you want to reduce a non-root EBS volume, then instead of creating a new instance, create a new EBS volume to the target size.
After this step, you should have a new EBS volume (New volume) in “Available state”
Step 4: Now spin up a new EC2 instance with same AMI, other configuration does not matter.
We will use this EC2 instance to copy data from Old volume to New volume.
Step 5: Once this new instance is up and running, attach both the volumes obtained in step 2 and 3 to this new EC2 instance.
Attach the Old volume as /dev/sdf (this becomes /dev/xvdf)
Attach the New volume as /dev/sdg (this becomes /dev/xvdg)
Step 6: Connect to your new EC2 instance execute below commands.
# Check the filesystem of Old volume
sudo e2fsck -f /dev/xvdf1
# If no issue reported in above command, execute below command
sudo resize2fs -M -p /dev/xvdf1
The last line from the resize2fs command should tell you how many 4k blocks the filesystem now is. To calculate the number of 16MB blocks you need, use the following formula: target = blockcount * 4 / (16 * 1024). Round this number up to give yourself a little buffer.
Note: If you’re resizing a different partition on the drive, change the number 1 to the partition number you wish to resize.
# Execute below command to copy data from your Old volume to New volume.
sudo dd bs=16M if=/dev/xvdf1 of=/dev/xvdg1 count=target
# This may take some time depending on your volume size.
# Once the above command executed successfully, execute below command to validate the new volume.
sudo resize2fs -p /dev/xvdg1
# And finally
sudo e2fsck -f /dev/xvdg1
If no issue reported, then we are ready for the next step.
Step 7: Detach the New volume from the EC2 instance and attach it to your existing instance that you have stopped in step 1.
Attach the New volume as /dev/sda1 (if root volume, else as suggested by console)
Step 8: Start your instance and check if everything works fine. Your EBS volume size reduced successfully.
Don’t forget to cleanup the snapshot, old volume and EC2 instance created in step 3 and 4.
Hope you have enjoyed this blog.