AWS infrastructure using a modular Terraform structure with support for multiple environments (dev & production).
TERRAFORM-MODULES/
├── ENVS/
│ └── dev/
│ ├── main.tf # Calls VPC & EC2 modules
│ ├── variables.tf # Variable declarations
│ ├── provider.tf # AWS provider config
│ ├── outputs.tf # Shows vpc_id, subnet IDs, EC2 IP
│ └── dev.tfvars.example # Example tfvars (actual dev.tfvars is gitignored)
│
└── MODULES/
├── VPC/
│ ├── main.tf # VPC, public & private subnets
│ ├── variables.tf
│ └── outputs.tf # Exports vpc_id, subnet IDs
└── EC2/
├── main.tf # EC2 instance inside custom VPC
├── variables.tf
└── outputs.tf
- Creates a custom AWS VPC with public and private subnets
- Launches an EC2 instance inside the custom VPC (not default VPC)
- Uses Terraform modules for reusable, clean infrastructure
- Currently has dev environment set up with full VPC + EC2 infrastructure
dev.tfvars → dev/main.tf → VPC Module → outputs (subnet_id)
→ EC2 Module ← uses subnet_id from VPC
The dev/main.tf acts as a bridge — it passes values from dev.tfvars to both modules, and passes the VPC output (subnet ID) into the EC2 module.
cd ENVS/dev#
Look at dev.tfvars.example and create dev.tfvars with your own values
cp dev.tfvars.example dev.tfvars
# Now, put the actual values in dev.tfvarsterraform initterraform plan -var-file="dev.tfvars"terraform apply -var-file="dev.tfvars"terraform destroy -var-file="dev.tfvars"This project is built to be easily expandable! Here are some things you can try:
Add a Production Environment:
ENVS/
└── production/
├── main.tf
├── variables.tf
├── provider.tf
└── prod.tfvars # Different CIDR, instance type, region etc.
Just copy the dev folder, rename to production, and update values in prod.tfvars!
Add More Modules:
MODULES/
├── RDS/ # Add a database
├── S3/ # Add storage buckets
├── ALB/ # Add a load balancer
└── SG/ # Add security groups as a module
Each module follows the same pattern — main.tf, variables.tf, and outputs.tf.
⚠️ Note:dev.tfvarsis not pushed to GitHub because it contains sensitive values like AMI IDs and environment-specific config. Instead,dev.tfvars.exampleis provided as a template. Copy it and fill in your own values.
cp dev.tfvars.example dev.tfvars| Variable | Description | Example |
|---|---|---|
cidr-block |
VPC CIDR range | 10.0.0.0/16 |
public_cidr |
Public subnet CIDR | 10.0.1.0/24 |
private_cidr |
Private subnet CIDR | 10.0.2.0/24 |
availability_zone |
AWS AZ | us-east-1a |
ami_id |
EC2 AMI ID | ami-0abc123... |
instance_type |
EC2 instance type | t3.micro |
name |
Resource name tag | Server |
Name |
VPC name tag | vpc_module |
After terraform apply, you will see:
vpc_id— ID of the created VPCpublic_subnet_id— ID of the public subnetprivate_subnet_id— ID of the private subnetec2_public_ip— Public IP of the EC2 instanceec2_public_dns— Public DNS of the EC2 instance
- Terraform Modules — Reusable infrastructure components
- Output Variables — Passing data between modules
- tfvars files — Environment-specific values
- Multi-environment setup — Dev is there but we can add production env also.