Add an Application Load Balancer to Your Scalable AWS Infrastructure
In my [previous blog], we built a scalable AWS infrastructure using Terraform modules to provision a VPC, EC2 Auto Scaling Group, and S3 bucket. Now, let’s take it a step further by introducing an Application Load Balancer (ALB) to route incoming traffic to our EC2 instances.
This setup ensures high availability and better fault tolerance while keeping the codebase clean and modular.

Objective
- Add an Application Load Balancer (ALB) in front of Auto Scaling EC2 instances.
- Route HTTP traffic via ALB
- Use modular Terraform templates for maintainability
- Ensure security groups, user data, and health checks are configured correctly
Architecture Overview
We’ll reuse our previous modules and add one new module: ALB.
Traffic will now flow like this:

Folder Structure
Key Enhancements
- ALB Module with security group, listener, and target group.
- EC2 User Data to install and start the Apache web server
- Security Groups updated to allow HTTP traffic on port 80
- Autoscaling Attachment to bind EC2 ASG to ALB target group.
Terraform Apply
Once your `.tf` files and variables are updated, simply run:
terraform init
terraform plan -var-file=”terraform.tfvars”
terraform apply -var-file=”terraform.tfvars”
Test the Setup
After successful deployment, go to the **ALB DNS name** from Terraform output. You should see:
> *"Hello from Terraform EC2 behind ALB"*

If not, check:
- Security Group for EC2
- ALB health check path
- Auto Scaling group logs (EC2 console)
Troubleshooting (real issues faced)
Here are a few errors I encountered and how I fixed them:
– Unhealthy Target: EC2 instance had no web server → Added `user_data` to install Apache
– ALB 504 Error: Security group didn’t allow HTTP on port 80 → Updated ingress rules
– Invalid count error: Conditional resource creation with `count` on unknown values → Used `-target` to apply dependencies first
– Broken state: Manually destroyed infra earlier → Cleaned up via `terraform state rm` or recreated stack
For a full list, check the `TROUBLESHOOTING.md` in the GitHub repo.
Cleanup
To safely destroy the infrastructure:
terraform destroy -var-file="terraform.tfvars"
Make sure:
– You apply from the same environment directory
– No dependent services (e.g., S3 bucket with versioning) block deletion
Final Thoughts
This post demonstrated how to enhance a modular Terraform-based infrastructure by integrating an Application Load Balancer for production-like routing. As you scale your applications, modular design and traffic routing via ALB become essential.
Stay tuned — in the next post, we’ll explore how to:
- Add HTTPS support
- Register a custom domain using Route 53.
- Deploy your static or dynamic app via CI/CD.
Pingback: Terraform Modules: How to Monitor AWS Infra Using CloudWatch - AWS In KiloBytes