Sunday, October 19, 2008

How to create an EC2 AMI

The easiest way to create an EC2 AMI (Amazon Machine Image) is to select one of the publicly available AMIs which suits your requirement. Let's say you selected an Ubuntu image, ami-0757b26e. If you don't have an Amazon Web Services (AWS) account, first create one. Also you will need to download the EC2 command line tools & then set them up. For instructions on how to setup the EC2 tools, read this.

Follw these step to create your AMI:

0. Generate a keypair if you have not already done so
e.g. ec2-add-keypair key1
The output will be something like the following:

KEYPAIR key1
-----BEGIN RSA PRIVATE KEY-----
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
-----END RSA PRIVATE KEY-----

Copy the string starting from -----BEGIN RSA PRIVATE KEY----- up to -----END RSA PRIVATE KEY----- and save it in your keys directory, say in the /home/azeez/.ec2/keys/id_key1 file. Make sure that only the owner can read & write to that file.
i.e. chmod 600 /home/azeez/.ec2/keys/id_key1

1. Launch an instance of ami-0757b26e, providing a key, say key1, which you generated in step 0
ec2-run-instances ami-0757b26e -k key1
or you could use the ElasticFox GUI to do the same thing
The output will be something similar to
---------------------------------------------------------------------------------------------------
RESERVATION r-d5825cbc 610968236798 default
INSTANCE i-5c7dd335 ami-0757b26e pending key1 0 m1.small 2008-10-20T03:25:27+0000 us-east-1b aki-a71cf9ce ari-a51cf9cc
---------------------------------------------------------------------------------------------------

2. Connect to that instance using SSH,
e.g. ssh -i /home/azeez/.ec2/keys/id_key1 root@ec2-67-202-60-248.compute-1.amazonaws.com

3. Make the necessary changes to that instance. For example, you may install some custom software on that instance.

4. Upload your Amazon Web Services (AWS) private key (PK) & certificate (CERT) files to that instance. You can use scp to do this.
scp -i /home/azeez/.ec2/keys/id_key1 pk-XXX.pem cert-xxx.pem root@ec2-75-101-215-95.compute-1.amazonaws.com:/mnt/

5. On that instance, create an image of the current setup.
ec2-bundle-vol -k /mnt/pk-xxx.pem -c /mnt/cert-xxx.pem -u [user-id] -d /mnt

pk-xxx.pem = the PK file you uploaded in step 4
cert-xxx.pem = the CERT file you uploaded in step 4.
user-id = Your AWS User ID

In this step, you may wish to exclude some directories from the new image. Use the -e option followed by the ABSOLUTE path of the directories to be excluded. By default, some directories, like /mnt/, are excluded during image creation.

6. Create a bucket in Amazon S3. You can use s3fox to do this using a GUI, or use the command line tooling.

7. Upload the newly created image to your S3 bucket which was created in step 6
ec2-upload-bundle -b [your-s3-bucket] -m /mnt/image.manifest.xml -a [aws-access-key-id] -s [aws-secret-access-key-id]
aws-access-key-id = your AWS access key
aws-secret-access-key-id = your AWS secret access key.

8. Register the image. On your local machine, run
ec2-register [your-s3-bucket]/image.manifest.xml
The AMI ID will be displayed if the image was successfully registered. Say this was ami-af34d0c6

9. Test your image. Launch an instance of your new image. On your local machine run, e.g.
ec2-run-instances ami-af34d0c6 -k key1
or you could use the ElasticFox GUI to do the same thing

10. Connect to your new instance using SSH. This is similar to step 2.

That's it. You have just created your own AMI.

If you would like to make your AMI public, do the following:
1. ec2-modify-image-attribute --launch-permission -a all
2. Check the launch permissions of an AMI
ec2-describe-image-attribute -l
ami_id= the ID of the AMI

7 comments:

Ashok said...

For those who want to bundle an Image of Windows Instances, Please bear in mind that we have to run ec2-bundle-instances command (run through EC2 API Tool) from the local machine rather than in that instances machine.

I have found the above procedure in the ElasticFox manual on the page 20 and the stated text is:

"Bundling a Linux/UNIX instance requires the use of the AMI tools, and differs from Windows in that the bundling is performed from within the running EC2 instance. (As described above, you need to bundle Windows from outside the actual EC2 instance)."

And more over, to delete a created bundle in S3, I didn't find ec2-delete-bundle command in EC2-API Tools. For that I used S3Fox firefox plugin. And also, the author of this article (Azeez) has informed me that he used S3 command line tool.

Nir Simionovich said...

Hi Azeez,

Your post is great, helped me out big time when doing my first AMI on EC2.

Check out my post regarding it at

http://www.simionovich.com/?p=201

Thanks again,
Cheers

Afkham Azeez said...

Hi Nir,
Glad that you found it useful.

Regards
Azeez

Duncan said...

Hi Azeez,

Your post made creating my first AMI really clear, thanks a lot!

Duncan

samuelj_2009 said...

Hi,

I was able to configure one Centos instance with Apache and MySQL on it using Elasticfox. I installed other tools which are required for my basic application hosting purpose. I wanted to instantiate the same instance multiple times so that I do not have to install all these tools always when I want a new instance. I understand that ec2 tools must be used for generating multiple instances with the same oonfiguration if the instances are based on Unix/Linux. Could you please tell me how can I use these tools to do that.

Thanks,
Sam

joydeep said...

very clear - thanks! much better than amazon documentation

Anonymous said...

Thanks for an excellent blog post.

An alternative to installing ec2-register from the AWS api tools package is to use the AWS EC2 Management console: http://console.aws.amazon.com

Then click on EC2 manager. Then click on AMI Images on the left. Then click the Register AMI Image at the top of the page.