DevOpsLesson
DOCKERKUBERNETESTERRAFORMAWSCI/CDLINUXGITDEVOPS ROADMAPCLOUD ROADMAPSRE ROADMAPGIT CHEATSHEETDOCKER CHEATSHEETK8S CHEATSHEETTF CHEATSHEETLINUX CHEATSHEETDOCKERFILE LINTERYAML VALIDATORCRON PARSERREGEX TESTER
DevOpsLesson

Free, comprehensive DevOps tutorials and learning roadmaps. Master Docker, Kubernetes, CI/CD, and more.

Stay Updated

Get notified about new tutorials and features.

Tutorials

  • What is DevOps?
  • Docker Tutorial
  • Terraform Tutorial
  • CI/CD Pipeline
  • All Tutorials

Roadmaps

  • DevOps Engineer
  • Cloud Engineer
  • SRE Path
  • All Roadmaps

Company

  • About Us
  • Blog
  • Contact
  • Privacy Policy
  • Terms of Service

© 2026 DevOpsLesson. All rights reserved.

DOCKERKUBERNETESTERRAFORMAWSCI/CDLINUXGITDEVOPS ROADMAPCLOUD ROADMAPSRE ROADMAPGIT CHEATSHEETDOCKER CHEATSHEETK8S CHEATSHEETTF CHEATSHEETLINUX CHEATSHEETDOCKERFILE LINTERYAML VALIDATORCRON PARSERREGEX TESTER

Terraform Cheatsheet

Showing 64 of 64 commands

Core Workflow

Initialize providers, validate configuration, preview changes and apply infrastructure safely

13 commands
terraform initbeginner

Initialize the working directory, download providers, and configure the backend.

terraform init -upgradeintermediate

Reinitialize the directory and upgrade providers and modules to newer allowed versions.

terraform init -backend-config=backend.hclintermediate

Initialize Terraform using backend settings from an external configuration file.

terraform validatebeginner

Check whether the current Terraform configuration is syntactically valid and internally consistent.

terraform fmtbeginner

Format Terraform files in the current directory to the canonical style.

terraform fmt -recursivebeginner

Format all Terraform files recursively across modules and environment folders.

terraform planbeginner

Create an execution plan showing what Terraform will add, change, or destroy.

terraform plan -out=tfplanintermediate

Save the generated plan to a file for later review or controlled apply.

terraform applybeginner

Apply the current execution plan and reconcile infrastructure to the configuration.

terraform apply tfplanintermediate

Apply a previously saved plan file to ensure exactly reviewed changes are executed.

terraform apply -auto-approveintermediate

Apply changes without an interactive approval prompt.

terraform destroyintermediate

Generate and execute a plan that destroys all managed resources in the current workspace.

terraform destroy -auto-approveadvanced

Destroy all managed resources without asking for confirmation.

State Management

Inspect, repair, move and import resources in Terraform state with precision

10 commands
terraform state listbeginner

List all resources currently tracked in the Terraform state file.

terraform state show <resource>intermediate

Display the attributes of a single resource as stored in state.

terraform state mv <source> <dest>advanced

Move a resource address in state without recreating the underlying infrastructure.

terraform state rm <resource>advanced

Remove a resource from state without destroying the real infrastructure object.

terraform state pullintermediate

Download and print the latest remote state to standard output.

terraform state pushadvanced

Upload a local state file to the configured backend.

terraform state replace-provider <old> <new>advanced

Rewrite provider references in state when migrating resources to a new provider source.

terraform import <resource_type.name> <id>advanced

Import an existing infrastructure object into Terraform state under a declared resource.

terraform refreshintermediate

Update state with the real infrastructure data from providers without changing configuration.

terraform force-unlock <lock-id>advanced

Manually remove a state lock when a previous run exited unexpectedly.

Workspace

Separate state for environments such as dev, staging and production

5 commands
terraform workspace listbeginner

List all available workspaces and mark the currently selected one.

terraform workspace new <name>beginner

Create a new workspace with isolated state for a new environment.

terraform workspace select <name>beginner

Switch the active workspace for future plan and apply operations.

terraform workspace showbeginner

Print the name of the currently selected workspace.

terraform workspace delete <name>intermediate

Delete an unused workspace after its state is no longer needed.

Variables & Outputs

Pass environment-specific inputs and extract output values for downstream tooling

7 commands
terraform plan -var="key=value"beginner

Pass a single input variable inline while generating a plan.

terraform plan -var-file=prod.tfvarsbeginner

Load input variables from a tfvars file for a specific environment.

terraform apply -var="environment=production"intermediate

