Resizing an EBS Volume

As a Cloud9 environment is hosted on an EC2 instance, that EC2 instance has an attached Elastic Block Store (EBS) volume. You can think of an EBS volume like an expandable hard disk, which is a feature we are now going to leverage as the Cloud9 EC2 instance’s default EBS volume is too small for our needs.

INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)

VOLUME_ID=$(aws ec2 describe-volumes \
  --query "Volumes[?Attachments[?InstanceId=='$INSTANCE_ID']].{ID:VolumeId}" \
  --output text)

aws ec2 modify-volume --volume-id $VOLUME_ID --size 32

Let’s break down what’s going on here. First, we are using a special request to the EC2 instance metadata endpoint that is available on all EC2 instances to retrieve the Cloud9 EC2 instance’s ID and store it in a shell variable.

Next, we use the AWS CLI for EC2 to query for the ID of the attached EBS volume. We need this for our next AWS CLI command, which modifies the volume to the specified size of 16 GiB.

Finally let’s ensure our Linux environment makes use of the newly allocated space:

sudo growpart /dev/nvme0n1 1

sudo xfs_growfs -d /