Monday 28 June 2010

Converting a local OS into an Amazon AMI



Converting a local OS into an Amazon AMI

These are the steps required to convert a local Linux OS instance to an Amazon AMI machine image. In this example I have converted a 64-bit CentOS 5.2 XEN VM instance to an AMI and launched it on EC2.

Pre-requisites

In this example I will assume you have installed the Amazon API and AMI tools and correctly setup the security keys as per this guide.

http://docs.amazonwebservices.com/AWSEC2/2008-05-05/GettingStartedGuide/?ref=get-started

You will need the following files:
wget http://s3.amazonaws.com/ec2-downloads/ec2-ami-tools.zip

wget http://s3.amazonaws.com/ec2-downloads/ec2-api-tools.zip

wget http://ec2-downloads.s3.amazonaws.com/ec2-modules-2.6.18-xenU-ec2-v1.0-x86_64.tgz

Creating the AMI image

The first step is to prep the system for booting properly in EC2. When booting, it will be forced to use the EC2 kernel, rather than the currently installed one. You can use other kernels by launching the instance under a different AMI/AKI identifier if it matches yours.

Prep kernel:

Unzip the tarball ec2-modules-2.6.18-xenU-ec2-v1.0-x86_64.tgz to /lib/modules/

Prep network:

Remove any hardware addresses in “ifcfg-eth0” and configure for DHCP
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=dhcp
TYPE=Ethernet

Prep fstab:

This can be done manually if you have specific volumes to mount, but I recommend letting the ec2 tools create it for you in the next section.

Building the AMI image:

Create a folder to create the image, in this example I use /image. You will need enough free space to create the image and it must not exceed 10G.

To create the image run the following command:
ec2-bundle-vol -d /image -k ~/.ec2/pk-.pem --cert ~/.ec2/cert-.pem -u --no-inherit -s 4000 --generate-fstab

When prompted for the arch type, select either i386 or x86_64 as required.

"--generate-fstab" - will create the required EC2 fstab file for you.
"--no-inherit" - Required if not re-bundling an EC2 image
"-s 4000" - This sets the image size in MB. I have made it as small as possible; I will then add a persistent volume to it and symlink /home /opt/ & /var/log/ to this.

Uploading AMI image to Amazon

You will need to upload the image to an S3 storage bucket so that it can be registered as an AMI instance.

ec2-upload-bundle -b tl-ami-images -m /image/image.manifest.xml --location EU -a -s -d /image/

"-b tl-ami-images" - This is where you define the S3 bucket name
"-m /image/image.manifest.xml" - Location of the manifest file created using ec2-bundle-vol
"--location EU" - US or EU

The access-keyid & secret key can be obtained from the AWS account under "Your Account"



Register & launch the new AMI

You can either use the API tools or the AWS Management Console to do this. The AWS Management Console is web based and much easier to use.
In the AWS console click on AMIs, the select "Register New AMI"
In the "Register Image" box enter the path to the S3 bucket image.
In this example you would enter tl-ami-images /image.manifest.xml




The AMI image will now appear in your list under "private" AMIs.


To launch open the "Instance" screen and select "Launch Instance"



Click "Select"



If you haven’t created a KeyPair or security group then do so. You only need to do this once for all instances. Launch it and your good to go !

No comments:

Post a Comment