Increase or decrease EBS volume of your AWS EC2 instance

Last updated on March 7th, 2023 at 01:22 pm

There used to be a time when Hard disks were very expensive and used to cost a lot of money. Nowadays they are dirt cheap and it is impervious to assume that that is the case with AWS EBS volume too. BUT, this is not true. Infact, EBS volumes are still very expensive and can become costly affairs if you don’t control your temptation to allocate space liberally.

We are humans and mistakes happen. So, You have set a 50GB space for EBS Vol and the whole program is set and running. And you suddenly see that you require no more than 15GB of space. You might be wondering, can we decrease EBS volume size? Well, It is impossible to decrease EBS volume size. If you try to decrease using AWS console you will be welcomed by : The size of a volume can only be increased, not decreased.

Shrinking size of EBS volume

Here are the few ways by which you can decrease the size of AWS EBS Volumes:

Create new volume and transfer all data from old volume to new volume.

This is the best way of resizing, it may be time consuming but trust me it holds a lot less roadblocks. Typically has 3-4 applications and its data installed. You can easily transfer your data to a new one in more than one way. For e.g.

  1. In the case of databases, you can create replica sets with a smaller disk size and sync all your data.
  2. Web-servers can easily be installed and configured again and that is much cloud preferred.
  3. You can reattach IP-addresses to new machines.

Assuming you do not have a choice but to make an exact replica of the root disk. Follow the given approach. It is a bit hit and miss.

Create new volume using rsync

I stuck myself with the same problem and luckily after a lot of googling and tries, I finally got it to work. Here is the summary of the steps you need to take. I haven’t tested these steps other than the Bitnami Ubuntu machine.

  1. Take a snapshot of root volume for backup
  2. Create Smaller EBS volume
  3. Attach the new volume to instance
  4. Format the new EBS volume and mount it.
  5. Copy data from root volume to the new volume
  6. Install the grub loader again
  7. Detach and unmount old volume
  8. Rename UUID and label for EBS volume

Let us assume that we have:

  1. Instance named my-instance in ap-south-1a zone
  2. 100GB EBS volume size named old-volume
  3. We wish to decrease into 30GB and name it new-volume

We would need to shutdown the instance to prevent inconsistencies.

Snapshot the volume

You should always take snapshots before such operations as you may end up corrupting the whole root disk.To take a snapshot of the existing volume, Simply go to AWS Console -> EC2 -> Volumes -> Select Volume -> Actions(Take Snapshot)

Create a new EBS volume

  1. Make sure instance is shutdown
  2. Go to Elastic Block Store Volumes
  3. Click Create Volume
  4. Choose the same volume type same with your old volume
  5. Enter your desired size; For eg. 30.
  6. Choose availability zone; The volume will be available to instance in the same availability zone, which is ap-south-1a . (This is very important as you need to attach this volume to machine within same AZ. )
  7. Add tag with Key : Name and value : EBS Small

Attach the new volume

  1. Right click on the new volume.
  2. Click Attach Volume.
  3. Choose instance name my-instance .
  4. Click Attach.

We could start the instance and login to SSH. List all available volumes with lsblk.

The new volume is could be one of these:

 /dev/xvdf

 /dev/sdf

 /dev/nvmeXn1 Here X could be (1, 2, 3 and so on)

we will consider xvdf from here

Format the new volume

  1. Check whether the volume has any data or not using command sudo file -s /dev/xvdf . For newer Volumes you might get NVMe storage. Rules remain the same.If it is displaying /dev/xvdf: data, it means the volume is empty. We could format the volume.If it is displaying other than above output, it means the volume has data. DO NOT format the volume if you saw this output. This means you must have chosen another Volume.Format the volume using command sudo mkfs -t ext4 /dev/xvdf .

Mount the new volume

  1. Create a directory to mount using command sudo mkdir /mnt/new-volume.
  2. Mount new volume into directory using command sudo mount /dev/xvdf /mnt/new-volume .
  3. Check volume with command df -h; The new volume should be mounted now.

Copy data from old volume to the new volume

  1. Use rysnc to copy from old volume to the new volume sudo rsync -axv / /mnt/new-volume/.
  2. Relax and wait until it’s finished. Get a coffee!

Prepare new volume

  1. Install grub on new-volume using command sudo grub-install --root-directory=/mnt/new-volume/ --force /dev/xvdf . Grub Install is needed for boot loading the disk. Rsync never creates a bootable disk. You need to make sure these commands run. I have seen the cases where you might have an older grub-install version installed on your Linux machine.
  2. Unmount new-volume sudo umount /mnt/new-volume .
  3. All EBS volumes come with a unique UUID which is basically a universal identifier for your disk. It helps you identify the disk attached to your machine. We need to check the older UUID and update the older one to the new one. This will maintain system integrity. Check UUID using command blkid .
  4. Copy UUID from /dev/xvda1 (paste anywhere to backup this UUID); This is your old UUID.
  5. Use tune2fs to replace UUID sudo tune2fs -U COPIED_UUID /dev/xvdf; COPIED_UUID is the string value from point 4. It might want you to run sudo efsck -f /dev/xvdf first
  6. Check the system label from old-volume using command sudo e2label /dev/xvda1 ; It will display string like cloudimg-rootfs or "/" .
  7. Replace new-volume label with old-volume label using command sudo e2label /dev/xvdf cloudimg-rootfs .
  8. We can logout SSH now.

