Shrinking an EBS Volume (Disk volume)

Is not possible to Shrink an EBS Volume by console AWS or CLI, so we have to provide some tasks:

  1. Create and prepare a new small little Instance (t2.micro is sufficient)
  2. Create a new EBS volume that is the size you wish to shrink to
  3. Detach the volume you wish to resize from the current EC2 instance, take a snapshot (EBS Volume snapshot) and attach both volumes to the new, small EC2 instance you created
    • Mount the old volume as /dev/sdf (this becomes /dev/xvdf)
    • Mount the new volume as /dev/sdg (this becomes /dev/xvdg)
  4. Power on the new small instance and wait for it to come online
  5. SSH into the instance and run the following commands
  6. # e2fsck -f /dev/xvdf1

    to ensure that the file system is in order (change the number 1 to the partition number you wish to resize)

  7. If the e2fsck command ran without errors, now run
    # resize2fs -M -p /dev/xvdf1

    Again, change the 1 to the partition number you wish to resize if you’re not resizing the first one.

  8. The last line from the resize2fs command should tell you how many 4k blocks the filesystem now is. To calculate the number of 16MB blocks you need, use the following formula: blockcount * 4 / (16 * 1024). Round this number up to give yourself a little buffer.
  9. On /dev/xvdg create a partition on your new volume if you dont yet have it (/dev/xvdg1) using fdisk.
  10. Execute the following command, using the number you came up with in the previous step:
    # dd bs=16M if=/dev/xvdf1 of=/dev/xvdg1 count=numberfrompreviousstep

    Depending on how large your volume is this may take several minutes to run — let it finish.

  11. After the copy finishes, resize and check and make sure that everything is in order with the new filesystem by running
    # resize2fs -p /dev/xvdg1

    followed by

    # e2fsck -f /dev/xvdg1
  12. After this step is complete, detach both volumes from the new instance you created. Attach the shrunken volume to the old EC2 instance as /dev/sda1 (your boot device) and restart your old instance. Save the previous, larger volume until you’ve validated that everything is working properly. When you’ve verified things are working well, feel free to delete the new EC2 instance you created, plus the larger volume and snapshot.

Expanding an EBS Volume

Expanding the size of an EBS volume is a bit easier, since we dont have to execute a disk-disk copy. To expand the size of the volume, execute the following steps:

  1. Detach the volume you wish to expand from the current EC2 instance and take a snapshot of your EC2 instance (EBS Volume snapshot)
  2. Create a new EBS volume from the snapshot specifying the new size
  3. Attach the new EBS volume to your existing EC2 instance, as /dev/sda1 if this is the root volume
  4. Power on your existing instance and wait for it to come online
  5. SSH into the instance and run the following commands
  6. To ensure that the file system is in order, run
    # e2fsck -f /dev/xvda1

    If you’re resizing a different partition on the drive, change the number 1 to the partition number you wish to resize.

  7. If the e2fsck command ran without errors, now run
    # resize2fs -p /dev/xvda1

    Again, change the 1 to the partition number you wish to resize if you’re not resizing the first one.

  8. Save the previous, smaller volume and the snapshot until you’ve validated that everything is working properly. When you’ve verified things are working well, feel free to delete the original volume and snapshot.