Last active
July 29, 2024 13:03
-
-
Save ao/70c03e0a42d82dc9d0e89d3ee27fcf3a to your computer and use it in GitHub Desktop.
Resize script for AWS Cloud9 instances
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Specify the desired volume size in GiB as a command line argument. If not specified, default to 50 GiB. | |
SIZE=${1:-50} | |
# Get the ID of the environment host Amazon EC2 instance. | |
TOKEN=$(curl -s -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 60") | |
INSTANCEID=$(curl -s -H "X-aws-ec2-metadata-token: $TOKEN" -v http://169.254.169.254/latest/meta-data/instance-id 2> /dev/null) | |
REGION=$(curl -s -H "X-aws-ec2-metadata-token: $TOKEN" -v http://169.254.169.254/latest/meta-data/placement/region 2> /dev/null) | |
# 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 or AL2023 | |
STR=$(cat /etc/os-release) | |
SUBAL2="VERSION_ID=\"2\"" | |
SUBAL2023="VERSION_ID=\"2023\"" | |
if [[ "$STR" == *"$SUBAL2"* || "$STR" == *"$SUBAL2023"* ]] | |
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 or AL2023 | |
STR=$(cat /etc/os-release) | |
SUBAL2="VERSION_ID=\"2\"" | |
SUBAL2023="VERSION_ID=\"2023\"" | |
if [[ "$STR" == *"$SUBAL2"* || "$STR" == *"$SUBAL2023"* ]] | |
then | |
sudo xfs_growfs -d / | |
else | |
sudo resize2fs /dev/nvme0n1p1 | |
fi | |
fi |
Or specify the size you want..
SIZE=100 && curl -L -s https://gist.githubusercontent.com/ao/70c03e0a42d82dc9d0e89d3ee27fcf3a/raw/57c23f9e5f08d5359d292e1773b7ccc77fc74c23/resize.sh | sed "s/50/$SIZE/g" | bash
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Run this as:
curl -L -s https://gist.githubusercontent.com/ao/70c03e0a42d82dc9d0e89d3ee27fcf3a/raw/57c23f9e5f08d5359d292e1773b7ccc77fc74c23/resize.sh | bash