Skip to content
Harjot Singh Rana
Back to blog
TerraformInfrastructureAWSDevOps7 min read

Infrastructure as Code: Terraform Patterns That Scale

May 20, 2024

Why Terraform

Infrastructure as Code turns your cloud architecture into version-controlled, reviewable, reproducible configuration. Terraform is the most mature tool in this space, and when used correctly, it lets teams manage complex multi-cloud infrastructure with confidence.

Module composition

Don't put everything in one monolithic configuration. Compose small, focused modules:

module "networking" {
  source   = "./modules/vpc"
  name     = "production"
  cidr     = "10.0.0.0/16"
  regions  = ["us-east-1", "us-west-2"]
}

module "database" {
  source      = "./modules/rds"
  vpc_id      = module.networking.vpc_id
  subnet_ids  = module.networking.private_subnet_ids
  instance_class = "db.r6g.large"
}

State management

Terraform state is the source of truth for your infrastructure. Store it remotely with locking:

terraform {
  backend "s3" {
    bucket         = "my-company-terraform-state"
    key            = "production/terraform.tfstate"
    region         = "us-east-1"
    dynamodb_table = "terraform-locks"
    encrypt        = true
  }
}

The review workflow

Every infrastructure change goes through the same pipeline:

  1. Engineer opens a PR with Terraform changes

2. CI runs terraform plan and posts the diff as a PR comment

3. Team reviews the plan, not just the code

4. On merge, CI runs terraform apply automatically

Common mistakes

  • Using terraform import as a crutch instead of writing config first
  • Storing secrets in Terraform state (use a secrets manager)
  • Running apply without reviewing the plan
  • Not using workspaces or separate state files for environments
  • Overusing count and for_each when a simpler approach works
Built with Moonshift