Complete Terraform Guide (2025): From Installation to Destroy – AWS EC2 Project
This guide walks through a real Terraform workflow: installing Terraform on Linux, designing a clean project structure, deploying AWS infrastructure using modules, verifying the setup, and safely destroying everything. The approach follows real-world DevOps and cloud security best practices.
- Overview
- Why Terraform Matters
- Terraform Installation (Linux)
- Project Structure Best Practices
- Provider Configuration
- Variables and tfvars
- VPC Module Explained
- EC2 Module Explained
- Root Module Wiring
- Deployment Workflow
- Verification
- Destroying Infrastructure
- Ethical and Security Notes
- Defensive Checklist
- Conclusion
1. Overview
Terraform is an Infrastructure as Code (IaC) tool that allows you to define cloud infrastructure using configuration files. Instead of clicking through the AWS console, infrastructure becomes version-controlled, repeatable, and auditable.
2. Why Terraform Matters
Manual cloud setups are error-prone and hard to reproduce. Terraform solves this by tracking infrastructure using a state file, which compares the desired configuration with the real environment.
For security teams and DevOps engineers, this means:
- Consistent environments
- Reduced misconfigurations
- Easy teardown to avoid cloud cost leaks
3. Terraform Installation (Linux – Ubuntu)
Add HashiCorp GPG Key
wget -O- https://apt.releases.hashicorp.com/gpg | \ gpg --dearmor | \ sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg
Add Repository
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] \ https://apt.releases.hashicorp.com $(lsb_release -cs) main" | \ sudo tee /etc/apt/sources.list.d/hashicorp.list
Install Terraform
sudo apt update sudo apt install terraform -y
Verify
terraform version
4. Project Structure Best Practices
A modular structure keeps infrastructure clean, reusable, and secure.
terraform-aws-http-infra/
├── provider.tf
├── variables.tf
├── terraform.tfvars
├── main.tf
├── outputs.tf
└── modules/
├── vpc/
└── ec2/
Each module has its own variables, resources, and outputs. This separation is critical for scaling real environments.
5. Provider Configuration
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
provider "aws" {
region = var.aws_region
}
6. Variables and tfvars
Variables prevent hardcoding sensitive or environment-specific values.
aws_region = "ap-southeast-1" az = "ap-southeast-1a" project_name = "http-demo" instance_type = "t2.micro" key_name = "LINUX"
7. VPC Module Explained
The VPC module builds the networking layer: VPC, subnet, internet gateway, and routing.
This isolation is essential for cloud security and future expansion.
8. EC2 Module Explained
The EC2 module launches an instance, creates a security group, and installs Apache using user data.
Only required ports (22 and 80) are opened, following minimal exposure principles.
9. Root Module Wiring
The root module connects outputs from the VPC module into the EC2 module. Terraform automatically handles dependency order.
10. Deployment Workflow
terraform init terraform validate terraform plan terraform apply
Always review the plan before applying changes to production environments.
11. Verification
After deployment, open the EC2 public IP in a browser. If Apache is running correctly, you should see:
Hello from Terraform HTTP Instance
12. Destroying Infrastructure
terraform destroy
This step is critical for cost control and security hygiene. Unused resources are a common attack and billing risk.
13. Ethical and Security Notes
This project is for educational and defensive purposes only. Always deploy infrastructure you own or have permission to manage. Misuse of cloud resources may violate laws or provider policies.
14. Defensive Checklist
- Use least-privilege IAM roles
- Restrict SSH access by IP when possible
- Enable logging and monitoring
- Destroy unused infrastructure
- Store Terraform state securely
15. Conclusion
This guide demonstrated a complete Terraform lifecycle: installation, modular design, AWS EC2 deployment, verification, and clean destruction.
Mastering these fundamentals is essential for modern DevOps, cloud security, and infrastructure automation workflows.
Comments
Post a Comment