Skip to content

Instantly share code, notes, and snippets.

@allanlei
Created May 2, 2013 08:39
Show Gist options
  • Save allanlei/5500965 to your computer and use it in GitHub Desktop.
Save allanlei/5500965 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Procedure
# 1. Stop instance
# 2. Detach volume
# 3. Make Snapshot
# 4. Make new volume from snapshop with new size
# 5. Attach volume to instance
# 6. Start instance
instanceid=INSTANCE_TO_RESIZE
size=SIZE_TO_RESIZE_TO
oldvolumeid=$(ec2-describe-instances $instanceid | egrep "^BLOCKDEVICE./dev/sda1" | cut -f3)
zone=$(ec2-describe-instances $instanceid | egrep ^INSTANCE | cut -f12)
echo "Resizing instance $instanceid in $zone with volume $oldvolumeid..."
echo "Starting resize.."
# 1.
echo "Instance stopping: $instanceid"
ec2-stop-instances $instanceid
echo "Instance stopped: $instanceid"
# 2.
echo "Detaching volume: $oldvolumeid"
while ! ec2-detach-volume $oldvolumeid; do sleep 1; done
echo "Volume detached: $oldvolumeid"
# 3.
echo "Creating snapshot..."
snapshotid=$(ec2-create-snapshot $oldvolumeid | cut -f2)
while ec2-describe-snapshots $snapshotid | grep -q pending; do sleep 1; done
echo "Snapshot of $oldvolumeid created: $snapshotid"
# 4.
echo "Creating volume from snapshot $snapshotid"
newvolumeid=$(ec2-create-volume --availability-zone $zone --size $size --snapshot $snapshotid | cut -f2)
echo "Volume of size $size from snapshot $snapshotid created in $zone: $newvolumeid"
# 5.
ec2-attach-volume --instance $instanceid --device /dev/sda1 $newvolumeid
while ! ec2-describe-volumes $newvolumeid | grep -q attached; do sleep 1; done
echo "Volume attached to $instanceid: $newvolumeid"
# 6.
ec2-start-instances $instanceid
while ! ec2-describe-instances $instanceid | grep -q running; do sleep 1; done
ec2-describe-instances $instanceid
echo "Instance started: $instanceid"
echo "Done resizing!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment