Created
November 13, 2018 14:28
-
-
Save kshcherban/a162af510d79e7542afb3cb8878a8bb2 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 | |
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