Skip to content

Instantly share code, notes, and snippets.

@dPacc
Created December 18, 2023 08:24
Show Gist options
  • Save dPacc/8e73af183b2246613d4f9394059228d2 to your computer and use it in GitHub Desktop.
Save dPacc/8e73af183b2246613d4f9394059228d2 to your computer and use it in GitHub Desktop.
Extending AWS EC2 Instance Storage

Extending AWS EC2 Instance Storage

This guide explains how to extend the storage of an AWS EC2 instance's root partition and resize the filesystem to utilize the additional space.

Prerequisites

  • An AWS EC2 instance with an EBS volume.
  • SSH access to the EC2 instance.

Steps

1. Stop the EC2 Instance (Optional but Recommended)

  • Stop the EC2 instance for safety. This step is optional but recommended to ensure data integrity.

2. Modify the EBS Volume

  • In the AWS Management Console, navigate to the EC2 dashboard.
  • Go to "Volumes" and locate the volume attached to your instance.
  • Right-click on the volume and select "Modify Volume".
  • Increase the size as needed and confirm the changes.

3. Restart the EC2 Instance

  • Restart the EC2 instance if it was stopped.

4. Check the Current Disk Space

  • Connect to the EC2 instance via SSH.

  • Run df -h to check the current disk space usage.

    df -h

    Output:

    Filesystem      Size  Used Avail Use% Mounted on
    /dev/root       15G   15G   18M 100% /
    ...
    

5. Check the Block Devices

  • Use lsblk to list all block devices and their sizes.

    lsblk

    Output:

    NAME    MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    xvda    202:0    0   50G  0 disk 
    ├─xvda1 202:1    0 14.9G  0 part /
    ...
    

6. Resize the Partition

  • Use growpart to resize the partition.

    sudo growpart /dev/xvda 1

7. Expand the Filesystem

  • After resizing the partition, use resize2fs to resize the filesystem.

    sudo resize2fs /dev/xvda1

8. Verify the Changes

  • Run df -h again to verify that the filesystem is now utilizing the new partition size.

    df -h

    Expected Output:

    Filesystem      Size  Used Avail Use% Mounted on
    /dev/root       50G   15G   35G  30% /
    ...
    

Conclusion

This guide helps in extending the storage of an AWS EC2 instance by resizing the EBS volume and extending the filesystem. Always ensure to have backups before performing such operations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment