How I Provisioned Scalable AWS Infrastructure with Terraform and Load Balancer

Kaustav DeyKaustav Dey
3 min read

canonical_url: https://github.com/KaustavDey357/Terraform-AWS-EC2-Load-Balancer-Deployment

As I deepened my understanding of DevOps and cloud fundamentals, I wanted to get hands-on with provisioning infrastructure the right way: using Infrastructure as Code (IaC). In this post, I’ll walk through how I built a reusable Terraform project to provision an EC2 instance, attach a load balancer, configure security groups, and get it all running on AWS.

This project was part of my journey in creating modular and production-oriented DevOps blueprints.


🧰 Tools & Tech

  • Terraform for IaC

  • AWS EC2 for compute resources

  • Application Load Balancer (ALB) for routing

  • Security Groups for access control

  • Remote backend support with Terraform state files


πŸ“¦ Project Structure

I broke the configuration into reusable, modular Terraform components to make the codebase scalable and production-ready. Here's how the structure looked:

.
β”œβ”€β”€ main.tf           # Orchestrator: may call modules or glue everything together
β”œβ”€β”€ variables.tf      # All variable declarations with types and descriptions
β”œβ”€β”€ outputs.tf        # Output values (e.g., IPs, DNS names, ARNs)
β”œβ”€β”€ vpc.tf            # VPC, subnets, internet gateway, etc.
β”œβ”€β”€ ec2.tf            # EC2 instance(s), AMIs, key pairs, EBS volumes
β”œβ”€β”€ alb.tf            # Application Load Balancer, listeners, target groups
β”œβ”€β”€ security.tf       # Security groups, network ACLs, firewall rules
β”œβ”€β”€ README.md         # Project documentation
β”œβ”€β”€ .gitignore        # Files to exclude from Git (e.g., `.terraform`, `*.tfstate`)

Each module encapsulates a piece of the infrastructure (e.g., EC2, security group), keeping things clean and reusable.


πŸ—οΈ What It Provisions

When executed, the Terraform code provisions:

  • A public subnet in a selected region

  • An EC2 instance with user data for bootstrapping

  • A Security Group that allows inbound traffic on ports 22 and 80

  • An Application Load Balancer (ALB) that distributes HTTP traffic

  • Target group + listener configuration for the EC2 instance


πŸ§ͺ How to Use It

Clone the repo and run the following inside the root directory:

terraform init
terraform plan
terraform apply

Make sure your AWS credentials are set in your environment or shared credentials file.

Once applied, the EC2 instance and ALB will be up and running. You’ll get the public DNS of the load balancer in the Terraform output.


βœ… Outcome

With a single command, I spun up a complete production-grade architecture using Terraform. It’s scalable, reusable, and can easily be extended to include databases, autoscaling, and monitoring.


πŸ“Œ Key Learnings

  • Writing modular Terraform code is essential for maintainability

  • ALBs are ideal for HTTP/HTTPS workloads with flexible routing

  • Outputs and variables improve reusability and flexibility

  • Infrastructure automation saves time and reduces errors


🧠 Next Steps

I’m planning to:

  • Add support for private subnets and NAT gateways

  • Integrate with RDS or DynamoDB

  • Add Terraform Cloud remote backend

  • Extend this into a full production deployment pipeline


πŸ”— Resources


☎️ Let's Connect

If you're building something cloud-native or want help setting up secure AWS infrastructure, I’d love to chat.

0
Subscribe to my newsletter

Read articles from Kaustav Dey directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Kaustav Dey
Kaustav Dey