Create an EC2 instance-store AMI from a running Ubuntu instance

04/29/2014
0

Since Amazon started offering EBS instances the classic instance-store images have gotten little love. But when you want to spawn lots and lots of instances a simple instance-store AMI is more flexible and cheaper.

The following steps let you create an instance-store AMI from a running Ubuntu 14.04 LTS instance.

Setting up your current instance

Start an instance-store Ubuntu image. You can find the correct image for your region with the Ubuntu Amazon EC2 AMI Locator.

SSH into your instance and install the EC2 ami and api tools

If you don’t use Ubuntu 14.04 you can adapt the following lines by replacing the trusty part with your version.

Add the following lines to your /etc/apt/sources.list file:

deb http://us.archive.ubuntu.com/ubuntu/ trusty multiverse
deb-src http://us.archive.ubuntu.com/ubuntu/ trusty multiverse
deb http://us.archive.ubuntu.com/ubuntu/ trusty-updates multiverse
deb-src http://us.archive.ubuntu.com/ubuntu/ trusty-updates multiverse

Run these two commands:

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

Copy your keys to the instance

You need the X.509 certificate and your private key. You can create them here in the X.509 Certificates section.

Copy both files to the /mnt directory on you instance and rename them to pk.pem and cert.pem.

Bundle your instance and create the image

This step creates the image in /mnt. Don’t worry, the keys won’t be included. In the following line replace the 123456789012 with your AWS account number. You can find yours here in the Account identifiers section.

Important: Make sure you remove the dashes, so that 1234-5678-9012 becomes 123456789012. Change the i386 to x86_64 if you use a 64-Bit image.

sudo ec2-bundle-vol -d /mnt -k /mnt/pk.pem -c /mnt/cert.pem –u 1234567890 -r i386

This might take a while.

Upload your bundled instance to S3

Optional: Create a new S3 bucket where you store your new image.

Upload your image to S3:

ec2-upload-bundle -b YourBucket -m /mnt/image.manifest.xml -a YourAWSAccessKey -s YourAWSSecretKey --location eu-west-1

Note: You might need to modifiy the --location.

Register your new AMI

ec2-register -n AnImageName –K /mnt/pk.pem –C /mnt/cert.pem YourBucket/image.manifest.xml --region eu-west-1

You are now ready to run your newly created AMI.

In case you need to deregister your AMI just use:

ec2-deregister Your-AMI-Id

If you've read this far you might as well follow me on Twitter here.


Comments