# shekh1995/3-tier-architecture architecture

> Architecture diagram of the GitHub repository shekh1995/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/shekh1995/3-tier-architecture
- Repository: https://github.com/shekh1995/3-tier-architecture

## Overview

#### 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`](https://github.com/shekh1995/3-tier-architecture/blob/main/main.tf) (Root composition), [`modules/networking/main.tf`](https://github.com/shekh1995/3-tier-architecture/blob/main/modules/networking/main.tf) (Networking module), [`modules/autoscaling/main.tf`](https://github.com/shekh1995/3-tier-architecture/blob/main/modules/autoscaling/main.tf) (Autoscaling module), [`modules/database/outputs.tf`](https://github.com/shekh1995/3-tier-architecture/blob/main/modules/database/outputs.tf) (Database endpoint and password)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_root["Root module"]
  node_inputs["Deployment inputs<br/>Terraform inputs"]
  node_root_main["Root composition<br/>Terraform root<br/>[main.tf]"]
  node_root_outputs["Deployment outputs<br/>Terraform outputs<br/>[outputs.tf]"]
end

subgraph group_network["Network foundation"]
  node_network_module["Networking module<br/>Terraform module<br/>[main.tf]"]
  node_vpc{{"VPC and subnet tiers<br/>AWS network"}}
  node_internet_nat["Internet gateway and NAT<br/>AWS routing"]
  node_security_groups["Tier security groups<br/>AWS security groups<br/>[main.tf]"]
  node_rds_subnet_group["RDS subnet group<br/>AWS RDS network"]
end

subgraph group_application["Application tier"]
  node_autoscaling_module["Autoscaling module<br/>Terraform module<br/>[main.tf]"]
  node_alb["Public Application Load Balancer<br/>AWS ALB"]
  node_launch_template["Launch template and cloud-init<br/>EC2 bootstrap<br/>[cloud_config.yaml]"]
  node_asg["Private EC2 Auto Scaling Group<br/>AWS ASG"]
  node_instance_profile["EC2 instance profile<br/>AWS IAM<br/>[main.tf]"]
end

subgraph group_database["Database tier"]
  node_database_module["Database module<br/>Terraform module<br/>[main.tf]"]
  node_rds[("Private MySQL RDS<br/>AWS RDS")]
  node_db_contract["Database endpoint and password<br/>Module outputs<br/>[outputs.tf]"]
end

node_internet(("Internet clients<br/>External traffic"))

node_inputs -->|"configure"| node_root_main
node_root_main -->|"provisions first"| node_network_module
node_network_module -->|"creates"| node_vpc
node_vpc -->|"public and private routes"| node_internet_nat
node_network_module -->|"creates"| node_security_groups
node_network_module -->|"creates"| node_rds_subnet_group
node_root_main -->|"passes DB network"| node_database_module
node_rds_subnet_group -->|"places"| node_rds
node_security_groups -->|"MySQL ingress"| node_rds
node_database_module -->|"provisions"| node_rds
node_rds -->|"endpoint and password"| node_db_contract
node_root_main -->|"passes subnets and security groups"| node_autoscaling_module
node_autoscaling_module -->|"provisions"| node_alb
node_autoscaling_module -->|"renders"| node_launch_template
node_launch_template -->|"defines instances"| node_asg
node_instance_profile -->|"attached to instances"| node_asg
node_db_contract -->|"injects connection values"| node_launch_template
node_internet -->|"HTTP :80"| node_alb
node_alb -->|"targets :8080"| node_asg
node_asg -->|"MySQL :3306"| node_rds
node_db_contract -->|"sensitive password"| node_root_outputs
node_alb -->|"DNS name"| node_root_outputs

