How shekh1995/3-tier-architecture works
Purpose and Entry Points
This is a Terraform root module that provisions an AWS three-tier environment: public ALB, private autoscaled EC2 web tier, and private MySQL RDS database. Terraform execution starts at main.tf, with provider/version setup in providers.tf and versions.tf; root inputs and exported deployment values are in variables.tf, terraform.tfvars, and outputs.tf.
Root Composition
main.tf composes three local modules in dependency order: networking supplies VPC/subnets/security groups; database consumes database networking; autoscaling consumes public/private subnets, security groups, and database connection values. Module contracts are defined through modules/*/variables.tf and modules/*/outputs.tf, keeping resource implementation behind module boundaries.
Paths: main.tf, modules/networking/outputs.tf, modules/database/outputs.tf.
Network Foundation
The networking module creates a 10.0.0.0/16 VPC across us-west-2a/b/c, with public, private application, and database subnet tiers. Public routes use an Internet Gateway; private application egress goes through one NAT Gateway/EIP. It also establishes the RDS subnet group. This module wraps the external terraform-aws-modules/vpc/aws module.
Paths: modules/networking/main.tf, modules/networking/variables.tf, modules/networking/outputs.tf.
Network Security Boundaries
Three security groups establish tiered ingress: Internet traffic reaches the ALB only on HTTP/80; the ALB reaches web instances on 8080; web instances reach MySQL on 3306. Web SSH access is constrained to the VPC CIDR. Security groups are implemented through the external terraform-in-action/sg/aws module.
Paths: modules/networking/main.tf, modules/networking/outputs.tf.
Database Tier
The database module generates a 16-character password and provisions a non-public MySQL 8.0 RDS instance (db.t2.micro, 10 GB), named pets, within the networking module’s DB subnet group and DB security group. Its sensitive password and database endpoint are passed to dependent consumers; the root exports the password as sensitive.
Paths: modules/database/main.tf, modules/database/outputs.tf, outputs.tf.
Application and Ingress Tier
The autoscaling module provisions an internet-facing Application Load Balancer in public subnets, with an HTTP/80 listener forwarding to instance targets on port 8080. It creates an EC2 launch template (AMI-specific, t2.micro) and Auto Scaling Group in private subnets, min 1/max 3, registered with the ALB target group. The ALB is implemented via external terraform-aws-modules/alb/aws.
Paths: modules/autoscaling/main.tf, modules/autoscaling/outputs.tf, modules/autoscaling/variables.tf.
Instance Bootstrap and AWS Access
Cloud-init renders modules/autoscaling/cloud_config.yaml into launch-template user data, likely injecting database-derived values at apply time. EC2 instances receive an IAM instance profile via terraform-in-action/iip/aws; its inline policy grants broad rds:* and logs:* access. Runtime dependencies are Terraform, AWS provider 3.63.0, CloudInit provider 2.2.0, and Random provider 3.1.0, locked in .terraform.lock.hcl.
Paths: modules/autoscaling/cloud_config.yaml, modules/autoscaling/main.tf, .terraform.lock.hcl.
Primary Flows
Inbound flow: Internet → public ALB:80 → private ASG instances:8080 → private RDS MySQL:3306. Provisioning flow: root inputs → networking outputs → database and autoscaling inputs; database password/endpoint feed cloud-init; ALB DNS and DB password are emitted by root outputs.
Main entry points: main.tf Root composition · networking/main.tf Networking module · autoscaling/main.tf Autoscaling module · outputs.tf Database endpoint and password
Components 17
Root module
- Deployment inputs
- Root composition · Composes networking, database, and autoscaling modules.
main.tf - Deployment outputs · Exports ALB DNS and sensitive database password.
outputs.tf
Network foundation
- Networking module · Builds VPC tiers, routing, subnet group, and security boundaries.
modules/networking/main.tf - VPC and subnet tiers · Public, private application, and database subnets span three AZs.
- Internet gateway and NAT · Internet serves public ingress; NAT provides private app egress.
- Tier security groups · Allow 80 to ALB, 8080 to web, and 3306 to MySQL.
modules/networking/main.tf - RDS subnet group
Application tier
- Autoscaling module · Creates public ingress and private EC2 capacity.
modules/autoscaling/main.tf - Public Application Load Balancer · HTTP listener on port 80 forwards to application targets.
- Launch template and cloud-init · Renders database-derived values into instance user data.
modules/autoscaling/cloud_config.yaml - Private EC2 Auto Scaling Group · Runs 1–3 web instances listening on port 8080.
- EC2 instance profile · Grants instances RDS and CloudWatch Logs permissions.
modules/autoscaling/main.tf
Database tier
- Database module · Creates MySQL and its generated credentials.
modules/database/main.tf - Private MySQL RDS · MySQL 8.0 database named pets.
- Database endpoint and password · Sensitive connection values passed to application bootstrap.
modules/database/outputs.tf
Other components
- Internet clients
Connections 22
- Deployment inputs → Root composition: configure
- Root composition → Networking module: provisions first
- Networking module → VPC and subnet tiers: creates
- VPC and subnet tiers → Internet gateway and NAT: public and private routes
- Networking module → Tier security groups: creates
- Networking module → RDS subnet group: creates
- Root composition → Database module: passes DB network
- RDS subnet group → Private MySQL RDS: places
- Tier security groups → Private MySQL RDS: MySQL ingress
- Database module → Private MySQL RDS: provisions
- Private MySQL RDS → Database endpoint and password: endpoint and password
- Root composition → Autoscaling module: passes subnets and security groups
- Autoscaling module → Public Application Load Balancer: provisions
- Autoscaling module → Launch template and cloud-init: renders
- Launch template and cloud-init → Private EC2 Auto Scaling Group: defines instances
- EC2 instance profile → Private EC2 Auto Scaling Group: attached to instances
- Database endpoint and password → Launch template and cloud-init: injects connection values
- Internet clients → Public Application Load Balancer: HTTP :80
- Public Application Load Balancer → Private EC2 Auto Scaling Group: targets :8080
- Private EC2 Auto Scaling Group → Private MySQL RDS: MySQL :3306
- Database endpoint and password → Deployment outputs: sensitive password
- Public Application Load Balancer → Deployment outputs: DNS name