# madcoderzx/terraform architecture

> Architecture diagram of the GitHub repository madcoderzx/terraform, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-22). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/madcoderzx/terraform
- Repository: https://github.com/madcoderzx/terraform

## Overview

#### Purpose and Entry Points
This is a Terraform root module that provisions a small AWS environment for hosting a Dockerized or other application workload on one public EC2 instance, with durable S3 storage. Run Terraform from the repository root using `terraform init`, `plan`, `apply`, and `destroy`. Core entry points are `main.tf`, `providers.tf`, `variables.tf`, and `outputs.tf`.

#### Terraform and AWS Boundary
Terraform is the control plane: it evaluates the root module, builds resource dependencies, persists state locally, and calls AWS APIs through the AWS provider. Provider/runtime constraints are defined in `providers.tf`; exact initialized provider selections are recorded in `.terraform.lock.hcl`. AWS credentials are expected to be supplied externally (for example, AWS CLI configuration), not by this repository.

#### Input Contract and Environment Selection
Deployment parameters are declared in `variables.tf`, including AWS region, EC2 instance type/name, optional SSH key pair, and globally unique S3 bucket name. `terraform.tfvars.example` is the template for environment-specific overrides. Defaults target `ap-southeast-2`; deployment configuration therefore controls both AWS placement and resource identity.

#### Network Foundation
`main.tf` establishes the dependency chain for public networking:
1. VPC with the `10.0.0.0/16` address space.
2. Public subnet (`10.0.1.0/24`) in the selected availability zone.
3. Internet Gateway attached to the VPC.
4. Route table with default internet traffic routed through that gateway, associated with the public subnet.

This chain is the connectivity boundary between the internet and workload resources. The EC2 instance is intentionally placed in the public subnet and receives a public IP.

#### Security Boundary
A security group in `main.tf` is the instance firewall boundary. It permits inbound TCP traffic for SSH (22), HTTP (80), and HTTPS (443), with traffic originating from the public internet according to the documented design. It is attached to the EC2 instance; route availability alone does not expose the workload without these security-group rules.

#### Compute Provisioning
`main.tf` resolves an Amazon Linux 2 AMI through a Terraform data source and creates a configurable `t3.micro`-class EC2 instance. The instance depends on the subnet and security group and optionally uses the configured AWS key pair for SSH access. This repository provisions the host only; no Docker image, container runtime setup, application artifact, or deployment pipeline exists in the file tree.

#### Storage and Outputs
`main.tf` provisions a standalone S3 bucket using the caller-provided bucket name. S3 is accessed through AWS APIs and is not a filesystem mount or explicitly coupled to the EC2 instance by this module. `outputs.tf` exports the instance public/private IPs, instance ID, and bucket name, forming the post-apply handoff for operators and downstream deployment steps.

#### State and Lifecycle
Terraform state is local by default; no remote backend, state locking service, CI/CD workflow, or application runtime automation is present. `.gitignore` is intended to exclude state and local variable files, while `.terraform.lock.hcl` should remain versioned. `terraform destroy` reverses the managed AWS graph, including compute, storage, networking, and security resources.

