Expanding Disk Size for an EC2 Instance on AWS with No Downtime

Published on
3 mins read
––– views
thumbnail-image

Expanding the disk size of an EC2 instance's Elastic Block Store (EBS) volume is a crucial task that allows you to accommodate increasing storage requirements without disrupting your instance. This step-by-step guide will help you navigate the process while ensuring data safety.

Prerequisites

Before proceeding with this tutorial, ensure you have:

  • An AWS account with access to the EC2 service.
  • Basic familiarity with AWS Console and command-line interface (CLI).
  • Backup of critical data to prevent data loss.

Step 1: Identify the EBS Volume

  1. Log in to the AWS Management Console.
  2. Navigate to the EC2 Dashboard.

For safety reasons, consider stopping the EC2 instance associated with the EBS volume before proceeding. This prevents potential data corruption during disk resizing.

Step 3: Modify the EBS Volume

  1. In the EC2 Dashboard, click on "Volumes" under the "Elastic Block Store" section.
  2. Locate the EBS volume you want to resize.
  3. Right-click on the volume and select "Modify Volume."
  4. Adjust the size as needed to accommodate your storage requirements.
  5. Confirm the modification.

Step 4: SSH into Your EC2 Instance

  1. If you've stopped the instance, start it again.
  2. Connect to your EC2 instance using SSH.

Step 5: List Block Devices

  1. Run the following command to list available block devices:
    lsblk
    
  2. Identify the device name and partition number of your EBS volume (e.g., /dev/xvda1).

Step 6: Extend the Partition

  1. Use the growpart command to extend the partition size. Replace /dev/xvda with your device name and 1 with the partition number:
    sudo growpart /dev/xvda 1
    

Step 7: Verify Partition Expansion

  1. Run lsblk again to verify that the partition size has been extended.

Step 8: Resize the File System

  1. Depending on your file system type:
    • For ext2, ext3, or ext4, use:
      sudo resize2fs /dev/xvda1
      
    • For XFS, use:
      sudo xfs_growfs /
      

Step 9: Reboot and Verify

  1. If you stopped the instance earlier, start it again.
  2. SSH back into the instance.
  3. Use the df -h command to confirm the disk size has increased:
    df -h
    

Conclusion

You've successfully expanded the disk size of your EC2 instance's EBS volume. By following these steps, you can effectively manage storage growth while maintaining data integrity. Remember to exercise caution and follow best practices to prevent potential issues.