Resize The Disk Space of EC2 Instance (Zero Downtime)

Looking for a step-by-step tutorial on how to increase disk space on your AWS EC2 instance?

I get it—I spent a lot of time trying to find the perfect guide myself. So I wrote this to save you the trouble I went through. (Now this article is always a go-to solution for me too😎)

You’ve landed in the right place.

In this tutorial, you’ll learn how to resize your EC2 instance’s disk without detaching the volume or restarting the server(Easily). AWS provides a block storage solution, EBS, for the instance. EBS’ Elastic Volumes feature allows you to increase volume size while the volume is still in use, making the resizing process much easier and faster without any downtime of the server.

Before extending the size of your EBS volume, It’s good to take a backup of your data with EBS Snapshot. It’s the best practice as if anything goes wrong, you can always restore the data.

Create Snapshot

To take a snapshot of your volume,

  • Go to EBS volume attached to your instance from EC2 Dashboard.

  • Click on Actions and Create Snapshot.

  • Add the description value as you wish.

  • You can add tags also.

  • Click on Create Snapshot. It may take some time.

Resize The EBS Volumes of EC2 Instance:

After creating a snapshot of your volume, now it’s time to increase your disk space.

Steps:

  • First Go to Services > EC2 > Instances.

  • Go to the EBS volume attached to your instance.

  • From Actions click on Modify Volume.

  • Add your desired size, I will be replacing 8 with 30.

  • You’ll get a confirmation pop-up. Click on Modify.

Resizing the EBS:

You have successfully added a new volume size to your instance, but you aren’t using the new volume size. For this, SSH to your server or instance and run: df -h

Here, you can see our disk partition is still using 8GB. Check the partition size by running commands lsblk and blkid.1

Here, xvda1 is your current volume with 8GB and xvda with 30GB.

<aside> 📌 *xvda1 or similar is for Xen Virtual Machine based server. For NVMe (Non-Volatile Memory Express) solid-state drive (SSD) based device, it will be nvme0n1p1 or similar.*

</aside>

Now extend the partition:

# For xvda1 partition
$ sudo growpart /dev/xvda 1
# For nvme0n1p1 partition
$ sudo growpart /dev/nvme0n1 1

And, extend the volume:

$ sudo xfs_growfs /dev/xvda1
$ sudo xfs_growfs /dev/nvme0n1p1

Type df -h to check the volume size, it must show 30GB.

In our case, since the file system is XFS, we have to use xfs_growfs tool.

For file systems ext4, ext2, and ext3 you have to use sudo resize2fs /dev/xvda1 OR sudo resize2fs /dev/nvme0n1p1.

Conclusion

In this way, the volume is now resized and ready to be used without any downtime.

Thank you!

0
Subscribe to my newsletter

Read articles from Sagar Budhathoki directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sagar Budhathoki
Sagar Budhathoki

I am a Python/DevOps Engineer. Skilled with hands-on experience in Python frameworks, system programming, cloud computing, automating, and optimizing mission-critical deployments in AWS, leveraging configuration management, CI/CD, and DevOps processes. From Nepal.