Monday, January 25, 2010

Steps to creating a EBS image from an AMI (Windows)

Download Amazon's EC2 API Tools to C:\ec2-api-tools

# Run the following on a remote machine with Amazon's EC2 API Tools installed
# Adjust these variables to match your own system
set EC2_HOME=c:\ec2-api-tools
set JAVA_HOME=c:\Program Files\Java\jre6

# Update PATH
set PATH=%PATH%:c:\ec2-api-tools\bin

#Test by running
ec2-version
# Output: 1.3-46266 2009-11-30

# Add keys
set EC2_PRIVATE_KEY=c:\aws-keys\pk-XXXXXXX.pem
set EC2_CERT=c:\aws-keys\cert-XXXXXXX.pem

# Use AWS Management Console
# Create new instance based on existing AMI
# Once instance is launch and running create a volume, make sure that the availability zone is the same as the instance, set device to /dev/sdh
# Attach the volume to the instance
# Connect to the instance using Putty
# Create the following script
vi instance-to-ebs-ami.sh

#!/bin/bash
# Run this script on the instance to be bundled
# tested with Canonical Ubuntu 9.10 base ami

EBS_DEVICE=${1:-'/dev/sdh'}
IMAGE_DIR=${2:-'/mnt/tmp'}
EBS_MOUNT_POINT=${3:-'/mnt/ebs'}

mkdir -p $EBS_MOUNT_POINT
mkfs.ext3 ${EBS_DEVICE}
mount ${EBS_DEVICE} $EBS_MOUNT_POINT

#make a local working copy
mkdir /mnt/tmp
rsync --stats -av --exclude /root/.bash_history --exclude /home/*/.bash_history --exclude /etc/ssh/ssh_host_* --exclude /etc/ssh/moduli --exclude /etc/udev/rules.d/*persistent-net.rules --exclude /var/lib/ec2/* --exclude=/mnt/* --exclude=/proc/* --exclude=/tmp/* / $IMAGE_DIR

#ensure that ami init scripts will be run
chmod u+x $IMAGE_DIR/etc/init.d/ec2-init-user-data

#clear out log files
cd $IMAGE_DIR/var/log
for i in `ls ./**/*`; do
echo $i && echo -n> $i
done

cd $IMAGE_DIR
tar -cSf - -C ./ . | tar xvf - -C $EBS_MOUNT_POINT
#NOTE, You could rsync / directly to EBS_MOUNT_POINT, but this tar trickery saves some space in the snapshot

umount $EBS_MOUNT_POINT

chmod +x instance-to-ebs-ami.sh
./instance-to-ebs-ami.sh
exit

# Go back to AWS Management Console
# Detach the volume
# Create a snapshot of the volume

# Now, we register the snapshot to be able to create an EBS image
# Run this command from your remote machine with Amazon's EC2 API Tools installed
# Fill in the required variables

ec2-register --snapshot snap-xxxxxx --kernel aki-xxxxxxx --ramdisk ari-xxxxxxxx --description="" --name="debian-5.0-lenny-base-20091011" --architecture i386 --root-device-name /dev/sda1
#sample output => IMAGE ami-xxxxxxx


References/Credits:
http://www.capsunlock.net/2009/12/create-ebs-boot-ami.html
http://gist.github.com/249915

Try this script for creating automated snapshots:
http://developer.amazonwebservices.com/connect/entry.jspa?categoryID=100&externalID=1663
or
http://johnwoconnor.blogspot.com/2009/06/automatic-volume-snapshots-using-amazon.html