Resizing an EBS disk

by Sebastien Mirolo on Fri, 21 Oct 2016

Sometimes you underestimated the disk size required to setup and run a service on AWS. The quickest way to keep moving forward is to resize the EBS storage associated with the EC2 instance.

Amazon's instructions to expand an EBS volume through the AWS console are straightforward. Here the steps from the command line.

We will first need to take note of the instance_id, availability_zone volume_id, and device_id.

Terminal
$ aws ec2 describe-instances --instance-ids *instance-id*
...
"Placement": {
    "AvailabilityZone": "*availability_zone*"
...
"BlockDeviceMappings": [{
    "DeviceName": "/dev/sda1",
    "Ebs": {
        "VolumeId": "*volume_id*",
...

volume_id=
instance_id=
availability_zone=
device_id=/dev/sda1

With the required information noted, we are ready to go. Let's stop the instance first.

Terminal
$ aws ec2 stop-instances --instance-ids *instance-id*

We then create a snapshot of the current volume.

Terminal
$ aws ec2 create-snapshot --volume-id *volume-id*
...
    "SnapshotId": "*snap-id*",
...

We can now create a new volume from the snapshot, setting it to the desired size. Note that we must make sure the availability zone of the volume matches the availability zone of the EC2 instance otherwise we won't be able to associated the volume to the instance.

Terminal
$ aws ec2 create-volume --size 32 --availability-zone *zone-id* --snapshot-id *snap-id*
...
    "VolumeId": "*new-volume-id*",
...
$ aws ec2 detach-volume --volume-id *volume-id*
$ aws ec2 attach-volume --instance-id *instance-id* --device /dev/sda1 --volume-id *new-volume-id*

It is time to restart the instance.

Terminal
$ aws ec2 start-instances --instance-ids *instance-id*

We can check that the new volume size is reflected so we don't need to extend the Linux File System in our case.

Terminal
$ df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/xvda1       32G  8.8G   22G  29% /

A bit of clean up: we delete the previous volume and snapshot.

Terminal
$ aws ec2 delete-volume --volume-id *volume-id*
$ aws ec2 delete-snapshot --snapshot-id *snap-id*

More to read

If you are looking for more AWS related posts, you might be interested to read PostgreSQL, encrypted EBS volume and Key Management Service and Deploying on EC2 with Ansible.

More technical posts are also available on the DjaoDjin blog, as well as business lessons we learned running a SaaS application hosting platform.

by Sebastien Mirolo on Fri, 21 Oct 2016


Receive news about DjaoDjin in your inbox.

Bring fully-featured SaaS products to production faster.

Follow us on