Skip to content

Cheat sheet: Terraform & Helm

Terraform plan/apply/state and Helm install/upgrade/rollback, flag by flag.

Index
  • #cheatsheet
  • #terraform
  • #helm
Updated

Also:tf · helm upgrade · atomic · rollback-on-failure · tfplan

Each entry shows the command, a copy button, and what every part does: sub-command, flag, pipe, redirect and placeholder, in the order they appear. <placeholders> are the bits you replace.

54 items
Click a command to see what each part does.

Terraform: everyday workflow8

  • terraform init -upgradeInitialise and upgrade providers

    What each part does

    1. initdownload providers/modules and configure the backend
    2. -upgradepick the newest versions allowed by the constraints
  • terraform fmt -recursive -check && terraform validateFormat and validate

    What each part does

    1. fmt -recursiveformat .tf files in sub-folders too
    2. -checkdon't change files; exit non-zero if formatting is needed (CI)
    3. validatecheck syntax and internal consistency (no API calls)
  • terraform plan -var-file=<env>.tfvars -out=tfplanPlan with a var file and save the plan

    What each part does

    1. plancompute what would change
    2. -var-file=<env>.tfvarsload variable values from a file (repeatable)
    3. -out=tfplansave this exact plan to apply later (contains values in clear text: keep it private)
  • terraform apply tfplanApply exactly the reviewed planCaution

    What each part does

    1. apply tfplanapply the saved plan with no re-planning and no prompt
  • terraform plan -input=false -no-color -detailed-exitcode -out=tfplanCI-friendly plan: did anything change?

    What each part does

    1. -input=falsenever prompt for variables
    2. -no-colorplain output for logs
    3. -detailed-exitcodeexit 0 = no changes, 1 = error, 2 = changes present
  • terraform plan -target=<address> -out=tfplanPlan only one resource (exceptional cases)

    What each part does

    1. -target=<address>limit to that resource and its dependencies (use sparingly)
  • terraform plan -replace=<address> -out=tfplanForce replacement of one resourceCaution

    What each part does

    1. -replace=<address>plan to destroy and recreate it (successor of taint)
  • terraform -chdir=<dir> planRun in another folder without cd

    What each part does

    1. -chdir=<dir>global option, goes before the subcommand

Terraform: state6

  • terraform state list | grep <name>Find a resource in state

    What each part does

    1. state listevery resource address in the state
    2. | grep <name>keep matching addresses
  • terraform state show <address>Inspect one resource

    What each part does

    1. state show <address>all attributes stored for that resource
  • terraform state mv <old-address> <new-address>Rename/move without destroyingCaution

    What each part does

    1. state mvchange the address in state (e.g. after refactoring into a module)

    Tip Newer alternative: a moved {} block in code.

  • terraform state rm <address>Stop managing a resource (keep it in the cloud)Caution

    What each part does

    1. state rmforget the resource; the real object is NOT deleted
  • terraform output -raw <name>Read an output for scripts

    What each part does

    1. output <name>print an output value
    2. -rawplain string without quotes (for shell variables)
  • terraform force-unlock <lock-id>Release a stuck lockDestructive

    What each part does

    1. force-unlock <lock-id>remove a lock left by a crashed run (ID is in the error message)

    Tip Make sure nothing else is running against this state.

