Terraform Infrastructure as Code

What is Infrastructure as Code (IaC)?

Gone are the days of manually clicking through cloud consoles. Infrastructure as Code (IaC) lets you define your entire cloud infrastructure in version-controlled, repeatable code files.

"The best infrastructure is the one you never have to think about." - Unknown DevOps Engineer

Why Terraform?

Terraform by HashiCorp has become the industry standard for IaC. Here's why:

  1. Declarative Syntax: You describe what you want, not how to build it.
  2. Multi-Cloud Support: AWS, Azure, GCP, Kubernetes—all with one tool.
  3. State Management: Terraform tracks what's deployed and what needs changing.
  4. Plan Before Apply: See exactly what will change before it happens.

Getting Started

# main.tf - Your first Terraform file
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
}

provider "aws" {
  region = "ap-southeast-1"
}

resource "aws_instance" "web_server" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t3.micro"

  tags = {
    Name        = "WebServer"
    Environment = "Production"
    ManagedBy   = "Terraform"
  }
}

The Terraform Workflow

  1. terraform init - Initialize your project and download providers
  2. terraform plan - Preview what will be created/modified/destroyed
  3. terraform apply - Make it happen!
  4. terraform destroy - Clean up when you're done

Best Practices

Use Modules

Don't repeat yourself. Create reusable modules for common patterns:

module "vpc" {
  source  = "terraform-aws-modules/vpc/aws"
  version = "5.0.0"

  name = "my-vpc"
  cidr = "10.0.0.0/16"
}

Remote State

Store your state file remotely for team collaboration:

terraform {
  backend "s3" {
    bucket = "my-terraform-state"
    key    = "prod/terraform.tfstate"
    region = "ap-southeast-1"
  }
}

Use Variables

Make your code flexible and reusable:

variable "environment" {
  description = "Deployment environment"
  type        = string
  default     = "dev"
}

Pro Tips

  • 🔒 Never commit secrets - Use AWS Secrets Manager or Vault
  • 📝 Version control everything - Your infrastructure history matters
  • 🧪 Test with terraform plan - Always review before applying
  • 🏷️ Tag all resources - You'll thank yourself later

Start small, iterate often, and watch your infrastructure become as reliable as your application code!