Detach old volume

  1. Stop instance my-instance
  2. Detach old-volume
  3. Detach new-volume
  4. Attach new-volume to /dev/sda1 or /dev/xvda depending upon your attachment.
  5. Start the instance 

After we finish all steps above, we can check our instance by login into SSH.

Give it a go for two or three days. If everything works, you can safely delete the older disks and snapshots. It will save you a lot of money.

Increasing size of EBS Volume

Increasing an EBS Volume size is a relatively easy task.

  1. Go to EBS Volumes
  2. Select EBS volume you want to increase
  3. Go to Actions -> Modify Volume. see screenshot
  4. Enter the new desired size and Click Modify

After that, you need to login into the instance and check volume.

To your surprise it will still show the previous volume size. You need to resize it using command sudo resize2fs /dev/xvda1. Here xvda is disk name which can be also nvmeXp1 depending on your account setting. That’s all. Your EBS volume is now resized.

If your volume has a partition, run following command to grow the partition size.

$] sudo growpart /dev/xvda 1

//check whether the size has increased or not by following command

$] lsblk

//should output something like this
nvme0n1     259:0    0 238.5G  0 disk 
├─nvme0n1p1 259:1    0   512M  0 part /boot/efi
└─nvme0n1p2 259:2    0   238G  0 part /

Note: if your filesystem is xfs based then you will need to execute following command

sudo xfs_growfs /dev/nvme2n1

Conclusion

Reducing an EBS Volumes size could be tricky and the lesson learned is always start with minimum requirement because it is much easier to expand volume than reduce.

Hope it help!

3 thoughts on “Increase or decrease EBS volume of your AWS EC2 instance”

  1. This is the best way of resizing, it may be time consuming but trust me it holds a lot less roadblocks. Typically has 3-4 applications and its data installed. You can easily transfer your data to a new one in more than one way.

  2. Before increasing or decreasing the EBS (Elastic Block Store) volume size of your AWS EC2 instance, there are several important steps you should take to ensure a smooth and successful process. Modifying EBS volumes can impact the data on your instance, so it’s crucial to follow best practices and precautions. Here’s a checklist of things to do before resizing an EBS volume:

    Before Increasing EBS Volume Size:

    Backup Your Data:
    Before making any changes, create a backup of your important data to prevent data loss in case something goes wrong during the resizing process.
    Check Free Disk Space:
    Ensure that you have enough available disk space on your instance to accommodate the larger EBS volume.
    Verify Snapshot:
    It’s a good practice to create a snapshot of your EBS volume before making any changes. This snapshot can be used as a restore point in case the resizing process encounters issues.
    Stop Applications:
    Stop any applications or services that might be writing to or using the EBS volume. This helps prevent data inconsistencies during the resizing process.
    Update Your Instance:
    Make sure your instance is up to date with the latest operating system updates and patches.
    Resize File System (if needed):
    If you’re increasing the volume size and the file system does not automatically expand to use the new space, you may need to resize the file system manually.
    Modify EBS Volume Size:
    In the AWS Management Console, modify the EBS volume size to the desired value. Note that this can only be done for EBS volumes that are not the root volume.
    Restart Instance:
    After modifying the EBS volume size, restart your EC2 instance to ensure that it recognizes the new volume size.
    Resize File System (if needed, continued):
    If you manually resized the file system earlier, make sure to resize it again now that the EBS volume size has been increased.
    Test Applications:
    Verify that your applications and services are functioning correctly after the EBS volume size increase.

    Before Decreasing EBS Volume Size:

    Backup Your Data:
    As with increasing volume size, create a backup of your important data to prevent data loss during the resizing process.
    Check Free Disk Space:
    Ensure that you have enough available disk space on your instance to accommodate the reduced EBS volume.
    Stop Applications:
    Stop any applications or services that might be using the EBS volume to prevent data inconsistencies during the resizing process.
    Update Your Instance:
    Ensure your instance is up to date with the latest operating system updates and patches.
    Verify Snapshot:
    Create a snapshot of your EBS volume before making any changes, so you have a restore point in case the resizing process encounters issues.
    Backup or Migrate Data (if needed):
    In some cases, you may need to move data off the volume you intend to shrink to ensure there is enough free space.
    Resize File System:
    Resize the file system on the EBS volume to a size smaller than the target volume size.
    Modify EBS Volume Size:
    In the AWS Management Console, modify the EBS volume size to the desired smaller value.
    Restart Instance:
    After modifying the EBS volume size, restart your EC2 instance to ensure that it recognizes the new volume size.
    Test Applications:
    Verify that your applications and services are functioning correctly after the EBS volume size reduction.

    Remember that while AWS provides tools and mechanisms to resize EBS volumes, these operations should be performed with caution, especially when reducing the volume size. Always refer to the latest AWS documentation and best practices for EBS volume resizing before making any changes to your instances.

Leave a Reply

%d bloggers like this: