At Agira, Technology Simplified, Innovation Delivered, and Empowering Business is what we are passionate about. We always strive to build solutions that boost your productivity.

,

Ruby on Rails application in VPC environment – Setup walkthrough

  • By Saravana
  • October 17, 2016
  • 879 Views

Introduction

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.
doc.jpc
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 Ruby on Rails application in VPC environment that we setup then.)
Prerequisites

• VPC Environment
• AWS Ami-Id
• Pem key name

AWS Ami-Id

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"
image_id: "AMI-id",
min_count: 1,
max_count: 1,
key_name: "Pem kem name",
subnet_id: "vpc private subnet id",
security_group_ids: "Array of security group ids",
monitoring: {
enabled: true/false
}
})

 
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)
elbClient.create_load_balancer(
{
: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.

elbClient.register_instances_with_load_balancer(
{
load_balancer_name: load_balancer_name,
instances: [
{
instance_id: instance_id,
}],
})

 
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.

Saravana

An enthusiastic Tech Lead with 7 plus years of experience in Web development arena. Owns legitimate experience in Ruby, Ruby On Rails, AngularJs, DevOps. Golang, Another add on, This young tech freak never miss a chance to get his hands on planting and Gardening even in his busy weekends.