![Using Terraform ELB & ASG in AWS Using Terraform ELB & ASG in AWS](https://askjoechan.com/wp-content/uploads/2022/12/cover-1024x289.png)
Using Terraform ELB & ASG in AWS
![](https://askjoechan.com/wp-content/uploads/2022/12/Untitled-Diagram2-1024x893.jpg)
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:
https://github.com/yourlifeuk/terra_project/tree/main/Project_5
![](https://askjoechan.com/wp-content/uploads/2022/12/Ter1.1-1024x727.png)
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.
![](https://askjoechan.com/wp-content/uploads/2022/12/ip-1.1-edited.png)
what actually happen
![](https://askjoechan.com/wp-content/uploads/2022/12/image.png)
- 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.
- Creating Launch configuration -> Auto scaling up the web server. AWS needs to know, which template it needs to use.
- 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.
![](https://askjoechan.com/wp-content/uploads/2022/12/ec1.1-1024x236.png)
![](https://askjoechan.com/wp-content/uploads/2022/12/ec-1.2-1024x394.png)
![](https://askjoechan.com/wp-content/uploads/2022/12/launch-config-1024x467.png)
![](https://askjoechan.com/wp-content/uploads/2022/12/auto-scalling-1024x468.png)
![](https://askjoechan.com/wp-content/uploads/2022/12/elb-1.1-1024x456.png)
![](https://askjoechan.com/wp-content/uploads/2022/12/elb-health-check-1024x490.png)
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.
![](https://askjoechan.com/wp-content/uploads/2022/12/delete-1.1-1-1024x336.png)
![](https://askjoechan.com/wp-content/uploads/2022/12/dele-1.2-1024x476.png)
![](https://askjoechan.com/wp-content/uploads/2022/12/delete-1.3-1-1024x467.png)