Terraform: workspaces, import & more15

  • terraform workspace list; terraform workspace new <name>; terraform workspace select <name>List, create and switch workspaces

    What each part does

    1. workspace listworkspaces; * marks the current one
    2. workspace newnew empty state for the same code
    3. workspace selectswitch
  • terraform plan -generate-config-out=generated.tfImport an existing resource (block, Terraform 1.5+)

    What each part does

    1. import block in codeimport { to = aws_s3_bucket.logs id = "my-logs" }
    2. -generate-config-outwrite matching resource code for you to review
  • terraform import <address> <id>Import an existing resource (CLI)

    What each part does

    1. importrecord a real resource in state
    2. <address>resource in your code, e.g. aws_s3_bucket.logs
    3. <id>the provider's ID for it
  • terraform plan -destroy -out=destroy.tfplanPlan destroying everythingDestructive

    What each part does

    1. -destroyplan to delete every managed resource
    2. -outsave it; apply with terraform apply destroy.tfplan
  • terraform destroy -var-file=<env>.tfvarsDestroy everythingDestructive

    What each part does

    1. destroydelete all resources in this state (asks to confirm)
    2. -var-filesame variables as for apply
  • terraform apply -refresh-onlySync state with reality without changing anythingCaution

    What each part does

    1. -refresh-onlyupdate state from the real infrastructure (drift), no resource changes; replaces terraform refresh
  • terraform consoleTry expressions interactively

    What each part does

    1. consoleREPL to evaluate expressions, functions and values from state, e.g. cidrsubnet("10.0.0.0/16", 8, 1)
  • terraform output -json | jqOutputs as JSON

    What each part does

    1. output -jsonevery output with type and sensitive flag
    2. | jqpretty-print or pick values
  • terraform show -json tfplan | jq '.resource_changes[] | {address, actions: .change.actions}'Show a saved plan or state in JSON

    What each part does

    1. show -json tfplanmachine-readable plan
    2. jq '...'list each resource and its actions (create, update, delete)
  • terraform state pull > state-backup.tfstateDownload / back up the remote state

    What each part does

    1. state pullprint the current remote state
    2. > filesave a copy before risky state surgery
  • terraform providers; terraform providers lock -platform=linux_amd64 -platform=darwin_arm64 -platform=windows_amd64Providers and lock file for all platforms

    What each part does

    1. providersprovider requirements per module
    2. providers lock -platform=...record checksums for every OS your team and CI use
  • terraform versionVersions of Terraform and providers

    What each part does

    1. versionTerraform version and the provider versions selected in this directory
  • terraform graph | dot -Tsvg > graph.svgDependency graph

    What each part does

    1. graphresource dependencies in DOT format
    2. dot -Tsvgrender with Graphviz
  • terraform testRun Terraform tests

    What each part does

    1. testrun *.tftest.hcl files: plan/apply assertions against your module
  • terraform init -upgrade -backend=falseUnlock a provider upgrade in one module

    What each part does

    1. -upgradenewest allowed provider versions
    2. -backend=falsedon't touch the backend (fast, for local checks)

Helm9

  • helm repo add <name> <url> && helm repo updateAdd a chart repo and refresh

    What each part does

    1. repo add <name> <url>register a chart repository
    2. repo updaterefresh the local index of all repos
  • helm show values <repo>/<chart> > values.yamlLook at a chart's default values

    What each part does

    1. show valuesprint the chart's values.yaml
    2. > values.yamlsave it as a starting point
  • helm template <release> <chart> -f <values-file> | lessRender templates locally (no cluster)

    What each part does

    1. templaterender manifests locally
    2. -f <values-file>your values (repeatable; later files win)
    3. | lesspage through the output
  • helm upgrade --install <release> <chart> -n <namespace> --create-namespace -f <values-file> --set <key>=<value> --wait --timeout 5mInstall or upgrade in one command

    What each part does

    1. upgrade --installupgrade if the release exists, otherwise install (-i)
    2. -n <namespace>target namespace
    3. --create-namespacecreate it if missing (with --install)
    4. -f <values-file>values file
    5. --set <key>=<value>override one value on the command line (wins over -f)
    6. --waitwait until resources are ready
    7. --timeout 5mhow long to wait
  • helm upgrade --install <release> <chart> --rollback-on-failure --timeout 5mRoll back automatically if the upgrade fails

    What each part does

    1. --rollback-on-failureHelm 4: on failure, roll back to the last good release (implies --wait)

    Tip Helm 3 calls this flag --atomic.

  • helm history <release> -n <namespace>; helm rollback <release> <revision> -n <namespace>Release history and rollbackCaution

    What each part does

    1. historyrevisions with status and chart/app versions
    2. rollback <release> <revision>return to that revision (omit it for the previous one)
  • helm list -A | grep <release>Find a release in any namespace

    What each part does

    1. list -Areleases in all namespaces
    2. | grep <release>keep the matching row
  • helm get values <release> -n <namespace> --allValues actually used by a release

    What each part does

    1. get valuesuser-supplied values of the deployed release
    2. --allinclude chart defaults too
  • helm uninstall <release> -n <namespace>Remove a releaseDestructive

    What each part does

    1. uninstalldelete all resources of the release (PVCs may stay)

