How to resize EBS Volume size for Amazon Linux 2

Resizing an EBS (Elastic Block Store) volume for an Amazon Linux 2 instance involves two main steps:

  1. Modifying the EBS volume size in the AWS Management Console or using the AWS CLI.

  2. Resizing the file system on the EBS volume to make use of the additional space.

Let's go through these steps in detail:

💡
To launch the EC2 instance use the following guide: How to launch a single EC2 instance via AWS CLI

1. Modifying the EBS volume size

a. Identify the volume ID:

First, you'll need the volume ID of the EBS volume attached to your instance. If you don't already have this, you can retrieve it with the following command:

aws ec2 describe-instances --instance-ids <your-instance-id> --query "Reservations[].Instances[].BlockDeviceMappings[]" --output table

Note: Replace <your-instance-id> with your instance ID.

b. Resize the volume:

Once you have the volume ID, you can modify its size with the following command:

aws ec2 modify-volume --volume-id <your-volume-id> --size <new-size>

Note: Replace <your-volume-id> with the volume ID and <new-size> with the desired size in GB.

2. Resizing the file system

After modifying the EBS volume size, you'll need to resize the file system on the volume. Before you do this, it's good practice to ensure data consistency by checking the file system first.

a. Connect to your Amazon Linux 2 instance:

Use the ssh command to connect to your instance.

ssh -i <path-to-your-key.pem> ec2-user@<your-instance-public-ip>

b. Check whether the volume has a partition. :

To check the partition use the lsblk command

sudo lsblk

c. Extend the partition:

Check whether the partition needs to be extended. In the lsblk command output from the previous step, compare the partition size and the volume size.

If the partition size is smaller than the volume size, continue to the next step. If the partition size is equal to the volume size, the partition can't be extended.

Use the growpart command and specify the partition to extend.

sudo growpart /dev/xvda 1

3. Verify the resizing:

To ensure that the file system now uses the entire available space on the EBS volume, you can check the disk usage with the df command:

df -hT

Look for your EBS volume's mount point in the output. You should see that the available space matches the new size you specified when modifying the volume.

In case there is no match in sizes, you need to reboot the instance and verify again.

You've successfully resized your EBS volume on an Amazon Linux 2 machine using the AWS CLI and verified it.

References:

  1. Extend a Linux file system after resizing a volume