Apply infrastructure changes using an inline variable override.

terraform outputbeginner

Show all root module outputs after an apply completes.

terraform output <name>beginner

Print a single named output value from the current state.

terraform output -jsonintermediate

Render all outputs as JSON for scripts, pipelines or tooling integrations.

terraform output -raw <name>intermediate

Print a single output as raw text without quotes or JSON formatting.

Modules

Download, update and selectively work with reusable Terraform modules

5 commands
terraform getbeginner

Download child modules referenced by the current configuration.

terraform get -updateintermediate

Re-download child modules and update them to newer allowed versions.

terraform initbeginner

Initialize the working directory and download required providers and modules.

terraform plan -target=module.<name>advanced

Preview changes for a specific module without planning the whole stack.

terraform apply -target=module.<name>advanced

Apply changes only for a selected module when performing controlled partial operations.

Targeting & Partial Apply

Scope Terraform operations to specific resources when troubleshooting or recovering safely

6 commands
terraform plan -target=aws_instance.webadvanced

Create a plan limited to a specific resource address.

terraform apply -target=aws_instance.webadvanced

Apply only the changes needed for a single targeted resource.

terraform destroy -target=aws_instance.webadvanced

Destroy one targeted resource without tearing down the entire workspace.

terraform apply -replace=aws_instance.webadvanced

Force Terraform to replace a specific resource during apply.

terraform plan -refresh=falseintermediate

Generate a plan without refreshing remote object state first.

terraform apply -parallelism=20advanced

Control how many resource operations Terraform performs concurrently during apply.

Debugging & Logging

Trace provider behavior, inspect plans and visualize dependency graphs during incident response

11 commands
TF_LOG=DEBUG terraform planintermediate

Run a plan with debug logging enabled for provider and backend troubleshooting.

TF_LOG=TRACE terraform applyadvanced

Run an apply with full trace logging to diagnose complex provider behavior.

TF_LOG_PATH=terraform.log terraform planintermediate

Write Terraform debug logs to a file instead of flooding standard output.

terraform showbeginner

Display the current state or a human-readable representation of Terraform data.

terraform show tfplanintermediate

Render the contents of a saved plan file for review before apply.

terraform show -json tfplan | jq .advanced

Convert a saved plan to JSON for machine analysis, policy checks or drift tooling.

terraform graphintermediate

Generate a dependency graph of resources and modules in DOT format.

terraform graph | dot -Tpng > graph.pngadvanced

Render the Terraform dependency graph to a PNG for architecture review.

terraform versionbeginner

Show the installed Terraform CLI version and platform details.

terraform providersintermediate

List the providers required by the current configuration and modules.

terraform providers lockadvanced

Generate or update dependency lock entries for provider checksums across platforms.

Cloud & Registry

Authenticate with Terraform Cloud and manage remote operations and legacy lifecycle actions

7 commands
terraform loginbeginner

Authenticate the Terraform CLI with Terraform Cloud or HCP Terraform.

terraform logoutbeginner

Remove locally stored Terraform Cloud credentials for the current hostname.

terraform whoamiintermediate

Show the Terraform Cloud user associated with the current authenticated token.

terraform workspace listbeginner

List remote workspaces when using a Terraform Cloud or remote backend.

terraform force-unlockadvanced

Release a stuck remote state lock after verifying no run is still active.

terraform taint <resource>advanced

Mark a resource as tainted so Terraform recreates it on the next apply.

terraform untaint <resource>advanced

Remove the tainted marker from a resource so Terraform stops forcing replacement.

Terraform Cheatsheet

Production-ready Terraform CLI reference for daily IaC work - initialize backends, validate modules, inspect state, manage workspaces, target resources safely, and debug plans with real-world examples and copyable commands.

devopslesson.com/tools/cheatsheets/terraform

64 commands8 categories

Core Workflow

13 commands
terraform initbeginner

Initialize the working directory, download providers, and configure the backend.

terraform init

💡 Run this first after cloning a repo or changing provider and backend settings.

terraform init -upgradeintermediate

Reinitialize the directory and upgrade providers and modules to newer allowed versions.

terraform init -upgrade
terraform init -backend-config=backend.hclintermediate

Initialize Terraform using backend settings from an external configuration file.

terraform init -backend-config=prod.backend.hcl
terraform validatebeginner

Check whether the current Terraform configuration is syntactically valid and internally consistent.

terraform validate

💡 Use this in CI before planning to catch broken references fast.

terraform fmtbeginner

Format Terraform files in the current directory to the canonical style.

terraform fmt
terraform fmt -recursivebeginner

