How clouddevopshub/3-tier-architecture works
Purpose and entry points
This is a Terraform infrastructure repository that provisions an AWS three-tier deployment: public HTTP ingress, private autoscaled EC2 web/application instances, and a private MySQL RDS database. Root composition begins at main.tf; provider/version constraints are in providers.tf and versions.tf. Inputs are defined in variables.tf and supplied through terraform.tfvars; exported access values are in outputs.tf.
Root orchestration
main.tf wires three local modules—networking, database, and autoscaling—by passing IDs and subnet/security-group outputs between them. This creates Terraform’s dependency graph: networking first; database and ALB can follow once network resources exist; launch configuration depends on the database’s generated connection values.
Paths: main.tf, outputs.tf, modules/networking/outputs.tf.
Network foundation and isolation
The networking module defines the VPC topology: a 10.0.0.0/16 VPC with public, private application, and database subnet tiers distributed across us-west-2a/b/c. Public routing uses an Internet Gateway; private application egress uses one NAT Gateway/EIP; database subnets are assembled into an RDS subnet group.
Paths: modules/networking/main.tf, modules/networking/variables.tf, modules/networking/outputs.tf.
Security boundaries
Networking also creates three security-group boundaries using the external terraform-in-action/sg/aws module: the ALB accepts Internet HTTP on port 80; web instances accept ALB-originated traffic on 8080 (and VPC-internal SSH on 22); the database permits MySQL port 3306 from the web tier. RDS is explicitly non-public.
Paths: modules/networking/main.tf, modules/networking/outputs.tf.
Database tier
The database module provisions a MySQL 8.0 RDS instance (db.t2.micro, 10 GB) in the database subnet group with the DB security group. It generates a 16-character password via the Random provider and exposes needed database values to root/autoscaling as sensitive outputs. The planned configuration uses a pets database and admin username; final snapshots are skipped.
Paths: modules/database/main.tf, modules/database/outputs.tf, modules/database/variables.tf.
Web compute, bootstrap, and permissions
The autoscaling module renders cloud-init user data from cloud_config.yaml, then attaches it to an EC2 launch template (AMI and instance details are module inputs). EC2 instances run in private subnets under an Auto Scaling Group with min 1 and max 3 instances. An external IAM instance-profile module gives instances an EC2 role with the configured RDS and CloudWatch Logs permissions.
Paths: modules/autoscaling/main.tf, modules/autoscaling/cloud_config.yaml, modules/autoscaling/variables.tf.
Public request flow
An internet-facing AWS Application Load Balancer is placed in public subnets and exposed through its DNS output. Its HTTP listener on port 80 forwards to an instance target group on port 8080. The ASG registers launched instances into this target group, making the runtime path: client → ALB → private EC2 application → private MySQL RDS.
Paths: modules/autoscaling/main.tf, modules/autoscaling/outputs.tf, outputs.tf.
Runtime and external dependencies
Terraform uses HashiCorp AWS, Cloud-Init, and Random providers, locked in .terraform.lock.hcl. It consumes terraform-aws-modules/vpc/aws (v2.64.0), terraform-aws-modules/alb/aws (v5.16.0), plus terraform-in-action security-group and IAM-profile modules. AWS is the sole runtime platform; the README’s plan shows deployment in us-west-2.
Paths: providers.tf, versions.tf, .terraform.lock.hcl.
Main entry points: main.tf Root composition · networking/main.tf Tier security groups · autoscaling/main.tf Private web Auto Scaling Group · database/main.tf Private MySQL RDS
Components 18
Terraform root
- Root composition · Wires networking, database, and autoscaling modules.
main.tf - Inputs and exports
outputs.tf - AWS and Terraform providers · AWS, Cloud-Init, and Random providers are locked for deployment.
providers.tf
Network foundation
- Networking module
modules/networking/main.tf - VPC and tiered subnets · Public, application, and database tiers span three availability zones.
modules/networking/main.tf - Internet and NAT routing · Internet Gateway serves public ingress; one NAT Gateway provides private egress.
modules/networking/main.tf - Tier security groups · ALB HTTP, web 8080/SSH, and DB MySQL access are separated.
modules/networking/main.tf - RDS subnet group
modules/networking/main.tf
Database tier
- Database module
modules/database/main.tf - Generated DB password · A 16-character password is generated and exposed as sensitive output.
modules/database/main.tf - Private MySQL RDS · MySQL 8.0 pets database is non-public.
modules/database/main.tf
Ingress and compute
- Autoscaling module
modules/autoscaling/main.tf - Public application load balancer · Internet-facing HTTP listener forwards to port 8080 targets.
modules/autoscaling/main.tf - Web target group
modules/autoscaling/main.tf - Cloud-init bootstrap · Rendered into launch-template user data.
modules/autoscaling/cloud_config.yaml - EC2 launch template · Defines private application instance configuration.
modules/autoscaling/main.tf - EC2 IAM instance profile · Grants configured RDS and CloudWatch Logs permissions.
modules/autoscaling/main.tf - Private web Auto Scaling Group · Runs 1–3 application instances in private subnets.
modules/autoscaling/main.tf
Connections 22
- Root composition → Networking module: instantiates
- Root composition → Database module: passes network outputs
- Root composition → Autoscaling module: passes network and DB values
- AWS and Terraform providers → Root composition: configures
- Inputs and exports → Root composition: supplies and exports values
- Networking module → VPC and tiered subnets: creates
- VPC and tiered subnets → Internet and NAT routing: provides subnet tiers
- Networking module → Tier security groups: creates
- VPC and tiered subnets → RDS subnet group: database subnets
- RDS subnet group → Private MySQL RDS: places database
- Tier security groups → Private MySQL RDS: permits MySQL from web tier
- Database module → Generated DB password: generates
- Generated DB password → Private MySQL RDS: sets credentials
- Autoscaling module → Public application load balancer: creates
- Public application load balancer → Web target group: forwards HTTP :80
- Cloud-init bootstrap → EC2 launch template: renders user data
- EC2 IAM instance profile → EC2 launch template: attaches role
- EC2 launch template → Private web Auto Scaling Group: launches instances
- Private web Auto Scaling Group → Web target group: registers instances
- Tier security groups → Public application load balancer: allows Internet HTTP
- Tier security groups → Private web Auto Scaling Group: allows ALB traffic
- Private web Auto Scaling Group → Private MySQL RDS: connects MySQL :3306