DEV Community

Samuel Udeh
Samuel Udeh

Posted on • Edited on

πŸš€ Building a Multi-Environment Web Application Infrastructure with Auto-Scaling on AWS Using Terraform

Introduction

In today’s cloud-native world, applications need to scale dynamically, stay cost-efficient, and remain reproducible across environments (dev, staging, prod). To achieve this, I built a Multi-Environment Web Application Infrastructure with Auto-Scaling using Terraform on AWS.

This project was a hands-on demonstration of Infrastructure as Code (IaC), scalable architecture design, and DevOps automation.

🎯 Project Goals

  • Provision AWS infrastructure for multiple environments (dev, prod).

  • Automate resource creation using Terraform modules.

  • Deploy a simple web app served by Nginx.

  • Configure Application Load Balancer (ALB) + Auto Scaling Group (ASG).

  • Ensure traffic is distributed and scaling happens automatically under load.

πŸ—οΈ Architecture

Here’s the high-level architecture I built:

  • VPC with public and private subnets across multiple AZs.

  • Internet Gateway and NAT Gateway for proper routing.

  • Application Load Balancer (ALB) in public subnets.

  • Auto Scaling Group (ASG) in private subnets.

  • EC2 instances bootstrapped with user_data to install Nginx and serve a web page.

  • CloudWatch Alarms to scale out/in based on CPU utilization.

πŸ“Œ When traffic increases β†’ ASG scales out by launching new EC2 instances.
πŸ“Œ When traffic decreases β†’ ASG scales in, saving costs.

Prerequisites

Before you start, ensure you have:

  • AWS account
  • Terraform installed
  • AWS CLI installed

Authenticate Terraform With AWS

To authenticate Terraform with AWS, you can follow these steps:

Configure AWS CLI

Open your terminal, and run the following command to configure the AWS CLI:

aws configure
Enter fullscreen mode Exit fullscreen mode

Enter the following details when prompted:

  • AWS Access Key ID: Your access key ID.
  • AWS Secret Access Key: Your secret access key.
  • Default region name: The AWS region you want to use (e.g., us-east-1).

Project Structure:

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

πŸ› οΈ Implementation with Terraform

I structured my Terraform project into reusable modules:

  • Network/ β†’ VPC, Subnets, Routing, NAT

  • Security/ β†’ Security Groups, Ingress/Egress rules

  • Compute/ β†’ ASG, Launch Templates, User Data

  • Loadbalancer/ β†’ ALB + Target Groups + Listeners

A snippet of my user_data (runs on EC2 launch):

!/bin/bash

apt-get update -y
apt-get install -y nginx
echo "Hello from ${var.environment} - ${var.project}" > /var/www/html/index.html
systemctl enable nginx && systemctl start nginx
Enter fullscreen mode Exit fullscreen mode

This ensures every new EC2 instance in the ASG automatically serves a simple environment-specific message.

πŸ” Testing the Setup

After applying Terraform (terraform apply), I tested the deployment:

Checked the ALB DNS Name

  • Terraform outputs alb_dns_name:
https://web-platform-dev-alb-495228054.eu-north-1.elb.amazonaws.com/
Enter fullscreen mode Exit fullscreen mode

Opening this URL displayed my Nginx welcome message. βœ…

Load Testing For Auto Scaling

I validated the Auto Scaling setup by simulating high traffic using Apache Bench. The test successfully triggered CloudWatch alarms, which in turn scaled out the Auto Scaling Group by adding EC2 instances. When traffic normalized, scale-in policies removed extra instances, ensuring efficiency and cost optimization

ab -n 1000 -c 100 http://<your-alb-dns-name>/
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • -n 1000 β†’ Total number of requests = 1000

  • -c 100 β†’ Concurrency level = 100 requests at the same time

  • http:/// β†’ Target URL (the ALB’s DNS name)

At high load, CloudWatch alarms triggered scale-out.

Observed Scaling

  • ASG increased desired capacity.

  • New EC2 instances launched and registered with the ALB.

  • When traffic reduced, ASG scaled back in automatically.

πŸ“Š Results

βœ… Highly Available: Load Balancer ensured zero downtime during scaling.

βœ… Scalable: ASG responded dynamically to CPU load.

βœ… Environment-Aware: Different environments (dev, staging, prod) could be deployed with the same modules.

βœ… Cost-Efficient: Unused capacity scaled in automatically.

πŸ’‘ Key Learnings

  • Modular Terraform makes infrastructure reusable and easy to extend.

  • CloudWatch Alarms + ASG are powerful for real-world auto-healing & scaling.

  • IaC ensures consistency across environments and reduces manual errors.

  • Testing Auto Scaling with stress/load tools validates that your setup actually works.

πŸ“Œ Next Steps

  • Add HTTPS (TLS/SSL) with ACM + ALB.

  • Implement Terraform remote backend with state locking (e.g., S3 + DynamoDB or HCP Terraform).

  • Integrate CI/CD pipeline for infrastructure deployments.

πŸŽ‰ Conclusion

This project gave me a solid foundation in building scalable, production-ready infrastructure on AWS using Terraform. It reflects real-world patterns used in modern cloud deployments and can be extended to host any kind of application.

πŸ‘‰ Check out the repo here: https://github.com/SamuelUdeh/terraform-aws-multi-env

Top comments (0)