Saturday, 13 September 2008

Getting piece of the elastic cloud ( Amazon EC2 HowTo) --- Part - 1

Amazon Elastic Compute Cloud (Amazon EC2) is a web service that provides resizable compute capacity in the cloud. It is designed to make web-scale computing easier for developers. As for amazon...

"Amazon EC2's simple web service interface allows you to obtain and configure capacity with minimal friction. It provides you with complete control of your computing resources and lets you run on Amazon's proven computing environment. Amazon EC2 reduces the time required to obtain and boot new server instances to minutes, allowing you to quickly scale capacity, both up and down, as your computing requirements change. Amazon EC2 changes the economics of computing by allowing you to pay only for capacity that you actually use. Amazon EC2 provides developers the tools to build failure resilient applications and isolate themselves from common failure scenarios."

"http://aws.amazon.com/ec2"

Ok so thats EC2.. Lets start the exciting bit now.. Lets look at the things you need to create EC2,

* First of all you need to have an Amazon Web Services (AWS) account. Well the glitch is , this is not free. Amazon will charge you $0.10 per hour and there will be additional charges based on the usage. You can create account by clicking here. Also remember to download X.509 keys from Access Identifiers Page in your account. You need to create a new X.509 key and then you'll be able to download X.509 certificate and private key file.
* You need to have ElasticFox plugin for Mozilla Firefox. Then you'll be able to control EC2 instance easily using your browser. To install the plugin click here.
* Then you need to create a Amazon Machine Image. Amazon has a collection pre-build AMIs here. But if we use this, where's the fun ?? So anyway I'll tell how to create an AMIs and upload it to EC2 in good old way...
* Also you need AMI (link) tool package, which use to create AMIs and upload then to EC2 and EC2 command line tools bundle (link), which can use in generic tasks such as creating and controlling EC2 instances.

Alright I think thats the checklist.. So I by now I hope you have created the account and downloaded the key files. My advise is key all your key and file in one location, So i did the following

# mkdir -p ~/ec2/.keys # mv ~/Desktop/*.pem ~/ec2/.keys

Always keep you amazon downloads in,

#mkdir ~/ec2/downloads

Ok thats the easy part.. Lets start the fun part.. Creating an AMI...

* First of all we need create a "loopback file" to install Linux. Advantage of a loopback file is we can use it as a hard disk and we don't have to go through the hassle of installing an OS in to a separate drive. Best way to create loopback file in Linux is using dd command.

# cd ~/ec2/
# dd if=/dev/zero of=debianimage.fs count=1024 bs=1M

* Right now we have raw disk. Now we need to create a file system. We can use mke2fs command.

# mke2fs -F -j ~/ec2/debianimage.fs

* Now you have hard disk which can use to install a Linux image. Mount the new hard disk.

# mkdir /mnt/debianimage # mount -o loop ~/ec2/debianimage.fs /mnt/debianimage

* To install debian into this file system we can use debootstrap. This program can setup a basic debian Linux system any given file system. If its not in your system, you should install it first.

# apt-cache search debootstrap
# apt-get install debootstrap

# cd /mnt
# debootstrap --arch 1386 etch debianimage




If all went well you should see something like above. This will go for few mins depending on your connection and will give a installed successful notice. Now if you look inside the debianimage file you will full Linux file system.

* Now is the best time to copy the repository of your current system into the new system and chroot to the new system and mount the proc file system. The main reason to chroot into the system is to change root password, install new packages etc.

# cp /etc/apt/sources.list /mnt/debianimage/etc/apt/sources.list
# chroot /mnt/debianimage/
# mount -t proc none proc
# passwd

* This is the best time to update the repository and install new packages like ssh and apache.

# apt-get update
# apt-get install apache2
# apt-get install openssh-server

* Check whether your interfaces are set to take an IP from a DHCP.

# vi /etc/network/interfaces
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet dhcp

* Finally add the following two lines to your file system and exit from the image

# vi /etc/fstab
/dev/sda2 /mnt ext3 defaults 1 2
/dev/sda3 swap swap defaults 0 0

# exit
#umount /mnt/debianimage

Now we are almost there... Now we need to prepare the Linux image we created and upload it to EC2...

It's in part 2...

No comments: