Home / Topics / Terraform & IaC / Cheatsheet
Terraform & IaC

Cheatsheet

Dense, scannable reference for daily Terraform work and interview prep: CLI, HCL patterns, state, modules, backends, quality gates and CI.


Core workflow

CommandWhat it does
terraform initDownloads providers and modules, configures the backend, writes .terraform.lock.hcl
terraform init -upgradeRe-resolves providers and modules to the newest versions allowed by constraints
terraform init -backend=falseInitialise without touching the backend (good for validate in CI)
terraform init -backend-config="bucket=x"Supply backend settings that cannot live in code
terraform init -reconfigureIgnore the saved backend config and start fresh
terraform init -migrate-stateCopy state from the old backend to the new one
terraform fmt -recursiveRewrite files to canonical style
terraform fmt -check -diffCI mode: fail if anything needs formatting
terraform validateCheck syntax and internal consistency (no cloud calls)
terraform planShow what would change
terraform applyApply changes after confirmation
terraform apply tfplanApply a saved plan exactly as reviewed
terraform destroyDestroy everything in this state (same as apply -destroy)
terraform output -raw namePrint one output without quotes (scripting)
terraform output -jsonAll outputs as JSON
terraform show tfplanHuman-readable view of a saved plan
terraform show -json tfplanPlan as JSON for policy tools
terraform graph | dot -Tpng > g.pngDependency graph
terraform providersShow providers required by config and modules
terraform providers lock -platform=linux_amd64 -platform=darwin_arm64Add hashes for several platforms to the lock file
terraform versionTerraform and provider versions
terraform consoleEvaluate expressions interactively against current state
terraform workspace list/new/select/show/deleteManage workspaces
terraform force-unlock <ID>Release a stuck lock (only if you are certain)

Plan and apply flags

FlagUse
-out=tfplanSave the plan to apply later
-var 'name=value' / -var-file=prod.tfvarsSet inputs
-target=aws_instance.webLimit to one resource and its dependencies (use for break-glass only)
-replace=aws_instance.webForce recreation (replaces taint)
-refresh-onlyUpdate state to match reality, change nothing
-refresh=falseSkip the refresh step (faster, riskier)
-destroyPlan a destroy
-detailed-exitcodeExit 0 no changes, 1 error, 2 changes
-lock-timeout=120sWait for a lock instead of failing at once
-lock=falseDo not lock (almost never)
-parallelism=20Concurrent operations, default 10
-input=falseNever prompt (CI)
-no-colorClean logs
-auto-approveSkip the prompt (CI after a reviewed plan, or labs)
-generate-config-out=gen.tfWith import blocks, write resource config for you
-compact-warningsShorter warnings

Plan symbols: + create, - destroy, ~ update in place, -/+ replace (destroy then create), +/- replace with create_before_destroy, <= data read.

State commands

bash
terraform state list                              # all addresses
terraform state list module.vpc                   # filter
terraform state show aws_instance.web             # attributes of one resource
terraform state mv aws_instance.a aws_instance.b  # rename in state
terraform state mv -state-out=other.tfstate aws_instance.a aws_instance.a   # move between states
terraform state rm aws_instance.web               # forget it, do not destroy it
terraform state pull > backup.tfstate             # download current state
terraform state push backup.tfstate               # upload (dangerous, check serial)
terraform state replace-provider registry.terraform.io/-/aws hashicorp/aws
terraform show -json | jq '.values.root_module.resources[].address'
RuleWhy
Never edit terraform.tfstate by handLineage and serial checks, easy to corrupt
Back up before surgerystate pull, plus S3 bucket versioning
State holds secrets in plain textEncrypt the bucket, restrict access, never commit
Prefer moved, import and removed blocks over CLI surgeryReviewable and repeatable

HCL building blocks

hcl
terraform {
  required_version = ">= 1.11"
  required_providers {
    aws = { source = "hashicorp/aws", version = "~> 6.0" }
  }
}

provider "aws" {
  region = var.region
  default_tags { tags = { Project = "demo", ManagedBy = "terraform" } }
}

provider "aws" {                       # second region
  alias  = "dr"
  region = "ap-southeast-1"
}

variable "instance_count" {
  description = "How many instances."
  type        = number
  default     = 2
  validation {
    condition     = var.instance_count >= 1 && var.instance_count <= 10
    error_message = "instance_count must be between 1 and 10."
  }
}

variable "db_password" {
  type      = string
  sensitive = true
}

variable "rules" {
  type = map(object({
    port  = number
    cidrs = optional(list(string), ["10.0.0.0/8"])
  }))
}

locals {
  name = "${var.project}-${var.env}"
  tags = merge(var.tags, { Name = local.name })
}

data "aws_caller_identity" "current" {}

resource "aws_s3_bucket" "this" {
  bucket   = "${local.name}-${data.aws_caller_identity.current.account_id}"
  provider = aws.dr                    # pick a non-default provider
}

output "bucket_arn" {
  description = "ARN of the bucket."
  value       = aws_s3_bucket.this.arn
  sensitive   = false
}
BlockPurpose
terraformVersion and provider requirements, backend
providerConfigure a plugin (region, auth, default tags)
resourceCreate and manage an object
dataRead an existing object
variableModule input
localsNamed expressions
outputModule return value
moduleCall a child module
moved / import / removedRefactor and adopt resources declaratively
checkNon-blocking assertions that warn during plan and apply

Common variable types: string, number, bool, list(string), set(string), map(string), object({...}), tuple([...]), any.

Expressions and functions

hcl
# conditional
instance_type = var.env == "prod" ? "t3.large" : "t3.micro"

# for expressions
upper_names = [for n in var.names : upper(n)]
by_name     = { for s in aws_subnet.private : s.tags["Name"] => s.id }
filtered    = { for k, v in var.buckets : k => v if v.versioning }
grouped     = { for r in var.rules : r.port => r.name... }     # group by key

# splat
ids = aws_instance.web[*].id

# dynamic blocks
dynamic "ingress" {
  for_each = var.ingress_rules
  content {
    from_port   = ingress.value.port
    to_port     = ingress.value.port
    protocol    = "tcp"
    cidr_blocks = ingress.value.cidrs
  }
}

# optional values and defaults
name = coalesce(var.name, "default")
cfg  = try(var.config.timeout, 30)
one_id = one(aws_s3_bucket.optional[*].id)
FunctionExampleResult
cidrsubnet(prefix, newbits, num)cidrsubnet("10.0.0.0/16", 8, 2)10.0.2.0/24
merge(a, b)merge({a=1},{b=2}){a=1,b=2}
lookup(map, key, default)lookup(var.sizes, "dev", "small")value or default
element(list, i)element(["a","b"], 3)wraps around: "b"
slice(list, from, to)slice(azs, 0, 2)first two
flatten([[1],[2,3]])[1,2,3]
toset(list) / tolist(set)toset(["a","a"])["a"]
format("%s-%03d", "web", 7)web-007
join(",", list) / split(",", str)string or list
templatefile(path, vars)templatefile("ud.tftpl", { env = "dev" })rendered text
jsonencode(obj) / jsondecode(str)policy documentsJSON
file(path) / filebase64(path)file contents
base64encode(str)user database64
try(a, b) / can(expr)fallback / boolean
length, keys, values, contains, distinct, concat, zipmap, setproductcollection helpers
abspath, path.module, path.root, path.cwdpaths
timestamp()changes every run, avoid in resources

Meta-arguments and lifecycle

hcl
resource "aws_instance" "web" {
  for_each = toset(["a", "b"])        # or count = 3, never both
  depends_on = [aws_iam_role_policy.x]
  provider   = aws.dr

  lifecycle {
    create_before_destroy = true
    prevent_destroy       = true
    ignore_changes        = [tags["LastScan"], desired_capacity]
    replace_triggered_by  = [aws_launch_template.this.latest_version]

    precondition {
      condition     = data.aws_ami.x.architecture == "x86_64"
      error_message = "AMI must be x86_64."
    }
    postcondition {
      condition     = self.public_ip == null
      error_message = "Instance must not have a public IP."
    }
  }
}
ChoicePreferBecause
Identical copies, on/off togglecountSimple and numeric
Distinct named itemsfor_eachStable addresses when items are added or removed
Order not inferable from referencesdepends_onExplicit edge in the graph
Zero-downtime replacementcreate_before_destroyNeeds unique names (name_prefix)
Autoscaler owns a fieldignore_changesStop plan noise
Critical data storesprevent_destroyPlan fails instead of deleting

Modules

hcl
module "vpc" {
  source  = "terraform-aws-modules/vpc/aws"     # registry
  version = "~> 6.0"
  name    = "main"
  cidr    = "10.0.0.0/16"
}

module "web" {
  source = "../../modules/web"                  # local path
  count  = 2                                    # modules support count, for_each, depends_on
  vpc_id = module.vpc.vpc_id
}

