Batch 01 · Aarambh — AWS + Agentic AI starts 28 June 2026Batch 01 · Aarambh — AWS + Agentic AI starts 28 June 2026Batch 01 · Aarambh — AWS + Agentic AI starts 28 June 2026Batch 01 · Aarambh — AWS + Agentic AI starts 28 June 2026Batch 01 · Aarambh — AWS + Agentic AI starts 28 June 2026Batch 01 · Aarambh — AWS + Agentic AI starts 28 June 2026Batch 01 · Aarambh — AWS + Agentic AI starts 28 June 2026Batch 01 · Aarambh — AWS + Agentic AI starts 28 June 2026
All cheatsheets
Terraform · Cheatsheet

Terraform Cheatsheet

Modules, workspaces, state surgery, import, plan/apply tips. Plus the OpenTofu equivalents.

Updated 2026-05-21 9 min

Core workflow

terraform initDownload providers + modules
terraform init -upgradeUpgrade providers to latest matching version
terraform fmt -recursiveFormat all .tf files
terraform validateStatic syntax + type check
terraform plan -out=tfplanPlan and save to file
terraform apply tfplanApply the saved plan (predictable)
terraform destroy -target=module.xDestroy just one resource/module

State surgery

terraform state listList everything in state
terraform state show aws_instance.webShow full state of one resource
terraform state mv old newRename without recreating
terraform state rm aws_instance.webForget about a resource (does not destroy it)
terraform import aws_iam_role.lambda role-nameBring existing resource into state
terraform refreshSync state with real-world resources
terraform force-unlock <LOCK_ID>Break a stuck state lock (use sparingly)

Workspaces

terraform workspace listList workspaces
terraform workspace new stagingCreate + switch to a workspace
terraform workspace select prodSwitch to existing workspace
${terraform.workspace}Reference current workspace in code

Variables & outputs

terraform plan -var='region=us-east-1'Override at CLI
terraform plan -var-file=prod.tfvarsOverride from a file
TF_VAR_region=us-east-1 terraform applyVia env var
terraform output -jsonAll outputs as JSON (CI-friendly)
terraform output vpc_idSingle output value

Modules

module "vpc" { source = "./modules/vpc" }Local module
source = "terraform-aws-modules/vpc/aws" version = "~> 5.5"Registry module, pinned
source = "git::https://github.com/x/repo.git//path?ref=v1.2.0"Git module, tagged
terraform get -updateRe-pull module sources

Common HCL idioms

for_each = toset(["a","b"])Iterate a set
count = var.enable ? 1 : 0Conditional resource
dynamic "ingress" { for_each = var.rules content { ... } }Dynamic nested blocks
depends_on = [aws_iam_role.x]Force ordering
lifecycle { prevent_destroy = true }Guard production resources
lifecycle { ignore_changes = [tags["LastModified"]] }Ignore drifty attributes

Backends (remote state)

backend "s3" { bucket=..., key=..., dynamodb_table=... }S3 + DynamoDB lock — standard
terraform init -reconfigureAfter changing backend config
terraform init -migrate-stateMove state to a new backend

Speed + safety tricks

terraform plan -parallelism=20Parallelise refresh
terraform apply -refresh=falseSkip refresh (faster on huge states)
TF_LOG=DEBUG terraform apply 2> tf.logVerbose log to file
terragrunt run-all planPlan multiple stacks (Terragrunt)
tflint / tfsec / checkov -d .Lint + security + policy scan

OpenTofu compatibility

tofu init / plan / applyDrop-in replacement, MPL-2.0
tofu state encryptionNative at-rest state encryption (1.7+)
Provider syntax unchangedSame providers, same registry mirror

Want the full hands-on training behind this?

Cloudadhar batches walk you through every command in a real production setup — with labs, code reviews, and 1:1 doubt sessions.