click node_root_main "https://github.com/shekh1995/3-tier-architecture/blob/main/main.tf"
click node_root_outputs "https://github.com/shekh1995/3-tier-architecture/blob/main/outputs.tf"
click node_network_module "https://github.com/shekh1995/3-tier-architecture/blob/main/modules/networking/main.tf"
click node_security_groups "https://github.com/shekh1995/3-tier-architecture/blob/main/modules/networking/main.tf"
click node_database_module "https://github.com/shekh1995/3-tier-architecture/blob/main/modules/database/main.tf"
click node_db_contract "https://github.com/shekh1995/3-tier-architecture/blob/main/modules/database/outputs.tf"
click node_autoscaling_module "https://github.com/shekh1995/3-tier-architecture/blob/main/modules/autoscaling/main.tf"
click node_launch_template "https://github.com/shekh1995/3-tier-architecture/blob/main/modules/autoscaling/cloud_config.yaml"
click node_instance_profile "https://github.com/shekh1995/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_inputs,node_root_main,node_root_outputs toneBlue
class node_network_module,node_vpc,node_internet_nat,node_security_groups,node_rds_subnet_group toneAmber
class node_autoscaling_module,node_alb,node_launch_template,node_asg,node_instance_profile toneMint
class node_database_module,node_rds,node_db_contract toneRose
class node_internet toneNeutral
```

## Components

### Root module

- **Deployment inputs**: Terraform inputs
- [Root composition](https://github.com/shekh1995/3-tier-architecture/blob/main/main.tf): Terraform root · Composes networking, database, and autoscaling modules. · `main.tf`
- [Deployment outputs](https://github.com/shekh1995/3-tier-architecture/blob/main/outputs.tf): Terraform outputs · Exports ALB DNS and sensitive database password. · `outputs.tf`

### Network foundation

- [Networking module](https://github.com/shekh1995/3-tier-architecture/blob/main/modules/networking/main.tf): Terraform module · Builds VPC tiers, routing, subnet group, and security boundaries. · `modules/networking/main.tf`
- **VPC and subnet tiers**: AWS network · Public, private application, and database subnets span three AZs.
- **Internet gateway and NAT**: AWS routing · Internet serves public ingress; NAT provides private app egress.
- [Tier security groups](https://github.com/shekh1995/3-tier-architecture/blob/main/modules/networking/main.tf): AWS security groups · Allow 80 to ALB, 8080 to web, and 3306 to MySQL. · `modules/networking/main.tf`
- **RDS subnet group**: AWS RDS network

### Application tier

- [Autoscaling module](https://github.com/shekh1995/3-tier-architecture/blob/main/modules/autoscaling/main.tf): Terraform module · Creates public ingress and private EC2 capacity. · `modules/autoscaling/main.tf`
- **Public Application Load Balancer**: AWS ALB · HTTP listener on port 80 forwards to application targets.
- [Launch template and cloud-init](https://github.com/shekh1995/3-tier-architecture/blob/main/modules/autoscaling/cloud_config.yaml): EC2 bootstrap · Renders database-derived values into instance user data. · `modules/autoscaling/cloud_config.yaml`
- **Private EC2 Auto Scaling Group**: AWS ASG · Runs 1–3 web instances listening on port 8080.
- [EC2 instance profile](https://github.com/shekh1995/3-tier-architecture/blob/main/modules/autoscaling/main.tf): AWS IAM · Grants instances RDS and CloudWatch Logs permissions. · `modules/autoscaling/main.tf`

### Database tier

- [Database module](https://github.com/shekh1995/3-tier-architecture/blob/main/modules/database/main.tf): Terraform module · Creates MySQL and its generated credentials. · `modules/database/main.tf`
- **Private MySQL RDS**: AWS RDS · MySQL 8.0 database named pets.
- [Database endpoint and password](https://github.com/shekh1995/3-tier-architecture/blob/main/modules/database/outputs.tf): Module outputs · Sensitive connection values passed to application bootstrap. · `modules/database/outputs.tf`

### Other components

- **Internet clients**: External traffic

## Connections

- 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
