Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save wongcyrus/a4e726b961260395efa7811cab0b4516 to your computer and use it in GitHub Desktop.
Save wongcyrus/a4e726b961260395efa7811cab0b4516 to your computer and use it in GitHub Desktop.
Cloud9 Disk Resize
curl -s https://gist.githubusercontent.com/wongcyrus/a4e726b961260395efa7811cab0b4516/raw/6a045f51acb2338bb2149024a28621db2abfcaab/resize.sh | bash /dev/stdin 60
#!/bin/bash
# Specify the desired volume size in GiB as a command line argument. If not specified, default to 20 GiB.
SIZE=${1:-20}
# Get the ID of the environment host Amazon EC2 instance.
INSTANCEID=$(curl http://169.254.169.254/latest/meta-data/instance-id)
REGION=$(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone | sed 's/\(.*\)[a-z]/\1/')
# Get the ID of the Amazon EBS volume associated with the instance.
VOLUMEID=$(aws ec2 describe-instances \
--instance-id $INSTANCEID \
--query "Reservations[0].Instances[0].BlockDeviceMappings[0].Ebs.VolumeId" \
--output text \
--region $REGION)
# Resize the EBS volume.
aws ec2 modify-volume --volume-id $VOLUMEID --size $SIZE
# Wait for the resize to finish.
while [ \
"$(aws ec2 describe-volumes-modifications \
--volume-id $VOLUMEID \
--filters Name=modification-state,Values="optimizing","completed" \
--query "length(VolumesModifications)"\
--output text)" != "1" ]; do
sleep 1
done
#Check if we're on an NVMe filesystem
if [[ -e "/dev/xvda" && $(readlink -f /dev/xvda) = "/dev/xvda" ]]
then
# Rewrite the partition table so that the partition takes up all the space that it can.
sudo growpart /dev/xvda 1
# Expand the size of the file system.
# Check if we're on AL2
STR=$(cat /etc/os-release)
SUB="VERSION_ID=\"2\""
if [[ "$STR" == *"$SUB"* ]]
then
sudo xfs_growfs -d /
else
sudo resize2fs /dev/xvda1
fi
else
# Rewrite the partition table so that the partition takes up all the space that it can.
sudo growpart /dev/nvme0n1 1
# Expand the size of the file system.
# Check if we're on AL2
STR=$(cat /etc/os-release)
SUB="VERSION_ID=\"2\""
if [[ "$STR" == *"$SUB"* ]]
then
sudo xfs_growfs -d /
else
sudo resize2fs /dev/nvme0n1p1
fi
fi
@henrybravo
Copy link

great script! suggesting to silence the curl:

curl -s http://169.254.169.254/latest/meta-data/instance-id

@brunokunace
Copy link

i try to use this on cloud9, but...

An error occurred (IncorrectModificationState) when calling the ModifyVolume operation: Volume vol-xxxxxxxxxx cannot be modified in modification state OPTIMIZING
FAILED: /dev/xvda: does not exist
resize2fs 1.44.1 (24-Mar-2018)
open: No such file or directory while opening /dev/xvda1

@john
Copy link

john commented Jun 29, 2021

Got the same error as @brunokunace, following the procedure at the URL below, which I believe is the original source of this gist:
https://docs.aws.amazon.com/cloud9/latest/user-guide/move-environment.html#move-environment-resize

@mhnatiuk
Copy link

that's because most AWS instances use NVME ssd drives and name of the linux device is different (probably connected using some kind of PCI-express like slot) while classic ssds appear under /dev/xvd** , one needs to add one more argument to this script

@scottche
Copy link

scottche commented Jun 3, 2022

If you have IMDSv2 turned on then the metadata curls will come back blank. Change the script to get the metadata token and then use it to get the metadata like this:

# Get the IMDBv2 metadata token
TOKEN=`curl -s -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"`

# Get the ID of the environment host Amazon EC2 instance.
INSTANCEID=$(curl -s -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/instance-id)
REGION=$(curl -s -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/placement/availability-zone | sed 's/\(.*\)[a-z]/\1/')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment