AWS, Cloud Computing

5 Mins Read

How to Resize EBS Volume Size in AWS EC2

Voiced by Amazon Polly

Introduction

An Amazon EBS volume is a block-level storage device that can be attached to your instances. You can use it as you would use a physical hard drive. Moreover, EBS volumes are flexible, and you can dynamically increase size, modify the provisioned input/output operations per second (IOPS) capacity and change volume type on live production volumes. 

This blog illustrates how to increase and decrease the size of EBS volume with a step-by-step instruction. 

Increase the Size of EBS Volume

If you are familiar with Amazon EC2 and EBS volume, you might have experience modifying the size of EBS volume. AWS provides an easy way to increase the size of your EBS volume. In fact, we could increase it without detaching the volume or restarting the instance. That’s quite nice work done there because we would not need to have downtime for our instance. 

Step 1: Go to the EC2 service in the AWS console and click on the server-specific volume by selecting the EC2 server > Storage > volume ID

Step1

Step 2: Select the Volume > Actions > Modify volume

Step2

Step 3: Modify the volume size by changing the Size value > click Modify.

Step3

Extending OS file system

After you finish modifying the volume, you need to extend the OS file system to see your increased volume size. The example below is the command I used for Ubuntu OS.

Step 4: SSH into the server and check whether the storage disk space is allocated by using “lsblk” command. Your increased volume will be shown just above your current volume, e.g.,xvda1 is your current volume with 10GB size and  xvda with 15GB size.

Step4

Step 5: Extend the partition by typing: sudo growpart /dev/xvda 1.  

Note: Here dev/xvda denotes the partition name and 1 denotes the partition number.  

Further, extend the volume by typing: sudo resize2fs /dev/xvda1

Step5

Now let us check the EBS size allocated to our server by using the “lsblk” command.

Hence, we have increased the EBS volume from 10GB to 15GB. 

Decrease the Size of EBS Volume

In the previous task we have learned about increasing our EBS volume size. You might be wondering, can we decrease EBS volume size? The answer is NO. It is impossible to decrease EBS volume size. When you have 100GB EBS and you decide to modify it into 30GB you will get error: The size of a volume can only be increased, not decreased. That’s terrible … 

But don’t worry. We could also do the trick to decrease EBS volume size. However, it is not easy task as it is not straightforward. 

Here is the trick: 

  1. Snapshot the volume 
  2. Create a new smaller EBS volume 
  3. Attach the new volume 
  4. Format the new volume 
  5. Mount the new volume
  6. Copy data from old volume to the new volume 
  7. Prepare the new volume 
  8. Detach and unmount old volume 

 Assume we have: 

  • Instance named “my-instance” in “ap-south-1a” zone 
  • 30GB EBS volume size named “old-volume” 
  • We wish to decrease into 10GB and name it “new-volume.”

We would need to stop the instance to prevent inconsistencies. 

Step 1: Stop the EC2 Instance and create a new EBS volume with desired storage (In our case it is 10GB) in the same availability zone. Name it as new-volume.

Dstep1

Dstep2

Dstep1c

Step 2: Attach the new EBS volume to the Instance, start the Instance and SSH into the instance.

Dstep2

Dstep2b

Step 3: Mount the new volume 

  1.  Create filesystem for new EBS volume by using the command: sudo mkfs -t ext4 /dev/xvdf 
  2. Create a directory to mount using the command: sudo mkdir /mnt/new-volume 
  3. Mount new volume into the directory using the command: sudo mount /dev/xvdf /mnt/new-volume 
  4. Check volume with the command: “df -h”  
  5. The new volume should be successfully mounted now.

Dstep3

Dstep3b

Step 4: Copy data from the old volume to the new volume. 

Use rysnc to copy from the old volume to the new volume “sudo rsync -axv / /mnt/new-volume/” 

Relax and wait until it is finished. Get a coffee! 

Step 5: Prepare the new volume. 

  1. Install grub on new-volume using the command: sudo grub-install –root-directory=/mnt/new-volume/ –force /dev/xvdf 
  2. Unmount new-volume with the following command  sudo umount /mnt/new-volume 
  3. 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. 
  6. Check the system label from old-volume using command sudo e2label /dev/xvda1 It will display string like cloudimg-rootfs 
  7. Replace new-volume label with old-volume label using command sudo e2label /dev/xvdf cloudimg-rootfs 
  8. We can logout SSH now. 

Dstep5

Step 6: Detach old volume 

  1. Stop instance my-instance 
  2. Detach old-volume 
  3. Detach new-volume 
  4. Attach new-volume to /dev/sda1 
  5. Start instance my-instance 

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

We could delete old-volume and snapshot-old-volume if our system works correctly. I prefer to keep snapshot for a week / month before deleting it.

Dstep6

Dstep6b

Dstep6c

Conclusion

AWS provide a straightforward method to increase the volume size of EBS. We need to extend the OS file system to see the changes. We cannot straightforward decrease the size of EBS volume. All we can do is to create a new smaller volume and transfer all the data from old volume to new volume and make new volume as root volume. 

Hope it’s help! 

About CloudThat

CloudThat is also the official AWS (Amazon Web Services) Advanced Consulting Partner and Training partner and Microsoft gold partner, helping people develop knowledge of the cloud and help their businesses aim for higher goals using best in industry cloud computing practices and expertise. We are on a mission to build a robust cloud computing ecosystem by disseminating knowledge on technological intricacies within the cloud space. Our blogs, webinars, case studies, and white papers enable all the stakeholders in the cloud computing sphere.

Drop a query if you have any questions regarding EBS Volume in EC2 and I will get back to you quickly.

To get started, go through our Consultancy page and Managed Services Package that is CloudThat’s offerings.

FAQs

1. What is EBS volume in AWS? 

ANS: – An Amazon EBS volume is a durable, block-level storage device that you can attach to your instances.

2. Which EBS volume is best? 

ANS: – AWS Elastic Block Store (EBS) is Amazon’s block-level storage solution used with the EC2 cloud service to store persistent data. That means the data is stored on the AWS EBS servers even if the EC2 instances are shut down.

WRITTEN BY Md Shahid Afridi P

Share

Comments

    Click to Comment

Get The Most Out Of Us

Our support doesn't end here. We have monthly newsletters, study guides, practice questions, and more to assist you in upgrading your cloud career. Subscribe to get them all!