Increase or decrease EBS volume of your AWS EC2 instance

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

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.

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!

Leave a Reply

%d bloggers like this: