Terraform Cheatsheet
Infrastructure as Code reference: terraform state locking, CLI commands, variable files, workspace switching, and module patterns.
Core Workflow
13 commandsterraform initbeginnerInitialize the working directory, download providers, and configure the backend.
terraform init -upgradeintermediateReinitialize the directory and upgrade providers and modules to newer allowed versions.
terraform init -backend-config=backend.hclintermediateInitialize Terraform using backend settings from an external configuration file.
terraform validatebeginnerCheck whether the current Terraform configuration is syntactically valid and internally consistent.
terraform fmtbeginnerFormat Terraform files in the current directory to the canonical style.
terraform fmt -recursivebeginnerFormat all Terraform files recursively across modules and environment folders.
terraform planbeginnerCreate an execution plan showing what Terraform will add, change, or destroy.
terraform plan -out=tfplanintermediateSave the generated plan to a file for later review or controlled apply.
terraform applybeginnerApply the current execution plan and reconcile infrastructure to the configuration.
terraform apply tfplanintermediateApply a previously saved plan file to ensure exactly reviewed changes are executed.
terraform apply -auto-approveintermediateApply changes without an interactive approval prompt.
terraform destroyintermediateGenerate and execute a plan that destroys all managed resources in the current workspace.
terraform destroy -auto-approveadvancedDestroy all managed resources without asking for confirmation.
State Management
10 commandsterraform state listbeginnerList all resources currently tracked in the Terraform state file.
terraform state show <resource>intermediateDisplay the attributes of a single resource as stored in state.
terraform state mv <source> <dest>advancedMove a resource address in state without recreating the underlying infrastructure.
terraform state rm <resource>advancedRemove a resource from state without destroying the real infrastructure object.
terraform state pullintermediateDownload and print the latest remote state to standard output.
terraform state pushadvancedUpload a local state file to the configured backend.
terraform state replace-provider <old> <new>advancedRewrite provider references in state when migrating resources to a new provider source.
terraform import <resource_type.name> <id>advancedImport an existing infrastructure object into Terraform state under a declared resource.
terraform refreshintermediateUpdate state with the real infrastructure data from providers without changing configuration.
terraform force-unlock <lock-id>advancedManually remove a state lock when a previous run exited unexpectedly.
Workspace
5 commandsterraform workspace listbeginnerList all available workspaces and mark the currently selected one.
terraform workspace new <name>beginnerCreate a new workspace with isolated state for a new environment.
terraform workspace select <name>beginnerSwitch the active workspace for future plan and apply operations.
terraform workspace showbeginnerPrint the name of the currently selected workspace.
terraform workspace delete <name>intermediateDelete an unused workspace after its state is no longer needed.
Variables & Outputs
7 commandsterraform plan -var="key=value"beginnerPass a single input variable inline while generating a plan.
terraform plan -var-file=prod.tfvarsbeginnerLoad input variables from a tfvars file for a specific environment.
terraform apply -var="environment=production"intermediateApply infrastructure changes using an inline variable override.
terraform outputbeginnerShow all root module outputs after an apply completes.
terraform output <name>beginnerPrint a single named output value from the current state.
terraform output -jsonintermediateRender all outputs as JSON for scripts, pipelines or tooling integrations.
terraform output -raw <name>intermediatePrint a single output as raw text without quotes or JSON formatting.
Modules
5 commandsterraform getbeginnerDownload child modules referenced by the current configuration.
terraform get -updateintermediateRe-download child modules and update them to newer allowed versions.
terraform initbeginnerInitialize the working directory and download required providers and modules.
terraform plan -target=module.<name>advancedPreview changes for a specific module without planning the whole stack.
terraform apply -target=module.<name>advancedApply changes only for a selected module when performing controlled partial operations.
Targeting & Partial Apply
6 commandsterraform plan -target=aws_instance.webadvancedCreate a plan limited to a specific resource address.
terraform apply -target=aws_instance.webadvancedApply only the changes needed for a single targeted resource.
terraform destroy -target=aws_instance.webadvancedDestroy one targeted resource without tearing down the entire workspace.
terraform apply -replace=aws_instance.webadvancedForce Terraform to replace a specific resource during apply.
terraform plan -refresh=falseintermediateGenerate a plan without refreshing remote object state first.
terraform apply -parallelism=20advancedControl how many resource operations Terraform performs concurrently during apply.
Debugging & Logging
11 commandsTF_LOG=DEBUG terraform planintermediateRun a plan with debug logging enabled for provider and backend troubleshooting.
TF_LOG=TRACE terraform applyadvancedRun an apply with full trace logging to diagnose complex provider behavior.
TF_LOG_PATH=terraform.log terraform planintermediateWrite Terraform debug logs to a file instead of flooding standard output.
terraform showbeginnerDisplay the current state or a human-readable representation of Terraform data.
terraform show tfplanintermediateRender the contents of a saved plan file for review before apply.
terraform show -json tfplan | jq .advancedConvert a saved plan to JSON for machine analysis, policy checks or drift tooling.
terraform graphintermediateGenerate a dependency graph of resources and modules in DOT format.
terraform graph | dot -Tpng > graph.pngadvancedRender the Terraform dependency graph to a PNG for architecture review.
terraform versionbeginnerShow the installed Terraform CLI version and platform details.
terraform providersintermediateList the providers required by the current configuration and modules.
terraform providers lockadvancedGenerate or update dependency lock entries for provider checksums across platforms.
Cloud & Registry
7 commandsterraform loginbeginnerAuthenticate the Terraform CLI with Terraform Cloud or HCP Terraform.
terraform logoutbeginnerRemove locally stored Terraform Cloud credentials for the current hostname.
terraform whoamiintermediateShow the Terraform Cloud user associated with the current authenticated token.
terraform workspace listbeginnerList remote workspaces when using a Terraform Cloud or remote backend.
terraform force-unlockadvancedRelease a stuck remote state lock after verifying no run is still active.
terraform taint <resource>advancedMark a resource as tainted so Terraform recreates it on the next apply.
terraform untaint <resource>advancedRemove the tainted marker from a resource so Terraform stops forcing replacement.