Wednesday, April 11, 2012

Creating Amazon Linux AMI

Sponsored Links
We cannot say Amazon ec2 instance wont go down or data wont be lost. Its always better to hve backups. But a data backup is not a easy to restore option. So it is always better to make a image of your amazon ec2 instance and keep it somewhere, for example in amazon simple storage or s3. In this post we will discuss how to create image or full backup of an amazon ec2 instance, how to upload the amazon ec2 instace image or AMI to amazon s3 bucket, how to register the AMI with ec2 account and how to create a amazon ec2 instance based on the created AMI. I'm sure all those things we'll discuss here will be possible to do with GUI but some of them are possible with the Mozilla addon ElasticFox. But we will do everything from the command line.



Requirements:
Private key and x.509 certificate can be get from the security credentials link in your account area of aws account. You have to upload this keys to the instance which you want to make the image of.

Private Key File:  pk-PRIVATEKEY.pem
X.509 Certificate File:  cert-X509CERT.pem
AWS Account ID:  AWS-ACCT-ID

Creating the AMI:
The command syntax is as follows:
ec2-bundle-vol -k pk-PRIVATEKEY.pem -c cert-X509CERT.pem -u AWS-KEY-ID -d /Destination_directory -p BUNDLE-NAME.ami

We are creating the image in the directory /mnt/image. We have to copy the pk-PRIVATEKEY.pem and cert-X509CERT.pem to /mnt/image.
[root@hostname_of_instace ~]# cp pk-PRIVATEKEY.pem /mnt/image
[root@hostname_of_instace ~]# cp cert-X509CERT.pem /mnt/image
[root@hostname_of_instace ~]# cd /mnt/image

Now running the following command to create the AMI of the current instace:
[root@hostname_of_instace ~]# ec2-bundle-vol -k pk-ABCDEFGHIJKLMN.pem -c cert-ABCDEFGHIJKLMN.pem -u 03497 -d /mnt/image/ -p nagios.ami
Please specify a value for arch [i386]:
Copying / into the image file /mnt/image/nagios.ami...
Excluding:
         /dev/pts
         /proc/sys/fs/binfmt_misc
         /sys
         /proc
         /dev
         /media
         /mnt
         /proc
         /sys
         /mnt/image/nagios.ami
         /mnt/img-mnt
1+0 records in
1+0 records out
1048576 bytes (1.0 MB) copied, 0.001951 seconds, 537 MB/s
mke2fs 1.39 (29-May-2006)
Bundling image file...
Splitting /mnt/image/nagios.ami.tar.gz.enc...
Created nagios.ami.part.00
Created nagios.ami.part.01
Created nagios.ami.part.02
****OUTPUT TRUNCATED******
Created nagios.ami.part.65
Created nagios.ami.part.66
Created nagios.ami.part.67
Generating digests for each part...
Digests generated.
Unable to read instance meta-data for ancestor-ami-ids
Unable to read instance meta-data for product-codes
Creating bundle manifest...
ec2-bundle-vol complete.
[root@hostname_of_instace ~]#

After the image bundling is over when you list the contents of the directory /mnt/image you can see that the image is splitted into many parts. And one *.manifest.xml file also will be there.

[root@hostname_of_instace image]# ll
total 3233376
-rw-r--r-- 1 root root         916 Apr 10 03:48 cert-ABCDEFGHIJKLMN.pem
-rw-r--r-- 1 root root 10737418240 Apr 10 03:53 nagios.ami
-rw-r--r-- 1 root root       11746 Apr 10 04:08 nagios.ami.manifest.xml
-rw-r--r-- 1 root root    10485760 Apr 10 04:07 nagios.ami.part.00
-rw-r--r-- 1 root root    10485760 Apr 10 04:07 nagios.ami.part.01
-rw-r--r-- 1 root root    10485760 Apr 10 04:07 nagios.ami.part.02
************************OUTPUT TRUNCATED**************************
-rw-r--r-- 1 root root    10485760 Apr 10 04:07 nagios.ami.part.65
-rw-r--r-- 1 root root    10485760 Apr 10 04:07 nagios.ami.part.66
-rw-r--r-- 1 root root      706944 Apr 10 04:07 nagios.ami.part.67
-rw-r--r-- 1 root root         922 Apr 10 03:48 pk-ABCDEFGHIJKLMN.pem
[root@hostname_of_instace image]#

Uploading the AMI:
Now we have to upload this image bundle to amazon s3 simple storage bucket.
Syntax is as follows:
ec2-upload-bundle -b BUCKET-NAME -m BUNDLE-NAME.ami.manifest.xml -a ACCESS-KEY-ID -s SECRET-ACCESS-KEY

Now uploading the bundle to amazon s3 by executing the following command.
[root@hostname_of_instace ~]# ec2-upload-bundle -b bucket_name/images/ -m /mnt/image/nagios.ami.manifest.xml -a AKITSUA -s mxXxX060X95Ii5W
Uploading bundled image parts to the S3 bucket bucket_name ...
Uploaded nagios.ami.part.00
Uploaded nagios.ami.part.01
Uploaded nagios.ami.part.02
****OUTPUT TRUNCATED*******
Uploaded nagios.ami.part.65
Uploaded nagios.ami.part.66
Uploaded nagios.ami.part.67
Uploading manifest ...
Uploaded manifest.
Bundle upload completed.
[root@hostname_of_instace ~]#

Registering the AMI
Now we have the image uploaded to our amzon s3 bucket. But in order to create instances based on this image we have to register the AMI with our ec2 account. To register the AMI from command line,
[root@hostname_of_instace image]# ec2-register -K pk-ABCDEFGHIJKLMN.pem -C cert-ABCDEFGHIJKLMN.pem -n nagios bucket_name/images/nagios.ami.manifest.xml
IMAGE   ami-wwwwww
[root@hostname_of_instace image]#

This the the image name we are registered with.
ami-wwwwww

Creating new instance based on the AMI:
Now we can create new amazon ec2 instance based on this newly created image. We can do this from command line. Here I have scpecified which security group(basics) I'm using and what kind of access method(cdnkey) I'll use for the new instance.
[root@hostname_of_instace ~]# ec2-run-instances -K pk-ABCDEFGHIJKLMN.pem -C cert-ABCDEFGHIJKLMN.pem -g Basics -k cdnkey ami-wwwwww
RESERVATION     r-abc1231234567789    Basics
INSTANCE        i-vvvvvvv      ami-wwwwww                    pending cdnkey  0               m1.small        2012-04-10T10:41:54+0000  us-east-1a      aki-123eee ari-1234ff monitoring-disabled          instance-store
[root@hostname_of_instace ~]#


Recommended Reading

1. Host Your Web Site In The Cloud: Amazon Web Services Made Easy: Amazon EC2 Made Easy
2. Programming Amazon Web Services: S3, EC2, SQS, FPS, and SimpleDB
3. Middleware and Cloud Computing: Oracle on Amazon Web Services (AWS), Rackspace Cloud and RightScale (Volume 1)

Sponsored Links

No comments:

Post a Comment

Be nice. That's all.