How madcoderzx/terraform works
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 Terraform Root Module · variables.tf Deployment Inputs · outputs.tf Post-apply Outputs · terraform.tfvars.example Override Template
Components 16
Terraform Control Plane
Root module planning, provider execution, and local state.
- Terraform Root Module · Builds the managed dependency graph.
main.tf - AWS Provider · Calls AWS APIs using externally supplied credentials.
providers.tf - Deployment Inputs · Region, instance, key pair, and bucket identity.
variables.tf - Override Template · Template for environment-specific values.
terraform.tfvars.example - Local Terraform State · Default local record of managed resources.
- Post-apply Outputs · Exports instance addresses, ID, and bucket name.
outputs.tf
Public Network
Internet-connected VPC path for the instance.
- VPC · 10.0.0.0/16 network boundary.
main.tf - Public Subnet · 10.0.1.0/24 subnet in the selected availability zone.
main.tf - Internet Gateway
main.tf - Public Route Table · Routes default internet traffic through the gateway.
main.tf - Subnet Route Association
main.tf
AWS Resources
- Instance Security Group · Allows public SSH, HTTP, and HTTPS ingress.
main.tf - Amazon Linux 2 AMI · Resolves the machine image for the host.
main.tf - Public EC2 Host · Configurable public application host; no workload is deployed by this module.
main.tf - Durable S3 Bucket · Standalone durable storage with caller-provided name.
main.tf
Other components
- Operator · Supplies credentials and runs Terraform commands.
Connections 20
- 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