How to decrease, resize and shrink your AWS EBS Volume (GP2,GP3) in EC2

Our AWS EC2 Project

Migrating Bitrix24 Cloud to Bitrix24 On-Premise hosted on AWS EC2

After migrating Bitrix24 cloud with 1TB storage usage to on-premise and having allocated 3TB of EBS storage as part of the migration process, we found ourself with USD400 bill per month cost to host Bitrix24. We were only using 1TB of storage after migration but had 3TB storage on EBS and AWS was charging us 100GB daily which account to 3TB monthly.

After some research, we decided that we need to shrink our EBS volume and use AWS S3 storage which is way cheaper. So, here are the steps we took to shrink our 3TB EBS volume to 850GB.

You may want to try the steps using AWS free tier account first using a Linux EC2 instance of 20GB and shrink it down to 10GB volume.


Take a snapshot of your existing EBS volume

This is for backup and also for you to create a new volume in which you will shrink. A 3TB volume takes about 2 hours to complete and be available.

If your Bitrix24 instance is already in production mode, do inform your users that anything they perform in system onwards will not be available after the scheduled maintenance.

Create a new volume from snapshot

GP3 volume type is recommended as it's much cheaper. Make sure you create in the correct sub-net and region.

Launch new lightweight AMI instance

You can use Bitrix24 AMI -BitrixVM-7.5.1_CentOS7.9_x64_HVM_EBS

Attached the new volume to this new instance. The volume should be attached as xvdf.

Resize Filesystem

SSH to your terminal with no timeout. Use tmux to run the command so that you can remote terminal using your mobile phone to check progress. You can use the free Termius IOS app. Run below command.

fdisk -l

sudo e2fsck -f /dev/xvdf3
sudo resize2fs -M -p /dev/xvdf3

A resize of 3TB volume takes about 12-14 hours.

After executing the previous command, the last line will output the blockcount number in the filesystem. The formula to calculate the total number of 16 blocks you need will be: blockcount * 4 / (16 * 1024) = counts

Create new smaller EBS Volume (GP3)

Round up the counts number as extra buffer and times 16, which equals to the estimated size of space needed in size MB.

Create this new volume based on the calculated size after running resize2fs so that you can save some money. AWS charges you when instance is created.

Copy data from shrink volume to new small volume

Run sudo dd bs=16M if=/dev/xvdf of=/dev/xvdg count=100 status=progress

Run sudo dd bs=16M if=/dev/xvdf3 of=/dev/xvdg3 count=50000 status=progress

[Change 50000 to counts number retrieved from the calculation in step 4]

Initialize instance with new smaller EBS Volume

Run

sudo resize2fs -p /dev/xvdg3
sudo e2fsck -f /dev/xvdg3

Detached from temporary instance.

Shutdown production instance and detach volumes

Attach new smaller volume to production instance and start the server.

Check and verify the new attached volume and storage with below command. 

df -h