Inspiration for this repo was a couple of YouTube tutorials from Java Home Cloud where it was demonstrated how to create AWS Application Load Balancers and Auto Scaling groups.
This is not a beginner's guide to learning Terraform or AWS. It is assumed the reader is already familier with these technologies and would instead like to find related topics like ASG and LB in one place for reference purpose.
I wanted to take a step further and automate the video instructions. The idea is that setting up of the infrastructure should be automated without human intervention.
This includes using Terraform Registry modules for creating:
- Virtual Private Cloud (VPC) that host the entire infrastructure in this repo.
- VPC is subdivided in four subnets; two are public and two are private
- Application Load Balancer (ALB) in public subnets
- Network Load Balancer (NLB) in public subnets
- Code has been written but commented out for the following reasons:
- NLB target groups appear to delete ALB target groups
- For NLB to work alongside ALB, ASG security will need to be open from the entire world e.g. 0.0.0.0/0. And so, I did not want to open security group so widely
- Code has been written but commented out for the following reasons:
- Classic ELB (ELB) in public subnets
- Auto Scaling Group (ASG) in private subnets and are only reachable via LB on port 80
- Auto Scaling policies for scaling up / down instances based on CloudWatch CPU alarms raised
- Security groups that only allow traffic on port 80 to EC2 instances via LB
This is how infrastrucutre is setup with Terraform:
terraform init
terraform apply -auto-approve
- Classic ELB and ALB were created above. Use DNS records for each of the LB for testing.
- Refreshing the pages should return different result depending which ASG the LB routed traffic to.
This is how the infrastructure created above may be destroyed if it is no longer needed
terraform destroy -auto-approve
- cloud-init Documentation
- Terraform Registry
- KnowledgeIndia - Auto Scaling Group, Launch Configuration, Scale-out & Scale-in Policies
- KnowledgeIndia - Application Load Balancer - Setup & DEMO - Differences from Classic ELB
- KnowledgeIndia - Network Load Balancer DEMO
- KnowledgeIndia - Classic ELB DEMO - Setup with Private instances
- A visual representation of ALB and Terraform code example