In the previous article we have seen “How to setup VPC environment using Ruby”. In this article we are going to setup a Ruby on Rails application in VPC environment.
When automated, the prerequisite parameters have to be passed on. In a manual or normal setup environment, these would not have been necessary since they would be given during the setting up of the instance inside the VPC environment. (We shall continue with the VPC environment that we set up in the previous article for the example to follow in the current one, and we shall set up the RoR app in the VPC environment that we setup then.)
• VPC Environment
• AWS Ami-Id
• Pem key name
Let’s assume we have an AWS EC2 image (of the client that we initialised in the previous article) which is already having Ruby, Ruby on Rails and Database pre-installed, so that we can launch multiple instances using the same AMI.
Why do we need an ELB?
All the instances under private subnet will be protected from the outside word. We cannot make a direct communication outside the VPC environment. So using ELB we will make the HTTP/HTTPS requests from outside of the VPC environment.
Following are the steps involved:
1. Launching instances
2. Launching Elastic Load Balancer
3. Attach instances to ELB
Step : 1 – Launching instances
To launch an instance we need to pass the below required parameters:
ec2client = Aws::EC2::Client.new(region: ”us-west-2”)
instance = @ec2client.run_instances(
instance_type: "Instance type"
key_name: "Pem kem name",
subnet_id: "vpc private subnet id",
security_group_ids: "Array of security group ids",
Mentioned are the mandatory fields. There are several other optional fields which have not been mentioned here.
We need to interate the script for the number of API Servers that need to be launched.
Step : 2 – Launching Elastic Load Balancer
To create ELB we need to pass the ELB name, Array of listeners, Array of public subnet ids and Security group id. For every VPC, we need a new load balancer. We cannot attach instances that are created in another VPC.
elbClient = Aws::ElasticLoadBalancing::Client.new(region: @region)
:load_balancer_name => load_balancer_name,
:listeners => [
:protocol => "HTTP/HTTPS",
:load_balancer_port => 80/443,
:instance_protocol => "HTTP",
:instance_port => 80
:subnets => subnet_ids,
:security_groups => load_balancer_sg_ids
Step : 3 – Attach instances to ELB
After completion of steps 1 & 2, the newly created instances need to be attached to the ELB, so that the outside world can access the application that is deployed in the newly created instances.
Launching instances and ELB are completed. This successfully deploys a Ruby on Rails application in VPC environment. Now we can now run the Ruby on Rails application inside these instances and can access the application using ELB from the outside world. Thus, by following the default/necessary steps for a RoR app, we can successfully run the application within this environment.