Create a reusable EC2 AMI from your instance

Here's how to bundle your running instance into its own AMI, in case you're wary of running your code on a server you didn't set up yourself.

Much of this post is based on Creating a New Image for EC2 by Rebundling a Running Instance from alestic.

Note: these instructions leave your ssh keys in the bundled AMI, and so are not appropriate for creating public AMIs.

These instructions assume you have set-up S3 in the manner described in the previous post.

bundle the instance

locally

copy your aws auth to the remote instance

> scp -r ~/.ec2 ec2-ubunutu:

on the remote machine

move the auth keys to /mnt/

> sudo mv .ec2 /mnt/

enable multiverse

> sudo sed -i.dist s/universe/multiverse/ /etc/apt/sources.list

install ec2-api-tools

> sudo apt-get install ec2-ami-tools
> sudo apt-get install ec2-api-tools

if you want to use the instance on micro, comment out the following line in /etc/fstab

> sudo vi /etc/fstab
# /dev/sda2 /mnt    auto    defaults,comment=cloudconfig    0   0

for the reason behind this, see

  1. http://stackoverflow.com/questions/3679156/ec2-small-to-micro-instance-downgrade-problems
  2. http://www.mail-archive.com/ubuntu-bugs@lists.ubuntu.com/msg2480051.html

setup environment variables

source /mnt/ec2/aws-keys
export EC2_PRIVATE_KEY=/mnt/ec2/pk-*.pem
export EC2_CERT=/mnt/ec2/cert-*.pem
export JAVA_HOME=/usr/lib/jvm/java-6-openjdk/

clean up after yourself before you save your tracks forever

> history -c && sudo rm -f /root/.*hist* /home/ubuntu/.*hist*

Bundle the files on the current instance into a copy of the image under /mnt note: this takes a while so you may want to run it under screen.

> sudo ec2-bundle-vol                   \
  -r i386                               \
  -k $EC2_PRIVATE_KEY                   \
  -c $EC2_CERT                          \
  -u $AWS_USER_ID                       \
  -d /mnt                               \
  -p myami                              \
  -e /mnt

Upload the bundle to a bucket on S3

> ec2-upload-bundle                 \
  -b myec2bucket     \
  -m /mnt/myami.manifest.xml  \
  -a $AWS_ACCESS_KEY_ID             \
  -s $AWS_SECRET_ACCESS_KEY

register the image as a new AMI.

> ec2-register                                             \
  --name myec2bucket/myami                                 \
  -K $EC2_PRIVATE_KEY                                      \
  -C $EC2_CERT                                             \
  myec2bucket/myami.manifest.xml

after the image is registered, you will see output like ami-1234de7b

create a new instance from the AMI

Just run the ami identified in the output from the above step with ec2-run-instances. It should be all set-up and ready to go.

> ec2-run-instances ami-98c035f1 --instance-type t1.micro --key mykeypair

A list of --instance-type options is available at http://docs.amazonwebservices.com/AWSEC2/latest/DeveloperGuide/