Skip to content

Instantly share code, notes, and snippets.

@chrishomer
Created October 17, 2011 18:05
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save chrishomer/1293317 to your computer and use it in GitHub Desktop.
Save chrishomer/1293317 to your computer and use it in GitHub Desktop.
Resizing an instance backed by an EBS drive on ec2
# IMPORTANT: On your machine, with the ec2 instance running
# Basic Config variables
instanceid=<instance-id>
size=50
#Get the root EBS volume id and availability zone for this instance:
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"
#Stop (not terminate!) the instance:
ec2-stop-instances $instanceid
#Detach the original volume from the instance:
while ! ec2-detach-volume $oldvolumeid; do sleep 1; done
#Create a snapshot of the original volume:
snapshotid=$(ec2-create-snapshot $oldvolumeid | cut -f2)
while ec2-describe-snapshots $snapshotid | grep -q pending; do sleep 1; done
echo "snapshot: $snapshotid"
#Create a new volume from the snapshot, specifying a larger size:
newvolumeid=$(ec2-create-volume --availability-zone $zone --size $size --snapshot $snapshotid |
cut -f2)
echo "new volume: $newvolumeid"
#Attach the new volume to the instance:
ec2-attach-volume --instance $instanceid --device /dev/sda1 $newvolumeid
while ! ec2-describe-volumes $newvolumeid | grep -q attached; do sleep 1; done
#Start the instance and find its new public IP address/hostname. (If you were using an elastic IP address, re-assign it to the instance.)
ec2-start-instances $instanceid
while ! ec2-describe-instances $instanceid | grep -q running; do sleep 1; done
ec2-describe-instances $instanceid
# IMPORTANT: Connect to the instance with ssh (not shown) and resize the root file system to fill the new EBS volume:
# ext3 root file system (most common)
sudo resize2fs /dev/xvda1
#Show that the root file system is the new, larger size (20 GB):
df -h /
# IMPORTANT: Back on your machine
#Delete the old EBS volume and snapshot if you no longer need them, though I recommend you at least keep the snapshot for a while just in case:
ec2-delete-volume $oldvolumeid
ec2-delete-snapshot $snapshotid
@sangramanand
Copy link

steps looks kool... easy to configure..

@cgbystrom
Copy link

Thanks, I found it useful.

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