Helm: charts, repos & debugging16

  • helm search repo <keyword> --versions; helm search hub <keyword>Search charts in added repos / Artifact Hub

    What each part does

    1. search repocharts in your added repos
    2. --versionsevery chart version, not only the latest
    3. search hubsearch Artifact Hub
  • helm pull <repo>/<chart> --version <version> --untarDownload and unpack a chart

    What each part does

    1. pulldownload the chart .tgz
    2. --versionspecific chart version
    3. --untarunpack to a folder to read templates
  • helm show chart <repo>/<chart>; helm show readme <repo>/<chart>Show a chart's metadata and README

    What each part does

    1. show chartChart.yaml: version, appVersion, dependencies
    2. show readmedocumentation
  • helm upgrade --install <release> oci://<registry>/<path>/<chart> --version <version> -n <namespace>Install a chart from an OCI registry

    What each part does

    1. oci://...charts stored in a container registry (no helm repo add)
    2. --versionchart version
  • helm status <release> -n <namespace>Status and notes of a release

    What each part does

    1. statusdeployed / failed / pending-upgrade, revision, and the chart's NOTES
  • helm get manifest <release> -n <namespace>What did Helm actually deploy?

    What each part does

    1. get manifestthe rendered YAML of the current revision (--revision <n> for older)
  • helm list -A --pending --failedReleases stuck or failed

    What each part does

    1. -Aall namespaces
    2. --pending --failedonly releases in those states (pending-upgrade blocks new upgrades)
  • helm upgrade <release> <chart> -f <values-file> --dry-run=server --debug -n <namespace>Preview an upgrade without applying

    What each part does

    1. --dry-run=serverrender and validate against the cluster, change nothing
    2. --debugprint the rendered manifests
  • helm diff upgrade <release> <chart> -f <values-file> -n <namespace>Diff an upgrade (helm-diff plugin)

    What each part does

    1. helm diff upgradecolored diff between the live release and what the upgrade would deploy (helm plugin install https://github.com/databus23/helm-diff)
  • helm upgrade <release> <chart> --reuse-values --set <key>=<value>Keep current values on upgradeCaution

    What each part does

    1. --reuse-valuesstart from the last release's values
    2. --setchange only this one

    Tip Can hide new chart defaults; --reset-then-reuse-values is safer on Helm 3.14+.

  • helm create <chart>Create a new chart

    What each part does

    1. helm createscaffold a chart: Chart.yaml, values.yaml, templates
  • helm lint <chart-dir> -f <values-file>Lint a chart

    What each part does

    1. lintcheck the chart and rendered templates for errors and best practices
  • helm dependency update <chart-dir>Fetch chart dependencies

    What each part does

    1. dependency updatedownload the subcharts listed in Chart.yaml into charts/ and update Chart.lock
  • helm package <chart-dir> && helm push <chart>-<version>.tgz oci://<registry>/<path>Package and push a chart to an OCI registry

    What each part does

    1. packagebuild <chart>-<version>.tgz
    2. push ... oci://upload to the registry (helm registry login first)
  • helm test <release> -n <namespace>Run a release's tests

    What each part does

    1. helm testrun the chart's test hook pods and report pass/fail
  • helm plugin listInstalled Helm plugins

    What each part does

    1. plugin liste.g. diff, secrets, unittest

My DevOps Playbook by Angelo John Calleja

New runbook

GitHub not connected

Saving is handled by the site: the GitHub token is stored in Cloudflare, not in your browser. Enter the editor password (the EDITOR_PASSWORD secret) to unlock saving in this browser.

Saving commits to gelogrammer/My_DevOps_Playbook. Create a fine-grained token with access to only this repo andContents: Read and write, then paste it here. It is stored only in this browser.

src/content/docs/…