# clouddevopshub/3-tier-architecture architecture

> Architecture diagram of the GitHub repository clouddevopshub/3-tier-architecture, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-31). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/clouddevopshub/3-tier-architecture
- Repository: https://github.com/clouddevopshub/3-tier-architecture

## Overview

#### 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`](https://github.com/clouddevopshub/3-tier-architecture/blob/main/main.tf) (Root composition), [`modules/networking/main.tf`](https://github.com/clouddevopshub/3-tier-architecture/blob/main/modules/networking/main.tf) (Tier security groups), [`modules/autoscaling/main.tf`](https://github.com/clouddevopshub/3-tier-architecture/blob/main/modules/autoscaling/main.tf) (Private web Auto Scaling Group), [`modules/database/main.tf`](https://github.com/clouddevopshub/3-tier-architecture/blob/main/modules/database/main.tf) (Private MySQL RDS)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_root["Terraform root"]
  node_terraform_root["Root composition<br/>Terraform orchestration<br/>[main.tf]"]
  node_inputs_outputs["Inputs and exports<br/>Terraform interface<br/>[outputs.tf]"]
  node_providers["AWS and Terraform providers<br/>provider constraints<br/>[providers.tf]"]
end

subgraph group_network["Network foundation"]
  node_network_module["Networking module<br/>Terraform module<br/>[main.tf]"]
  node_vpc_subnets["VPC and tiered subnets<br/>AWS network<br/>[main.tf]"]
  node_public_private_routing["Internet and NAT routing<br/>AWS routing<br/>[main.tf]"]
  node_security_groups["Tier security groups<br/>security boundary<br/>[main.tf]"]
  node_db_subnet_group["RDS subnet group<br/>AWS RDS network<br/>[main.tf]"]
end

subgraph group_database["Database tier"]
  node_database_module["Database module<br/>Terraform module<br/>[main.tf]"]
  node_db_password["Generated DB password<br/>Random secret<br/>[main.tf]"]
  node_mysql_rds[("Private MySQL RDS<br/>AWS RDS instance<br/>[main.tf]")]
end

subgraph group_compute["Ingress and compute"]
  node_autoscaling_module["Autoscaling module<br/>Terraform module<br/>[main.tf]"]
  node_application_load_balancer["Public application load balancer<br/>AWS ALB<br/>[main.tf]"]
  node_target_group["Web target group<br/>AWS target group<br/>[main.tf]"]
  node_cloud_init["Cloud-init bootstrap<br/>instance user data<br/>[cloud_config.yaml]"]
  node_launch_template["EC2 launch template<br/>AWS EC2 template<br/>[main.tf]"]
  node_instance_profile["EC2 IAM instance profile<br/>AWS IAM role<br/>[main.tf]"]
  node_web_asg["Private web Auto Scaling Group<br/>AWS ASG<br/>[main.tf]"]
end

node_terraform_root -->|"instantiates"| node_network_module
node_terraform_root -->|"passes network outputs"| node_database_module
node_terraform_root -->|"passes network and DB values"| node_autoscaling_module
node_providers -.->|"configures"| node_terraform_root
node_inputs_outputs -.->|"supplies and exports values"| node_terraform_root
node_network_module -->|"creates"| node_vpc_subnets
node_vpc_subnets -->|"provides subnet tiers"| node_public_private_routing
node_network_module -->|"creates"| node_security_groups
node_vpc_subnets -->|"database subnets"| node_db_subnet_group
node_db_subnet_group -->|"places database"| node_mysql_rds
node_security_groups -->|"permits MySQL from web tier"| node_mysql_rds
node_database_module -->|"generates"| node_db_password
node_db_password -->|"sets credentials"| node_mysql_rds
node_autoscaling_module -->|"creates"| node_application_load_balancer
node_application_load_balancer -->|"forwards HTTP :80"| node_target_group
node_cloud_init -->|"renders user data"| node_launch_template
node_instance_profile -->|"attaches role"| node_launch_template
node_launch_template -->|"launches instances"| node_web_asg
node_web_asg -->|"registers instances"| node_target_group
node_security_groups -->|"allows Internet HTTP"| node_application_load_balancer
node_security_groups -->|"allows ALB traffic"| node_web_asg
node_web_asg -->|"connects MySQL :3306"| node_mysql_rds

