Skip to content

Instantly share code, notes, and snippets.

@inbeom
Forked from thefron/resize_instance.sh
Created November 5, 2012 12:14
Show Gist options
  • Save inbeom/4016908 to your computer and use it in GitHub Desktop.
Save inbeom/4016908 to your computer and use it in GitHub Desktop.
EC2 instance resizing script
#!/bin/bash
# resize_instance.sh
# Resize specified instance with given size
# resize_instance.sh [instance_id] [size]
# This script is based on http://alestic.com/2010/02/ec2-resize-running-ebs-root
E_BADARGS=65
if [ ! -n "$1" ]
then
echo "Usage: `basename $0` [instance_id] [size]"
exit $E_BADARGS
fi
instanceid=$1
size=$2
echo "About to increase instance $1 to $2 GB"
oldvolumeid=$(ec2-describe-instances $instanceid | egrep "^BLOCKDEVICE./dev/sda1" | cut -f3)
zone=$(ec2-describe-instances $instanceid | egrep ^INSTANCE | cut -f12)
echo "instance $instanceid in $zone with original volume $oldvolumeid"
ec2-stop-instances $instanceid
while ! ec2-detach-volume $oldvolumeid; do sleep 1; done
snapshotid=$(ec2-create-snapshot $oldvolumeid | cut -f2)
while ec2-describe-snapshots $snapshotid | grep -q pending; do sleep 1; done
echo "snapshot: $snapshotid"
newvolumeid=$(ec2-create-volume --availability-zone $zone --size $size --snapshot $snapshotid | cut -f2)
echo "new volume: $newvolumeid"
ec2-attach-volume --instance $instanceid --device /dev/sda1 $newvolumeid
while ! ec2-describe-volumes $newvolumeid | grep -q attached; do sleep 1; done
ec2-start-instances $instanceid
while ! ec2-describe-instances $instanceid | grep -q running; do sleep 1; done
ec2-describe-instances $instanceid
echo "to delete previous volume, type: ec2-delete-volume $oldvolumeid"
echo "to snapshot, type: ec2-delete-snapshot $snapshotid"
echo "All done. Connect to instance and type: sudo resize2fs /dev/sda1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment