Cheatsheets
Terraform
init/plan/apply, state & backends, modules, variables, for_each, and drift.
Visualize
Terraform — plan → apply → state
plan previews a diff against state; apply reconciles real infrastructure to match the config.
apply writes resources here
plan is a preview: a diff of config against the current state — nothing has changed yet.
65 entries
Core workflow10
terraform initInitialize a working directory
terraform planPreview changes without applying them
terraform applyCreate or update infrastructure to match config
terraform destroyTear down all managed infrastructure
terraform fmt -recursiveRewrite files to the canonical HCL style
terraform validateCheck config for syntax and internal consistency
terraform showRender state or a saved plan in human-readable form
terraform outputRead root-module output values
terraform plan -target=...Limit operations to specific resources
terraform apply -replace=ADDRForce-recreate one resource (modern taint)
State11
terraform.tfstateWhat state IS — Terraform's source of truth
backend "s3" { ... }Remote state in S3 with DynamoDB locking
cloud { organization = ... }Terraform Cloud / HCP remote state + runs
terraform state listList every resource tracked in state
terraform state show ADDRShow one resource's tracked attributes
terraform state mv SRC DSTRename/move a resource in state (no infra change)
terraform state rm ADDRForget a resource without destroying it
terraform import ADDR IDBring an existing resource under management
import { to = ...; id = ... }Config-driven import (Terraform 1.5+)
state lockingWhy concurrent applies must be serialized
sensitive data in stateState stores secrets in plaintext — protect it
Providers & resources9
required_providers { ... }Declare and pin which providers to use
version = "~> 5.40"Version constraint operators (~>, >=, ranges)
provider "aws" { region = ... }Configure a provider
provider "aws" { alias = "west" }Multiple provider configs via aliases
resource "type" "name" { ... }Declare a managed resource
data "type" "name" { ... }Read existing data without managing it
resource addressingHow to name any resource: type.name[key]
depends_on = [aws_iam_role.r]Declare an explicit dependency
implicit dependency graphReferences define apply order automatically
Variables, outputs & locals11
variable "name" { type = ... }Declare an input variable
validation { condition = ... }Reject invalid variable values early
variable "x" { sensitive = true }Mark a variable as sensitive
terraform.tfvarsDefault variable values file
variable precedenceWhich value wins when set in multiple places
output "name" { value = ... }Expose a value after apply
output "x" { sensitive = true }Redact an output from CLI display
locals { name = expr }Named intermediate values (DRY expressions)
"${var.project}-${var.env}"String interpolation
for / lookup / coalesce / tryEveryday expressions & built-in functions
cond ? a : bConditional (ternary) expression
Modules6
root vs child moduleEvery directory of .tf files is a module
module "name" { source = ... }Call a reusable module
source = "..." (local/git/registry)Where module code comes from
module variables & outputsPass values in, read results out
module compositionWire modules together via I/O
for_each on a moduleInstantiate a module multiple times
Meta-arguments8
count = NCreate N copies, indexed 0..N-1
for_each = toset([...]) / mapCreate one instance per set/map key (stable)
count vs for_eachWhen to pick which (the core decision)
dynamic "block" { for_each = ... }Generate repeated nested blocks
lifecycle { create_before_destroy }Build the replacement before deleting the old
lifecycle { prevent_destroy = true }Guard a resource against deletion
lifecycle { ignore_changes = [...] }Stop fighting out-of-band changes to specific attributes
provisioner "remote-exec" { ... }Run scripts on a resource — last resort
Operations & safety10
drift detectionPlan reveals config-vs-reality drift
terraform apply -refresh-onlyReconcile state with reality (no infra change)
terraform workspaceMultiple states from one config (and its limits)
TF_VAR_ secretsFeed secrets via environment, not files
secrets handlingKeep credentials out of code, VCS, and logs
CI/CD: plan on PR, apply on mergeThe safe pipeline pattern
footgun: wrong workspace / var-fileDestroying prod via the wrong context
footgun: count index shiftRemoving a list item churns later resources
footgun: monolithic stateOne giant state file is slow and risky
footgun: provider version driftUnpinned providers change under you