Main entry points: [`main.tf`](https://github.com/madcoderzx/terraform/blob/main/main.tf) (Terraform Root Module), [`variables.tf`](https://github.com/madcoderzx/terraform/blob/main/variables.tf) (Deployment Inputs), [`outputs.tf`](https://github.com/madcoderzx/terraform/blob/main/outputs.tf) (Post-apply Outputs), [`terraform.tfvars.example`](https://github.com/madcoderzx/terraform/blob/main/terraform.tfvars.example) (Override Template)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_terraform["Terraform Control Plane"]
  node_root_module["Terraform Root Module<br/>[main.tf]"]
  node_provider{{"AWS Provider<br/>Terraform provider<br/>[providers.tf]"}}
  node_inputs["Deployment Inputs<br/>variables<br/>[variables.tf]"]
  node_tfvars_template["Override Template<br/>tfvars example"]
  node_local_state["Local Terraform State"]
  node_outputs["Post-apply Outputs<br/>Terraform outputs<br/>[outputs.tf]"]
end

subgraph group_network["Public Network"]
  node_vpc["VPC<br/>[main.tf]"]
  node_public_subnet["Public Subnet<br/>aws_subnet<br/>[main.tf]"]
  node_internet_gateway{{"Internet Gateway<br/>aws_internet_gateway<br/>[main.tf]"}}
  node_route_table["Public Route Table<br/>aws_route_table<br/>[main.tf]"]
  node_subnet_association["Subnet Route Association<br/>aws_route_table_association<br/>[main.tf]"]
end

subgraph group_aws["AWS Resources"]
  node_security_group["Instance Security Group<br/>aws_security_group<br/>[main.tf]"]
  node_amazon_linux_ami["Amazon Linux 2 AMI<br/>aws_ami data source<br/>[main.tf]"]
  node_ec2_instance["Public EC2 Host<br/>aws_instance<br/>[main.tf]"]
  node_s3_bucket[("Durable S3 Bucket<br/>aws_s3_bucket<br/>[main.tf]")]
end

node_operator(("Operator<br/>external user"))

node_operator -->|"provides overrides"| node_inputs
node_tfvars_template -.->|"templates"| node_inputs
node_inputs -->|"configures"| node_root_module
node_root_module -->|"uses"| node_provider
node_root_module -->|"persists state"| node_local_state
node_root_module -->|"provisions"| node_vpc
node_root_module -->|"provisions"| node_security_group
node_root_module -->|"queries"| node_amazon_linux_ami
node_root_module -->|"provisions"| node_s3_bucket
node_vpc -->|"contains"| node_public_subnet
node_vpc -->|"attached gateway"| node_internet_gateway
node_vpc -->|"network route table"| node_route_table
node_internet_gateway -->|"default route target"| node_route_table
node_route_table -->|"associated through"| node_subnet_association
node_public_subnet -->|"uses routes"| node_subnet_association
node_public_subnet -->|"places host"| node_ec2_instance
node_security_group -->|"firewall attached"| node_ec2_instance
node_amazon_linux_ami -->|"machine image"| node_ec2_instance
node_ec2_instance -->|"exports ID and IPs"| node_outputs
node_s3_bucket -->|"exports bucket name"| node_outputs

click node_root_module "https://github.com/madcoderzx/terraform/blob/main/main.tf"
click node_provider "https://github.com/madcoderzx/terraform/blob/main/providers.tf"
click node_inputs "https://github.com/madcoderzx/terraform/blob/main/variables.tf"
click node_tfvars_template "https://github.com/madcoderzx/terraform/blob/main/terraform.tfvars.example"
click node_outputs "https://github.com/madcoderzx/terraform/blob/main/outputs.tf"
click node_vpc "https://github.com/madcoderzx/terraform/blob/main/main.tf"
click node_public_subnet "https://github.com/madcoderzx/terraform/blob/main/main.tf"
click node_internet_gateway "https://github.com/madcoderzx/terraform/blob/main/main.tf"
click node_route_table "https://github.com/madcoderzx/terraform/blob/main/main.tf"
click node_subnet_association "https://github.com/madcoderzx/terraform/blob/main/main.tf"
click node_security_group "https://github.com/madcoderzx/terraform/blob/main/main.tf"
click node_amazon_linux_ami "https://github.com/madcoderzx/terraform/blob/main/main.tf"
click node_ec2_instance "https://github.com/madcoderzx/terraform/blob/main/main.tf"
click node_s3_bucket "https://github.com/madcoderzx/terraform/blob/main/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_root_module,node_provider,node_inputs,node_tfvars_template,node_local_state,node_outputs toneBlue
class node_vpc,node_public_subnet,node_internet_gateway,node_route_table,node_subnet_association toneAmber
class node_security_group,node_amazon_linux_ami,node_ec2_instance,node_s3_bucket toneMint
class node_operator toneNeutral
```

## Components

### Terraform Control Plane

Root module planning, provider execution, and local state.

- [Terraform Root Module](https://github.com/madcoderzx/terraform/blob/main/main.tf): root module · Builds the managed dependency graph. · `main.tf`
- [AWS Provider](https://github.com/madcoderzx/terraform/blob/main/providers.tf): Terraform provider · Calls AWS APIs using externally supplied credentials. · `providers.tf`
- [Deployment Inputs](https://github.com/madcoderzx/terraform/blob/main/variables.tf): variables · Region, instance, key pair, and bucket identity. · `variables.tf`
- [Override Template](https://github.com/madcoderzx/terraform/blob/main/terraform.tfvars.example): tfvars example · Template for environment-specific values. · `terraform.tfvars.example`
- **Local Terraform State**: state · Default local record of managed resources.
- [Post-apply Outputs](https://github.com/madcoderzx/terraform/blob/main/outputs.tf): Terraform outputs · Exports instance addresses, ID, and bucket name. · `outputs.tf`

### Public Network

Internet-connected VPC path for the instance.

- [VPC](https://github.com/madcoderzx/terraform/blob/main/main.tf): aws_vpc · 10.0.0.0/16 network boundary. · `main.tf`
- [Public Subnet](https://github.com/madcoderzx/terraform/blob/main/main.tf): aws_subnet · 10.0.1.0/24 subnet in the selected availability zone. · `main.tf`
- [Internet Gateway](https://github.com/madcoderzx/terraform/blob/main/main.tf): aws_internet_gateway · `main.tf`
- [Public Route Table](https://github.com/madcoderzx/terraform/blob/main/main.tf): aws_route_table · Routes default internet traffic through the gateway. · `main.tf`
- [Subnet Route Association](https://github.com/madcoderzx/terraform/blob/main/main.tf): aws_route_table_association · `main.tf`

### AWS Resources

- [Instance Security Group](https://github.com/madcoderzx/terraform/blob/main/main.tf): aws_security_group · Allows public SSH, HTTP, and HTTPS ingress. · `main.tf`
- [Amazon Linux 2 AMI](https://github.com/madcoderzx/terraform/blob/main/main.tf): aws_ami data source · Resolves the machine image for the host. · `main.tf`
- [Public EC2 Host](https://github.com/madcoderzx/terraform/blob/main/main.tf): aws_instance · Configurable public application host; no workload is deployed by this module. · `main.tf`
- [Durable S3 Bucket](https://github.com/madcoderzx/terraform/blob/main/main.tf): aws_s3_bucket · Standalone durable storage with caller-provided name. · `main.tf`

### Other components

- **Operator**: external user · Supplies credentials and runs Terraform commands.

## Connections

- Operator → Deployment Inputs: provides overrides
- Override Template → Deployment Inputs: templates
- Deployment Inputs → Terraform Root Module: configures
- Terraform Root Module → AWS Provider: uses
- Terraform Root Module → Local Terraform State: persists state
- Terraform Root Module → VPC: provisions
- Terraform Root Module → Instance Security Group: provisions
- Terraform Root Module → Amazon Linux 2 AMI: queries
- Terraform Root Module → Durable S3 Bucket: provisions
- VPC → Public Subnet: contains
- VPC → Internet Gateway: attached gateway
- VPC → Public Route Table: network route table
- Internet Gateway → Public Route Table: default route target
- Public Route Table → Subnet Route Association: associated through
- Public Subnet → Subnet Route Association: uses routes
- Public Subnet → Public EC2 Host: places host
- Instance Security Group → Public EC2 Host: firewall attached
- Amazon Linux 2 AMI → Public EC2 Host: machine image
- Public EC2 Host → Post-apply Outputs: exports ID and IPs
- Durable S3 Bucket → Post-apply Outputs: exports bucket name
