In the last artice, we launched an ec2 instance but we didn’t do anything after that. You will discover that most AMIs have a default size and soon you will be wishing if you could increase the size of the EBS of your ec2 instance. And while there are various ways of doing this, the way i did mine was to heavily borrow a script from Eric Hammond and specifically, this article. However, as someone starting out on AWS at the point in time, i ran into various issues and what follows are ways that tried to solve those issues while answering the questions.
Generate Credentials
To resize you will need to start a temporary instance on which you will need to run a script. But before that you will need two important files from your Amazon AWS account. These can be generated and downloaded from the Security Credentials area which is located here. Under Access Credentials and in the X.509 Certificates, create a new certificate and download the files. You will require them at a later stage.
Start the temporary instance
Start a temporary instance, you might follow the steps given in an earlier article.
1. Log into the temporary instance using ssh.
2. If using Ubuntu, enable Multiverse by editing the /etc/apt/sources.list file.
3. sudo apt-get update
3. sudo apt-get install ec2-api-tools
Resize Script
Run the resize script but take into account the variables that need to be changed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
#instanceid=YOUR INSTANCE ID e.g i-2bd00d57
#size=TARGET SIZE IN GB e.g 40
#pk_pem=PATH TO PRIVATE KEY FILE e.g keys/x.509/pk.pem
#cert_pem=PATH TO CERT FILE keys/x.509/cert.pem
#region=YOUR INSTANCE REGION e.g ap-southeast-1
oldvolumeid=$(ec2-describe-instances -K $pk_pem -C $cert_pem --region $region $instanceid |
egrep "^BLOCKDEVICE./dev/sda1" | cut -f3)
zone=$(ec2-describe-instances -K $pk_pem -C $cert_pem --region $region $instanceid | egrep ^INSTANCE | cut -f12)
echo "instance $instanceid in $zone with original volume $oldvolumeid"
echo "Stopping instance : $instanceid"
ec2-stop-instances -K $pk_pem -C $cert_pem --region $region $instanceid
echo "Stopped instance : $instanceid"
echo "Detaching volume : $oldvolumeid"
while ! ec2-detach-volume -K $pk_pem -C $cert_pem --region $region $oldvolumeid; do sleep 1; done
echo "Detached volume : $oldvolumeid"
echo "Taking snapshot of volume : $oldvolumeid"
snapshotid=$(ec2-create-snapshot -K $pk_pem -C $cert_pem --region $region $oldvolumeid | cut -f2)
while ec2-describe-snapshots -K $pk_pem -C $cert_pem --region $region $snapshotid | grep -q pending; do sleep 1; done
echo "Taken snapshot of volume: $oldvolumeid and new snapshot is : $snapshotid"
echo "Creating new volume from snapshot : $snapshotid"
newvolumeid=$(ec2-create-volume -K $pk_pem -C $cert_pem --region $region --availability-zone $zone --size $size --snapshot $snapshotid |
cut -f2)
echo "Created new volume: $newvolumeid"
echo "attaching volume : $newvolumeid to instance : $instanceid"
ec2-attach-volume -K $pk_pem -C $cert_pem --region $region --instance $instanceid --device /dev/sda1 $newvolumeid
while ! ec2-describe-volumes -K $pk_pem -C $cert_pem --region $region $newvolumeid | grep -q attached; do sleep 1; done
echo "attached volume : $newvolumeid to instance : $instanceid"
echo "start instance : $instanceid"
ec2-start-instances -K $pk_pem -C $cert_pem --region $region $instanceid
while ! ec2-describe-instances -K $pk_pem -C $cert_pem --region $region $instanceid | grep -q running; do sleep 1; done
ec2-describe-instances -K $pk_pem -C $cert_pem --region $region $instanceid
echo "Resize done!" |
CollectiveZen
Tel: (+91) 020 2672-2150
Mobile: (+91) 7875757782
The convergence of commoditized infrastructure, multi-gestural interfaces, the emerging ubiquity of the mobile Internet, and location-based services has made the world an exciting place and we are extremely passionate about the potential that technology holds for all of us.