module "net" {
  source = "git::https://github.com/org/tf-vpc.git?ref=v1.2.0"   # Git, pin a tag
}
ConventionDetail
Filesmain.tf, variables.tf, outputs.tf, versions.tf, README.md
Providersrequired_providers in modules, provider blocks only in roots
InterfaceEvery variable and output has a description, variables have type
VersioningSemantic version tags, pin consumers, upgrade dev first
Referencing outputsmodule.<name>.<output>
Update modulesterraform init -upgrade or terraform get -update

Registry source format is <namespace>/<name>/<provider>. Registry modules take version, Git modules take ?ref=.

Backends and locking

hcl
terraform {
  backend "s3" {
    bucket       = "tfstate-123456789012-ap-south-1"
    key          = "web-platform/prod/terraform.tfstate"
    region       = "ap-south-1"
    encrypt      = true
    use_lockfile = true            # native S3 locking, Terraform 1.11+
    # kms_key_id = "alias/tfstate"
    # dynamodb_table = "tf-locks"  # legacy locking, deprecated in favour of use_lockfile
  }
}
ItemDetail
Lock object<key>.tflock next to the state file
Needed S3 permissionss3:ListBucket, s3:GetObject, s3:PutObject, s3:DeleteObject (the lock file is written and deleted)
Bucket hardeningVersioning on, SSE on, public access block, TLS-only policy
Cross-account accessAssume a role: assume_role = { role_arn = "..." } inside the backend block
Reading another stackdata "terraform_remote_state" (exposes all outputs) or SSM parameters
Backends cannot use variablesUse -backend-config or a backend.hcl file
bash
terraform init -backend-config=backend.hcl
terraform init -migrate-state          # move local state to the new backend

Import, move and remove

hcl
import {
  to = aws_s3_bucket.legacy
  id = "my-existing-bucket"
}

import {
  for_each = var.subnet_ids
  to       = aws_subnet.imported[each.key]
  id       = each.value
}

moved {
  from = aws_instance.web
  to   = module.web.aws_instance.this
}

removed {
  from = aws_instance.old
  lifecycle { destroy = false }       # drop from state, keep in AWS
}
bash
terraform plan -generate-config-out=generated.tf     # draft code for import blocks
terraform import aws_s3_bucket.legacy my-existing-bucket   # legacy CLI form
terraform apply -replace=aws_instance.web            # recreate one resource

Environment variables

VariableMeaning
TF_VAR_name=valueSet input variable name
TF_LOG=TRACE|DEBUG|INFO|WARN|ERROREnable logging
TF_LOG_PATH=./tf.logWrite logs to a file
TF_IN_AUTOMATION=trueTrim "next steps" hints in output
TF_INPUT=falseSame as -input=false
TF_CLI_ARGS_plan="-lock-timeout=60s"Default args for a subcommand
TF_PLUGIN_CACHE_DIR=$HOME/.terraform.d/plugin-cacheShare provider downloads between projects
TF_WORKSPACE=prodSelect a workspace
AWS_PROFILE, AWS_REGIONAWS credentials and region

Variable precedence, lowest to highest: defaults, TF_VAR_*, terraform.tfvars, *.auto.tfvars (alphabetical), then -var and -var-file in command-line order.

Quality gates

bash
terraform fmt -check -recursive -diff
terraform validate
tflint --init && tflint --recursive --config "$(pwd)/.tflint.hcl"
checkov -d . --framework terraform --compact --quiet
checkov -d . -c CKV_AWS_79,CKV_AWS_8          # run selected checks
checkov -d . --skip-check CKV_AWS_91          # skip with a reason in review
trivy config .                                 # alternative IaC scanner
terraform-docs markdown table . > README.md    # generate module docs
infracost breakdown --path .                   # cost estimate
ToolCatches
fmtStyle drift
validateSyntax, wrong argument names, bad references
tflintInvalid instance types, deprecated syntax, unused declarations, naming
checkov / trivyInsecure settings: open security groups, unencrypted storage, IMDSv1
conftest (OPA) / SentinelOrganisation rules evaluated on the plan JSON
infracostCost change per PR
pre-commitRuns the above before the commit lands

Inline skip for one resource: #checkov:skip=CKV_AWS_260:reason.

Testing

hcl
# tests/basic.tftest.hcl
mock_provider "aws" {}

variables {
  name = "demo"
}

run "encryption_enabled" {
  command = plan
  assert {
    condition     = aws_s3_bucket_server_side_encryption_configuration.this.rule[0].apply_server_side_encryption_by_default[0].sse_algorithm == "AES256"
    error_message = "Bucket must be encrypted."
  }
}