Format all Terraform files recursively across modules and environment folders.

terraform fmt -recursive
terraform planbeginner

Create an execution plan showing what Terraform will add, change, or destroy.

terraform plan

💡 Review plans carefully in production to avoid unintended replacements.

terraform plan -out=tfplanintermediate

Save the generated plan to a file for later review or controlled apply.

terraform plan -out=prod.tfplan
terraform applybeginner

Apply the current execution plan and reconcile infrastructure to the configuration.

terraform apply
terraform apply tfplanintermediate

Apply a previously saved plan file to ensure exactly reviewed changes are executed.

terraform apply prod.tfplan

💡 Use saved plans in deployment pipelines to separate review from execution.

terraform apply -auto-approveintermediate

Apply changes without an interactive approval prompt.

terraform apply -auto-approve
terraform destroyintermediate

Generate and execute a plan that destroys all managed resources in the current workspace.

terraform destroy

💡 Always verify the active workspace before destroying shared environments.

terraform destroy -auto-approveadvanced

Destroy all managed resources without asking for confirmation.

terraform destroy -auto-approve

State Management

10 commands
terraform state listbeginner

List all resources currently tracked in the Terraform state file.

terraform state list

💡 Pair this with grep when auditing large stacks for a specific module or resource type.

terraform state show <resource>intermediate

Display the attributes of a single resource as stored in state.

terraform state show aws_instance.web
terraform state mv <source> <dest>advanced

Move a resource address in state without recreating the underlying infrastructure.

terraform state mv aws_instance.web module.compute.aws_instance.web

💡 Use this during refactors to preserve resources when introducing modules.

terraform state rm <resource>advanced

Remove a resource from state without destroying the real infrastructure object.

terraform state rm aws_security_group.legacy
terraform state pullintermediate

Download and print the latest remote state to standard output.

terraform state pull
terraform state pushadvanced

Upload a local state file to the configured backend.

terraform state push migrated-prod.tfstate

💡 Back up state before pushing and coordinate carefully to avoid overwriting newer remote state.

terraform state replace-provider <old> <new>advanced

Rewrite provider references in state when migrating resources to a new provider source.

terraform state replace-provider hashicorp/aws registry.acme.corp/acme/aws
terraform import <resource_type.name> <id>advanced

Import an existing infrastructure object into Terraform state under a declared resource.

terraform import aws_s3_bucket.logs company-prod-logs

💡 Define the resource block first so the imported object can be managed cleanly afterward.

terraform refreshintermediate

Update state with the real infrastructure data from providers without changing configuration.

terraform refresh
terraform force-unlock <lock-id>advanced

Manually remove a state lock when a previous run exited unexpectedly.

terraform force-unlock 4d2a9fd9-1b2c-4f18-9b44-2f1f4c8d8c11

💡 Only unlock after confirming no active plan or apply is still using the state.

Workspace

5 commands
terraform workspace listbeginner

List all available workspaces and mark the currently selected one.

terraform workspace list
terraform workspace new <name>beginner

Create a new workspace with isolated state for a new environment.

terraform workspace new staging

💡 Standardize workspace names across repositories so automation stays predictable.

terraform workspace select <name>beginner

Switch the active workspace for future plan and apply operations.

terraform workspace select production
terraform workspace showbeginner

Print the name of the currently selected workspace.

terraform workspace show
terraform workspace delete <name>intermediate

Delete an unused workspace after its state is no longer needed.

terraform workspace delete feature-preview-42

Variables & Outputs

7 commands
terraform plan -var="key=value"beginner

Pass a single input variable inline while generating a plan.

terraform plan -var="instance_type=t3.medium"
terraform plan -var-file=prod.tfvarsbeginner

Load input variables from a tfvars file for a specific environment.

terraform plan -var-file=production.tfvars

💡 Keep common defaults in variables and environment overrides in versioned tfvars files.

terraform apply -var="environment=production"intermediate

Apply infrastructure changes using an inline variable override.

terraform apply -var="environment=production"
terraform outputbeginner

Show all root module outputs after an apply completes.

terraform output
terraform output <name>beginner

Print a single named output value from the current state.

terraform output alb_dns_name
terraform output -jsonintermediate

Render all outputs as JSON for scripts, pipelines or tooling integrations.

terraform output -json

💡 Prefer JSON output when feeding values into deployment scripts or automation.

terraform output -raw <name>intermediate

Print a single output as raw text without quotes or JSON formatting.

terraform output -raw kubeconfig_path

Modules

5 commands
terraform getbeginner

Download child modules referenced by the current configuration.

