DEV Community

Cover image for How I Provisioned Scalable AWS Infrastructure with Terraform and Load Balancer
Kaustav Dey
Kaustav Dey

Posted on

2 1

How I Provisioned Scalable AWS Infrastructure with Terraform and Load Balancer

The Git Repo

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`)
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.

The architecture diagram is:

Image description

📌 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.

Google AI Education track image

Build Apps with Google AI Studio 🧱

This track will guide you through Google AI Studio's new "Build apps with Gemini" feature, where you can turn a simple text prompt into a fully functional, deployed web application in minutes.

Read more →

Top comments (0)

Heroku

Build AI apps faster with Heroku.

Heroku makes it easy to build with AI, without the complexity of managing your own AI services. Access leading AI models and build faster with Managed Inference and Agents, and extend your AI with MCP.

Get Started

👋 Kindness is contagious

Dive into this compelling post celebrated by our lively DEV Community. Developers everywhere are invited to share insights and uplift our collective expertise.

A simple “thank you” can make someone’s day—drop your appreciation in the comments!

On DEV, sharing expertise sparks growth and tightens our community bonds. Found this helpful? A quick nod to the author goes a long way.

Get started