DEV Community

Alao Abdul-zahir
Alao Abdul-zahir

Posted on • Edited on

πŸš€ Building a Multi-Environment, Auto-Scaling Web Platform with Terraform

Managing cloud infrastructure by hand can be messy, especially when juggling multiple environments like dev, staging, and prod. That’s where Terraform shines – it lets you define your infrastructure as code, making your deployments consistent, repeatable, and version-controlled.

In this post, I’ll walk you through building a production-ready web application infrastructure on AWS using Terraform. We’ll cover:

  • πŸ”Ή Multi-environment setup (dev & prod)
  • πŸ”Ή VPC with public/private subnets
  • πŸ”Ή Auto Scaling Group with EC2 instances
  • πŸ”Ή RDS database layer
  • πŸ”Ή Secure backend state management
  • πŸ”Ή Secrets scanning with git-secrets

πŸ—οΈ Project Overview

We’re building a simple yet scalable platform:

Internet
   ↓
Application Load Balancer (ALB)
   ↓
Auto Scaling Group (EC2 Instances)
   ↓
RDS Database (MySQL)
Enter fullscreen mode Exit fullscreen mode

This setup ensures high availability, cost optimization, and easy environment promotion (from dev to prod).


πŸ“‚ Project Structure

We organize Terraform code using modules for reusability:

terraform-web-platform/
β”œβ”€β”€ environments/
β”‚   β”œβ”€β”€ dev/
β”‚   β”‚   β”œβ”€β”€ main.tf
β”‚   β”‚   └── terraform.tfvars
β”‚   └── prod/
β”‚       β”œβ”€β”€ main.tf
β”‚       └── terraform.tfvars
β”œβ”€β”€ modules/
β”‚   β”œβ”€β”€ networking/
β”‚   β”œβ”€β”€ compute/
β”‚   β”œβ”€β”€ database/
β”‚   └── monitoring/
└── README.md
Enter fullscreen mode Exit fullscreen mode

πŸ”‘ Key Features

βœ… Multi-Environment Setup – Each environment has its own state and variables.

βœ… Terraform Modules – Networking, compute, database, and monitoring are modularized.

βœ… Secure State Management – Remote state stored in S3 with DynamoDB locking.

βœ… Auto Scaling – EC2 instances scale based on CPU utilization.

βœ… Monitoring – CloudWatch metrics and alarms for visibility.


πŸ› οΈ Setup Instructions

1. Clone the Repo

git clone https://github.com/pojava/terraform-web-platform.git
cd terraform-web-platform
Enter fullscreen mode Exit fullscreen mode

2. Configure AWS Credentials

aws configure
Enter fullscreen mode Exit fullscreen mode

Provide your AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, region, and output format.


3. Initialize and Apply Terraform

cd environments/dev
terraform init
terraform plan
terraform apply
Enter fullscreen mode Exit fullscreen mode

To deploy to production:

cd ../prod
terraform init
terraform plan
terraform apply
Enter fullscreen mode Exit fullscreen mode

4. Check the Load Balancer

Once deployed, Terraform outputs the ALB DNS. Visit it in your browser to verify your app is live.


πŸ”’ Security Practices

  • No Hardcoded Secrets: All sensitive values are managed through environment variables or AWS Secrets Manager.
  • git-secrets Installed: Prevents accidentally committing API keys.
  • .gitignore Applied: Ignores Terraform state and provider files.

🧹 Cleaning Up

When you’re done testing, destroy resources to avoid costs:

terraform destroy
Enter fullscreen mode Exit fullscreen mode

πŸ“ Lessons Learned

  • Using Terraform modules keeps your code DRY and maintainable.
  • Remote state locking prevents accidental overwrites during team deployments.
  • AWS Auto Scaling ensures your application stays resilient without overspending.

πŸ”— Useful Resources


πŸš€ Final Thoughts

This project shows how to go from zero to a production-ready, auto-scaling infrastructure using Terraform. By structuring code with modules and managing state securely, scaling your app becomes a breeze.

If you found this useful, ⭐️ the repo and drop a comment on Dev.to!

πŸ”— GitHub Repo

Top comments (0)