run "rejects_bad_name" {
  command = plan
  variables { name = "UPPER" }
  expect_failures = [var.name]
}
bash
terraform test                         # all tests
terraform test -filter=tests/basic.tftest.hcl
terraform test -verbose

GitHub Actions snippets

yaml
permissions:
  id-token: write       # required for OIDC
  contents: read

steps:
  - uses: actions/checkout@v4
  - uses: hashicorp/setup-terraform@v3
    with: { terraform_version: "1.13.x", terraform_wrapper: false }
  - uses: aws-actions/configure-aws-credentials@v4
    with:
      role-to-assume: ${{ vars.AWS_PLAN_ROLE_ARN }}
      aws-region: ap-south-1
  - run: terraform init -input=false -backend-config="bucket=${{ vars.TF_STATE_BUCKET }}"
  - run: terraform plan -input=false -no-color -detailed-exitcode -out=tfplan

OIDC trust sub values: repo:ORG/REPO:pull_request, repo:ORG/REPO:ref:refs/heads/main, repo:ORG/REPO:environment:prod. Gate prod with a GitHub environment that has required reviewers, and serialise runs with concurrency: { group: tf-prod, cancel-in-progress: false }.

Debugging

SymptomCommand or action
Need provider API detailTF_LOG=DEBUG terraform plan 2> debug.log
Unsure what an expression returnsterraform console then type the expression
Plan wants to replace something unexpectedlyRead the line marked # forces replacement; check ignore_changes or a renamed attribute
Error acquiring the state lockCheck who holds it in the message, then terraform force-unlock <ID> only if dead
Saved plan is staleCreate a new plan, state changed after it was saved
Resource exists in AWS but not in stateimport block
Resource in state but deleted in AWSterraform plan shows re-create; or state rm if intentionally gone
Provider hash mismatchterraform providers lock -platform=... and commit the lock file
Slow plansSplit state, raise -parallelism, use -target only to diagnose
Cycle errorBreak the circular reference, often by using separate rule resources

Version constraints

ConstraintMeaning
= 6.1.0 or 6.1.0Exactly this version
>= 6.0This or newer (includes next major, risky)
~> 6.0>= 6.0, < 7.0 (allows minor and patch)
~> 6.1.0>= 6.1.0, < 6.2.0 (patch only)
>= 6.0, < 7.0Explicit range

Rule of thumb: roots use ~> on providers and commit the lock file, modules use >= lower bounds so consumers can choose.

Top 15 interview one-liners

  1. What is Terraform? A declarative, provider-based tool that builds a dependency graph from your config, diffs it against state, and calls cloud APIs to make reality match.
  2. Why state? It maps code addresses to real resource IDs and caches attributes, so Terraform knows what it owns and what to change.
  3. Why remote state with locking? Teams need one shared source of truth and a lock so two applies cannot corrupt it. On S3 use versioning, encryption and use_lockfile.
  4. plan versus apply? plan is a read-only diff. apply executes it. In CI we save the plan with -out and apply that exact file.
  5. count versus for_each? count addresses by index and shifts when items are removed. for_each uses stable keys, so we default to it.
  6. How do you handle secrets? Keep them out of code and Git, mark inputs sensitive, fetch from Secrets Manager or SSM at runtime, and remember they can still appear in state, so lock state down.
  7. How do you structure environments? Separate root modules per environment sharing versioned modules, each with its own state key, rather than workspaces for dev and prod.
  8. What is drift and how do you find it? Real infrastructure differing from state or code. plan -refresh-only shows it, and a scheduled plan with -detailed-exitcode alerts on it.
  9. How do you adopt existing infrastructure? An import block plus plan -generate-config-out, then clean the code until the plan shows no changes.
  10. How do you rename or refactor without downtime? Use moved blocks so the plan shows a move instead of destroy and create.
  11. What does terraform destroy actually do, and how do you protect against it? It deletes everything in that state. Use prevent_destroy, deletion protection on resources, scoped IAM, and approval gates.
  12. What do tflint and checkov do? tflint validates provider-specific correctness and conventions. checkov scans for insecure configuration. Both run on every PR before the plan is read.
  13. How does CI authenticate to AWS? OIDC federation with short-lived tokens and separate roles: read-only for PR plans, scoped write for apply restricted to a protected environment.
  14. How do you version and share modules? Semantic version tags in Git or a registry, pinned by consumers, upgraded dev first, and moved blocks to keep upgrades non-destructive.
  15. A prod apply failed halfway. What now? Do not panic and do not re-run blindly. Read the error, check state with state list and the console, fix the cause, and run plan again. Terraform is idempotent, and the bucket's state versioning is the safety net if state itself is bad.

Feedback