terraform get
terraform get -updateintermediate

Re-download child modules and update them to newer allowed versions.

terraform get -update

💡 Review module changelogs before updating shared VPC, EKS or networking modules.

terraform initbeginner

Initialize the working directory and download required providers and modules.

terraform init
terraform plan -target=module.<name>advanced

Preview changes for a specific module without planning the whole stack.

terraform plan -target=module.vpc
terraform apply -target=module.<name>advanced

Apply changes only for a selected module when performing controlled partial operations.

terraform apply -target=module.vpc

💡 Targeting is useful for break-glass fixes, but a full follow-up plan should always be reviewed.

Targeting & Partial Apply

6 commands
terraform plan -target=aws_instance.webadvanced

Create a plan limited to a specific resource address.

terraform plan -target=aws_instance.web
terraform apply -target=aws_instance.webadvanced

Apply only the changes needed for a single targeted resource.

terraform apply -target=aws_instance.web

💡 Use targeting sparingly because it can bypass normal dependency evaluation across the stack.

terraform destroy -target=aws_instance.webadvanced

Destroy one targeted resource without tearing down the entire workspace.

terraform destroy -target=aws_instance.web
terraform apply -replace=aws_instance.webadvanced

Force Terraform to replace a specific resource during apply.

terraform apply -replace=aws_instance.web

💡 Prefer -replace over taint for explicit, reviewable replacement workflows.

terraform plan -refresh=falseintermediate

Generate a plan without refreshing remote object state first.

terraform plan -refresh=false
terraform apply -parallelism=20advanced

Control how many resource operations Terraform performs concurrently during apply.

terraform apply -parallelism=20

Debugging & Logging

11 commands
TF_LOG=DEBUG terraform planintermediate

Run a plan with debug logging enabled for provider and backend troubleshooting.

TF_LOG=DEBUG terraform plan

💡 DEBUG is usually detailed enough without the noise and size of TRACE logs.

TF_LOG=TRACE terraform applyadvanced

Run an apply with full trace logging to diagnose complex provider behavior.

TF_LOG=TRACE terraform apply
TF_LOG_PATH=terraform.log terraform planintermediate

Write Terraform debug logs to a file instead of flooding standard output.

TF_LOG_PATH=terraform.log terraform plan
terraform showbeginner

Display the current state or a human-readable representation of Terraform data.

terraform show
terraform show tfplanintermediate

Render the contents of a saved plan file for review before apply.

terraform show prod.tfplan
terraform show -json tfplan | jq .advanced

Convert a saved plan to JSON for machine analysis, policy checks or drift tooling.

terraform show -json prod.tfplan | jq .resource_changes[]

💡 JSON plans are ideal for CI gates, cost estimation and policy-as-code checks.

terraform graphintermediate

Generate a dependency graph of resources and modules in DOT format.

terraform graph
terraform graph | dot -Tpng > graph.pngadvanced

Render the Terraform dependency graph to a PNG for architecture review.

terraform graph | dot -Tpng > graph.png
terraform versionbeginner

Show the installed Terraform CLI version and platform details.

terraform version
terraform providersintermediate

List the providers required by the current configuration and modules.

terraform providers
terraform providers lockadvanced

Generate or update dependency lock entries for provider checksums across platforms.

terraform providers lock -platform=darwin_amd64 -platform=linux_amd64

💡 Commit provider lock files so teams and CI use verified provider builds consistently.

Cloud & Registry

7 commands
terraform loginbeginner

Authenticate the Terraform CLI with Terraform Cloud or HCP Terraform.

terraform login

💡 This stores an API token locally so remote runs and private module access can succeed.

terraform logoutbeginner

Remove locally stored Terraform Cloud credentials for the current hostname.

terraform logout
terraform whoamiintermediate

Show the Terraform Cloud user associated with the current authenticated token.

terraform whoami
terraform workspace listbeginner

List remote workspaces when using a Terraform Cloud or remote backend.

terraform workspace list
terraform force-unlockadvanced

Release a stuck remote state lock after verifying no run is still active.

terraform force-unlock 7d4b3770-6f9a-4e94-8e75-5ab49d5d441c
terraform taint <resource>advanced

Mark a resource as tainted so Terraform recreates it on the next apply.

terraform taint aws_instance.web

💡 Deprecated in newer workflows; prefer terraform apply -replace for explicit replacement intent.

terraform untaint <resource>advanced

Remove the tainted marker from a resource so Terraform stops forcing replacement.

terraform untaint aws_instance.web
devopslesson.comFree DevOps Cheatsheets · Tutorials · Roadmaps