I have my Ruby on Rails application running on EC2.
EC2 is a web service that helps in elastic computation across the web. It is elastic because the computation is very resizable. When compared with Plain Hosting it provides the following advantages:
- Unlike traditional hosting services EC2 provides resources that can be used unlimitedly,
- EC2 provides full control on the resource where in the entire resource configurations can be manipulated according to the users requirement and
- EC2’s service doesn’t require users to pay a fixed, up-front fee irrespective of their actual computing power used but instead the pay is very dynamic and the computed based on the usage of the resource.
The usage of the EC2 image becomes easier if ruby on rails application is to be hosted. As rails provide gems for additional utilities, it provides a gem called as “ec2onrails” that makes the hosting even simpler.
The following are the steps in using the EC2 image:
-
Start the instance of the image – The instance can be started as follows:
-
Signup for Amazon EC2 and S3 to acquire the necessary authentication credentials – The necessary authentication credentials are the privacy encoded mail security X509 certificate, privacy encoded mail security private key file and the account ID.
-
Installing the EC2 Tools – This basically involves no installation as such because all what is required is to get the tools downloaded from the EC2 site, unzip it into a folder. The unzipped files are the pre-installed files, hence to make the tools visible, an environment variable – EC2_HOME is set with the path to the EC2 tools folder. Apart from this we need to tell the tools for whom it is going to be used for by setting two more environment variables such as EC2_PRIVATE_KEY and EC2_CERT containing the paths of the private key file and X509 certificate.
-
Start running the instance – All what we are left now is with running the instance. In order to run the instance we need to do the following:
-
Find a suitable AMI(Amazon Machine Image) – This happens to be the crucial step. Why I say that this is a crucial one is because on executing the command: ec2-describe-images –o self –o amazon, it will list all the available images. We need to pick the most stable image that will suffice our purpose. This is where I initially went wrong. What I did was, I chose an image without reading the name of the image file, which happened to be a very unstable one and hence had to install all the required software manually which had dependencies issue and as a result of this I had to spend several additional hours in the setup process. But realized pretty early that I was in the wrong track and then chose the right one. So how do we choose the right image? In the listing we need to look for developer-image which is public and available and make a note of the ami ID(something like ami-xxxxxxxx).
-
Generating the key pair – The privacy enhanced mail security files is used for all the interactions with the image but each and every time we interact we need to use then in our command with various options. This can be avoided by generating key pair out of the two security files and use this key pair for all the usage. In order to generate the key pair execute the command: ec2-add-keypair <name>. The name can be anything and this list a key pair which needs to be stored in a file with the name of our choice.
-
Run the instance – On executing: “ec2-run-instance ami-xxxxxxxx key_pair_filename”, the instance starts running. But it takes long time to start. This is because Amazon EC2 moves the images around the network before they can be launched. For big images and/or congested networks, this takes several minutes. To improve performance, images can be cached. As you launch your images more frequently, it becomes less noticeable.
-
Authorize Network Access o the instance – The instance uses port 80 and 22 which can be authorized by executing: “ec2-authorize default -p 22” and “ec2-authorize default -p 80”. If your application requires nay other port for access, it can be done the same way.
-
Fetch the Public key – The Capistrano deployment requires the public key of the server which can be fetched by executing: “cap ec2onrails:get_public_key_from_server”
-
Finally deploy the application with Capistrano – Firstly setup the ec2onrails Capistrano deployment by saying, cap ec2onrails:setup and then the first deployment can be done by executing, cap deploy:cold and for the later successive deployments just use, cap deploy. Apart from this the Capistrano with ec2onrails provides cap ec2onrails:db:archive and cap ec2onrails:db:restore. They can be explicitly executed for archiving and restoring the data for the database. But on the cold deploy execution the ec2 instance starts the incremental archiving of the data using the predefined cron jobs.
We now have our application hosted on EC2. Yes, it is that easy.
The only question that one would have in his/her mind now is, what will happen if my instance or the ec2 server terminates? Once the server terminates, the instance is lost and can never be got back. This is one feature that ec2 lacks. But it provides a solution to handle such situation.
There is something called as bundling of the image. On bundling the image, the instance configuration and the application is uploaded to the S3 which can then be register to another instance and the application can be in form again. This can be achieved as follows:
Prepare for bundling – The server requires the two privacy enhanced mail security files. This can be got by using scp command.
Example - scp pk-xxyyzz.pem cert-xxyyzz.pem root@ec2-xx-xxx-xx-xxx.compute-1.amazonaws.com:/mnt -i keypair_file_name
Bundle the current image – From the ec2 instance bundle the image.
Example - ec2-bundle-vol -d /mnt -k /mnt/pk-xxyyzz.pem -c /mnt/cert-xxyyzz.pem -u account_number -r i386
Upload the AMI – The AMI is then uploaded to S3.
Example - ec2-upload-bundle -b bucket_name -m /mnt/image.manifest.xml -a access_key_id -s secret_access_key
Register the Uploaded image – The image needs to be register from the local system and can be done as: ec2-register bucket_name/image.manifest.xml
This gives an ami ID with which the instance can be run.
EC2 - a quick and cost effective way of hosting Web Applications.