How to deploy Ruby on Rails Application on Amazon EC2

Hello guys,

I am using Ubuntu 10.04, Rails 2.3.8 and Ruby 1.8.7. I want to deploy my rails application on Amazon EC2. But before that we have to create instance on Amazon and configure it. So I will show you how to do this.

First of all I am not using ec2onrails gem because it isn’t maintained regularly and supports old AMI . Then I assume that you are somewhat familiar with Amazon terms and you have account there. If not then please do it first.

After creating account and undestanding Amazon terms you need to have following things which you can find from Security Credentials tab from Here.

1) Amazon Account Number
2) Access Key ID
3) Secret Acsess Key
4) Amazon x.509 Certificate
5) Private Key Certificate

Now download developer tools from Here and then extract it and put it in some folder.

I put certificates and developer tools in /home/pulkit/cert folder.

now install Java if you don’t have.

So after downloading all necessary things, let’s start for setup.

# Set Java Path
1) export JAVA_HOME=/usr/lib/jvm/java-6-openjdk/
2) export PATH=$PATH:$JAVA_HOME/bin

# Set Private Key
3) export EC2_PRIVATE_KEY=/home/pulkit/cert/pk-PUL4MOWVQI4IOOKRV3BRJC2MHXJ2KKIO.pem

# Set Certificate
4) export EC2_CERT=/home/pulkit/cert/cert-PUL4MOWVQI4IOOKRV3BRJC2MHXJ2KKIO.pem

# Set api-tools
5) export EC2_HOME=/home/pulkit/cert/ec2-api-tools-1.3-62308
6) export PATH=$EC2_HOME/bin:$PATH

# add keypair to Amazon
# Save it in /home/pulkit/.ssh folder with name “id_rsa-rails-keypair”
7) ec2-add-keypair rails-keypair

# create instance by choosing AMI
# There are lots of AMI available in the market. Choose it as per your requirement. To see list of AMI and it’s details, visit Here.
8 ) ec2-run-instances ami-66e2130f -k rails-keypair

# See details of instance
# change i-9f57h52f3 with your instance id. You will get it when you will run above command.
9) ec2-describe-instances i-9f57h52f3

# Allow Port 22 and 80
# Unfortunately If you will get error
# “Client.InvalidPermission.Duplicate: The permission ’′ has already been authorized on the specified group” for ec2-authorize default -p 22
# OR
# “Client.InvalidPermission.Duplicate: The permission ’′ has already been authorized on the specified group” for ec2-authorize default -p 80
# it means that your ports are already allowed. You can move to next steps.
10) ec2-authorize default -p 22
11) ec2-authorize default -p 80

# If you are using git repository then copy your key) to Amazon Instance.
# As per your instance, username can be different(root or ubuntu). Make sure that you set name as “authorized_keys2” not “authorized_keys”. Because “authorized_keys” is already there.
12) scp -i ~/.ssh/id_rsa-rails-keypair ~/.ssh/

# login to Amazon Instance using SSH command.
13) ssh -i /home/pulkit/.ssh/id_rsa-rails-keypair

Now you should be in Amazon Instance. Install all necessary packages and gems as per your requirements.

In next post I will show you how to deploy rails application using capistrano on Amazon Instance.

Thanks & Regards,
Pulkit J. Pancholi

Reference :