Skip to content

Instantly share code, notes, and snippets.

@landsman
Forked from kshcherban/ec2-root-resize.sh
Created December 3, 2018 19:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save landsman/83f917c0bc21e75b134b1095f1bfb99d to your computer and use it in GitHub Desktop.
Save landsman/83f917c0bc21e75b134b1095f1bfb99d to your computer and use it in GitHub Desktop.
ec2 root volume resize
#!/bin/bash -e
if [ $# -ne 2 ]; then
echo "Usage: $0 <instance-id> <volume-size-gb> [PARITION_NAME=/dev/xvda2]"
exit 2
fi
sudo apt-get install jq -y
INSTANCE="$1"
VOLUME_SIZE="$2"
PARTITION="${3:-/dev/xvda2}"
PARTITION_NUMBER=$(echo $PARTITION | tail -c2)
DISK_NAME=$(echo $PARTITION | sed 's/'$PARTITION_NUMBER'//')
# Retrieve all needed information
instance_data=$(aws ec2 describe-instances --instance-ids $INSTANCE)
root_volume=$(echo $instance_data | jq -r '.Reservations[0].Instances[0].BlockDeviceMappings[0].Ebs.VolumeId')
instance_ip=$(echo $instance_data | jq -r '.Reservations[0].Instances[0].NetworkInterfaces[0].PrivateIpAddress')
# Modify volume
aws ec2 modify-volume --volume-id $root_volume --size $VOLUME_SIZE
sleep 5
# Update root partition
ssh $instance_ip "sudo growpart $DISK_NAME $PARTITION_NUMBER"
ssh $instance_ip "sudo resize2fs $PARTITION"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment