Skip to main content

Terraform — Infrastructure as Declarative Code

Declare infrastructure resources in HCL — version-controlled, reproducible, diff before apply.

When to use

  • Any cloud resource that needs to be reproducible across environments (dev/staging/prod)

Tradeoffs

  • State drift: manual changes to infra cause plan surprises — always change via Terraform
  • State file may contain sensitive values — protect with encryption and access control
terraform {
required_providers {
aws = { source = "hashicorp/aws", version = "~> 5.0" }
}
backend "s3" {
bucket = "my-tf-state"
key = "prod/terraform.tfstate"
region = "us-east-1"
encrypt = true
dynamodb_table = "tf-locks"
}
}

variable "env" { default = "prod" }

resource "aws_s3_bucket" "assets" {
bucket = "my-assets-${var.env}"
tags = { Environment = var.env }
}

output "bucket_name" { value = aws_s3_bucket.assets.bucket }

Gotcha: Terraform state is the source of truth. If terraform plan shows unexpected changes, investigate before applying — something was changed outside of Terraform.