click node_terraform_root "https://github.com/clouddevopshub/3-tier-architecture/blob/main/main.tf"
click node_inputs_outputs "https://github.com/clouddevopshub/3-tier-architecture/blob/main/outputs.tf"
click node_providers "https://github.com/clouddevopshub/3-tier-architecture/blob/main/providers.tf"
click node_network_module "https://github.com/clouddevopshub/3-tier-architecture/blob/main/modules/networking/main.tf"
click node_vpc_subnets "https://github.com/clouddevopshub/3-tier-architecture/blob/main/modules/networking/main.tf"
click node_public_private_routing "https://github.com/clouddevopshub/3-tier-architecture/blob/main/modules/networking/main.tf"
click node_security_groups "https://github.com/clouddevopshub/3-tier-architecture/blob/main/modules/networking/main.tf"
click node_db_subnet_group "https://github.com/clouddevopshub/3-tier-architecture/blob/main/modules/networking/main.tf"
click node_database_module "https://github.com/clouddevopshub/3-tier-architecture/blob/main/modules/database/main.tf"
click node_db_password "https://github.com/clouddevopshub/3-tier-architecture/blob/main/modules/database/main.tf"
click node_mysql_rds "https://github.com/clouddevopshub/3-tier-architecture/blob/main/modules/database/main.tf"
click node_autoscaling_module "https://github.com/clouddevopshub/3-tier-architecture/blob/main/modules/autoscaling/main.tf"
click node_application_load_balancer "https://github.com/clouddevopshub/3-tier-architecture/blob/main/modules/autoscaling/main.tf"
click node_target_group "https://github.com/clouddevopshub/3-tier-architecture/blob/main/modules/autoscaling/main.tf"
click node_cloud_init "https://github.com/clouddevopshub/3-tier-architecture/blob/main/modules/autoscaling/cloud_config.yaml"
click node_launch_template "https://github.com/clouddevopshub/3-tier-architecture/blob/main/modules/autoscaling/main.tf"
click node_instance_profile "https://github.com/clouddevopshub/3-tier-architecture/blob/main/modules/autoscaling/main.tf"
click node_web_asg "https://github.com/clouddevopshub/3-tier-architecture/blob/main/modules/autoscaling/main.tf"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_terraform_root,node_inputs_outputs,node_providers toneBlue
class node_network_module,node_vpc_subnets,node_public_private_routing,node_security_groups,node_db_subnet_group toneAmber
class node_database_module,node_db_password,node_mysql_rds toneMint
class node_autoscaling_module,node_application_load_balancer,node_target_group,node_cloud_init,node_launch_template,node_instance_profile,node_web_asg toneRose
```

## Components

### Terraform root

- [Root composition](https://github.com/clouddevopshub/3-tier-architecture/blob/main/main.tf): Terraform orchestration · Wires networking, database, and autoscaling modules. · `main.tf`
- [Inputs and exports](https://github.com/clouddevopshub/3-tier-architecture/blob/main/outputs.tf): Terraform interface · `outputs.tf`
- [AWS and Terraform providers](https://github.com/clouddevopshub/3-tier-architecture/blob/main/providers.tf): provider constraints · AWS, Cloud-Init, and Random providers are locked for deployment. · `providers.tf`

### Network foundation

- [Networking module](https://github.com/clouddevopshub/3-tier-architecture/blob/main/modules/networking/main.tf): Terraform module · `modules/networking/main.tf`
- [VPC and tiered subnets](https://github.com/clouddevopshub/3-tier-architecture/blob/main/modules/networking/main.tf): AWS network · Public, application, and database tiers span three availability zones. · `modules/networking/main.tf`
- [Internet and NAT routing](https://github.com/clouddevopshub/3-tier-architecture/blob/main/modules/networking/main.tf): AWS routing · Internet Gateway serves public ingress; one NAT Gateway provides private egress. · `modules/networking/main.tf`
- [Tier security groups](https://github.com/clouddevopshub/3-tier-architecture/blob/main/modules/networking/main.tf): security boundary · ALB HTTP, web 8080/SSH, and DB MySQL access are separated. · `modules/networking/main.tf`
- [RDS subnet group](https://github.com/clouddevopshub/3-tier-architecture/blob/main/modules/networking/main.tf): AWS RDS network · `modules/networking/main.tf`

### Database tier

- [Database module](https://github.com/clouddevopshub/3-tier-architecture/blob/main/modules/database/main.tf): Terraform module · `modules/database/main.tf`
- [Generated DB password](https://github.com/clouddevopshub/3-tier-architecture/blob/main/modules/database/main.tf): Random secret · A 16-character password is generated and exposed as sensitive output. · `modules/database/main.tf`
- [Private MySQL RDS](https://github.com/clouddevopshub/3-tier-architecture/blob/main/modules/database/main.tf): AWS RDS instance · MySQL 8.0 pets database is non-public. · `modules/database/main.tf`

### Ingress and compute

- [Autoscaling module](https://github.com/clouddevopshub/3-tier-architecture/blob/main/modules/autoscaling/main.tf): Terraform module · `modules/autoscaling/main.tf`
- [Public application load balancer](https://github.com/clouddevopshub/3-tier-architecture/blob/main/modules/autoscaling/main.tf): AWS ALB · Internet-facing HTTP listener forwards to port 8080 targets. · `modules/autoscaling/main.tf`
- [Web target group](https://github.com/clouddevopshub/3-tier-architecture/blob/main/modules/autoscaling/main.tf): AWS target group · `modules/autoscaling/main.tf`
- [Cloud-init bootstrap](https://github.com/clouddevopshub/3-tier-architecture/blob/main/modules/autoscaling/cloud_config.yaml): instance user data · Rendered into launch-template user data. · `modules/autoscaling/cloud_config.yaml`
- [EC2 launch template](https://github.com/clouddevopshub/3-tier-architecture/blob/main/modules/autoscaling/main.tf): AWS EC2 template · Defines private application instance configuration. · `modules/autoscaling/main.tf`
- [EC2 IAM instance profile](https://github.com/clouddevopshub/3-tier-architecture/blob/main/modules/autoscaling/main.tf): AWS IAM role · Grants configured RDS and CloudWatch Logs permissions. · `modules/autoscaling/main.tf`
- [Private web Auto Scaling Group](https://github.com/clouddevopshub/3-tier-architecture/blob/main/modules/autoscaling/main.tf): AWS ASG · Runs 1–3 application instances in private subnets. · `modules/autoscaling/main.tf`

## Connections

- 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
