• jcnetworking
Using Terraform ELB & ASG in AWS

Using Terraform ELB & ASG in AWS

Why do we need Load Balance + Auto Scaling

With a Star-up business, we didn’t expect that many peoples to visit the website at the begining, Also we are afraid the web server can’t handle it with the peak season. You don’t want your web server can’t take any workload due to a busy time. So you will add as much as resources you think, in order to take traffic. But it will cost you money when the services are not in use.

In AWS, we can use route 53 to weight route the traffic to your “CloudFront”, “S3 Website” or “Load Balance”. Now we will go through how to build a Elastic Load Balance “ELB” and Auto Scaling Group “ASG” in order to take high traffic when it is peak. And also We want it reduce the cost, when it is not busy. We will use Terraform to help us, to manage the infrasture more efficiency way. If we want to do it again, we want the same result that you you will get.

Terraform code as below:


Terraform Plan & Terraform Apply

Type Terraform apply

We will build following:

  • VPC with 2 subnets
  • Internet gateway + assicoated with routing tables & subnets
  • Security Group for instance & Load Balancer
  • Load Balancer + Auto Scaling Group ( Including lanuch configuration)

In output, it will export Public DNS to you at the end. Hit copy, paste & Enter.

Nginx template is now running.

Public DNS

what actually happen

Launch script
  1. Creating ELB -> Which has its own public DNS. When we hit the public DNS, it will forward the traffic to required instance. Listening port here we set it port 80.
  2. Creating Launch configuration -> Auto scaling up the web server. AWS needs to know, which template it needs to use.
  3. Creating Auto Scaling Group -> it is where you set what is your desired numbers of instance, max or min. It will base on the metrics to scale up and down.
Launch Configuration
Auto Scaling Group
Public DNS

Auto Scaling Group how it works

As in my ASG, I have set my desired instances are “2”. So Terraform created 2 instances for me at the begining. Now I will terminate them.

Health check will notice that, instances are unreachable. Now is “0” Which is less than the desired number “2” now. ASG will process base on the launch configuration to creates instance to reach desired state.

After terminated instances
Created Instances

Leave a Reply

Your email address will not be published. Required fields are marked *