-
-
Save landsman/83f917c0bc21e75b134b1095f1bfb99d to your computer and use it in GitHub Desktop.
ec2 root volume resize
This file contains 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 -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