Skip to main content

Command Palette

Search for a command to run...

8. πŸ—οΈ Terraform: Infrastructure as Code (IaC) from Scratch for DevOps Engineers

Published
β€’3 min readβ€’View as Markdown
N

Open to Work | Cloud & DevOps Engineer | AWS | Kubernetes | Terraform | CI/CD | Automations | Available for Full-Time / Freelance / Mentorship

🎯 Why Terraform?

Manual infrastructure creation leads to:

  • Configuration drift

  • Human errors

  • No version control

Terraform solves this using Infrastructure as Code (IaC).


πŸ”Ή What is Terraform?

Terraform is an open-source IaC tool by HashiCorp that:

  • Automates infrastructure provisioning

  • Works across clouds (AWS, Azure, GCP)

  • Uses declarative configuration


πŸ”Ή Terraform vs Manual Provisioning

ManualTerraform
Error-proneConsistent
No historyVersion-controlled
Hard to replicateRepeatable

πŸ”Ή Terraform Workflow

terraform init
terraform plan
terraform apply
terraform destroy

πŸ”Ή Terraform Configuration Files

FilePurpose
main.tfResource definitions
variables.tfInput variables
outputs.tfOutput values
provider.tfCloud provider
backend.tfRemote state

πŸ”Ή Terraform Provider Example

provider "aws" {
  region = "us-east-1"
}

πŸ”Ή Create an EC2 Instance (Basic)

resource "aws_instance" "web" {
  ami           = "ami-0abcd1234"
  instance_type = "t2.micro"

  tags = {
    Name = "Terraform-EC2"
  }
}

πŸ”Ή Terraform Variables

variable "instance_type" {
  default = "t2.micro"
}

Usage:

instance_type = var.instance_type

πŸ”Ή Terraform Output

output "instance_ip" {
  value = aws_instance.web.public_ip
}

πŸ”Ή What is Terraform State?

State file (terraform.tfstate):

  • Maps real infra to code

  • Tracks resource changes

⚠️ Never edit manually.


πŸ”Ή Why Remote Backend (S3 + DynamoDB)?

  • Centralized state

  • Team collaboration

  • State locking


Backend Example

terraform {
  backend "s3" {
    bucket         = "tf-state-bucket"
    key            = "prod/terraform.tfstate"
    region         = "us-east-1"
    dynamodb_table = "terraform-lock"
  }
}

πŸ”Ή What is State Locking?

Prevents:

  • Multiple users applying changes simultaneously

  • State corruption

DynamoDB handles locking.


πŸ”Ή Terraform Plan vs Apply

planapply
Preview changesExecute changes
SafeDestructive

πŸ”Ή Terraform Modules

Modules allow:

  • Reusability

  • Clean structure

  • Standardization


Simple Module Structure

modules/
 └── ec2/
     β”œβ”€β”€ main.tf
     β”œβ”€β”€ variables.tf
     └── outputs.tf

πŸ”Ή Terraform Best Practices

  • Use remote backend

  • Lock state

  • Use modules

  • Separate environments

  • Never commit state file


πŸ”Ή Terraform Destroy (Use Carefully)

terraform destroy

πŸ”Ή Real Production Scenario

Issue: Infra drift across environments
Fix: Terraform applied consistently
Result: Zero manual changes, stable infra


🎯 Interview Takeaways

  • Terraform is declarative

  • State is critical

  • Remote backend is mandatory

  • plan before apply

  • Modules are reusable

More from this blog

Beginner to Advanced

16 posts