Skip to content

Instantly share code, notes, and snippets.

@thefron
Created November 25, 2011 13:39
Show Gist options
  • Save thefron/1393550 to your computer and use it in GitHub Desktop.
Save thefron/1393550 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"
@thefron
Copy link
Author

thefron commented Nov 26, 2011

export PATH=$PATH:/usr/local/bin:/Users/[user]/bin/ec2-api-tools-1.4.4.2/bin
export EC2_HOME=/Users/[user]/bin/ec2-api-tools-1.4.4.2/
export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Home
export EC2_PRIVATE_KEY=/.ssh/pk.pem
export EC2_CERT=
/.ssh/cert.pem
export EC2_URL=https://ec2.us-west-1.amazonaws.com

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment