Shrinking An Amazon EBS Volume

Shrinking An Amazon EBS Volume

September 9, 2022 Off By Nick

Amazon’s Elastic Block Storage Volumes are easy-to-use and expand, but they can be difficult to shrink once they have grown in size. Here are the steps to shrink any mounted EBS volume via EC2 Instances.
You may need to increase or decrease the size of your EBS volume for various reasons. You are only charged for the space you have allocated to your EBS volumes. This makes it cost-efficient to only allocate the space you need in the short term and then increase the volume as needed. This should be considered during planning and/or when developing growth strategies for your service.
These steps were tested on an Amazon Linux instance.
Attach an EBS Voume to your instance by creating a new EBS Voume in the console.
Log in to your instance with ssh. Format your attached volume using this command. (Assume that the volume is attached to /dev/sdi).

Shellssh -i [email protected] -i [email protected]
sudo mkfs-t ext4/dev/sdi1sudo-mkfs-t ext4/dev/sdi3. Next, create a directory at/mnt/ebs1 then mount the volume using these commands
sudo mkdir /mnt/ebs11sudo mkdir /mnt/ebs1
sudo mount /dev/sdi /mnt/ebs11sudo mount /dev/sdi /mnt/ebs14. Copy your data recursively to your new volume by using the following command.
sudo cp-r –preserve=all /mnt/ebs1 -verbose1sudo:cp –r –preserve=all/mnt/ebs/ /mnt/ebs1 –verboseCopying data recursively preserves the file attributes such as mode,ownership,timestamps and security contexts, if possible additional attributes such as links.
5. After copying the data, disconnect your old volume and mount the new volume at the same mount location using the following command
sudo umount -l /mnt/ebs1sudo umount -l /mnt/ebs
sudo mount /dev/sdi /mnt/ebs1sudo mount /dev/sdi /mnt/ebs
Shell Script automates the process
This script automates all of this.
Download the zip from this page https://github.com/cloudthat/ebs-shrink
Navigate to the extracted folder by extracting the zip
Step 1: Copy the script to the instance with the appropriate permissions
scp -i /ebsreducescript @:/home/ec2-user/.1scp -i /ebsreducescript @:/home/ec2-user/.Now SSH into the instance using the following command and Update file permissions
ssh -i [email protected] -i [email protected]
sudo chmod 500 ebsreducescript1sudo chmod 500 ebsreducescriptStep 2: Configure AWS Cli
Before you execute the script, configure awscli using the following commands:
sudo yum python -pip1sudo: yum python -pip1.
sudo pip awscli1sudo pips install awscliRun at the command prompt to set up your credentials.
$ aws configureAWS Access Key ID [None]: ANKITIOSFODNN7EXAMPLEAWS Secret Access Key [None]: ANKITXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEYDefault region name [None]: us-west-1Default output format [None]: json12345$ aws configureAWS Access Key ID [None]: ANKITIOSFODNN7EXAMPLEAWS Secret Access Key [None]: ANKITXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEYDefault region name [None]: us-west-1Default output format [None]: jsonThis Script Will Create a New Volume of User Defined,Mount it to /mnt/ebs1 Copy all the data from the attached EBS Volume recursively that need to be Migrated preserving all the user permissions,then Unmount it and Mount the Newly Created EBS Volume at the Same Mount Point
Not to be confused with the deleted volume, it is only unmounted. If an error occurs, we can reattach it. This script can only be used for Attached (Mounted EBS Volumes) and not Root Volume.