Today is a good day to code

Script to Bundle Running EC2 Instance

Posted: October 1st, 2010 | Author: | Filed under: Amazon, Companies | Tags: , , , , , , | No Comments »

For the past few hours I have been battling with trying to create a script to bundle a running EC2 instance.  After many, S3 access denied, errors,  “Error talking to S3: Server.OperationAborted(409): A conflicting conditional operation is currently in progress against this resource. Please try again.” errors, and “you are trying to upload to a different region than you are bundling in errors, I think I finally have it.

The key is to just allow the script to create the bucket.  I used s3fox to create the bucket, and it built it for no region.  Performing a manifest migration solved one problem, but after that one was solved, I still kept getting the error.  After I deleted the bucket, and changed the name of the bucket in the script, it was still giving me the “OperationAborted” issue.  After a bunch of trying, this script got me to the gold.

#!/bin/bash

remotehost=yourec2instanceaddress

remoteuser=root

bucket=yourbucketname

prefix=yourprefixname

AWS_USER_ID=yourawsuserid

AWS_ACCESS_KEY_ID=yourawsaccesskeyid

AWS_SECRET_ACCESS_KEY=yourawssecretaccesskey

rsync –rsh=”ssh -i
|pathtoyourawskey|.pem” –rsync-path=”sudo rsync” |pathtokeys|{cert,pk}-*.pem $remoteuser@$remotehost:/mnt/

ssh -i |pathtoyourawskey|.pem $remoteuser@$remotehost “\

\

export JAVA_HOME=/usr;\

sudo -E ec2-bundle-vol -r i386 -d /mnt -p $prefix -u $AWS_USER_ID -k /mnt/pk-*.pem -c /mnt/cert-*.pem -s 10240 -e /mnt,/root/.ssh;\

sudo ec2-migrate-manifest –region us-west-1 –manifest /mnt/$prefix.manifest.xml -k /mnt/pk-*.pem -c /mnt/cert-*.pem -a $AWS_ACCESS_KEY_ID -s $AWS_SECRET_ACCESS_KEY

ec2-upload-bundle –location us-west-1 -b $bucket -m /mnt/$prefix.manifest.xml -a $AWS_ACCESS_KEY_ID -s
$AWS_SECRET_ACCESS_KEY –acl aws-exec-read;\

ec2-register –region us-west-1 -K /mnt/pk-*.pem -C /mnt/cert-*.pem $bucket/$prefix.manifest.xml;”

Another super important thing was to ( I use ubuntu ) perform an apt-get ec2-ami-tools to make sure you have the ec2-register installed.  It will install java as a result, but will not set the JAVA_HOME environment variable.  It just wants to be able to find the binary so I added the export JAVA_HOME=/usr.  Java is in /usr/bin/java.

After everything is up, you’ll still have quite the mess in your /mnt folder, but I wrote a cleanup script to deal with that.

Happy scaling!