Showing 64 of 64 commands
Initialize providers, validate configuration, preview changes and apply infrastructure safely
terraform 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.
Inspect, repair, move and import resources in Terraform state with precision
terraform 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.
Separate state for environments such as dev, staging and production
terraform 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.
Pass environment-specific inputs and extract output values for downstream tooling
terraform 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.
Download, update and selectively work with reusable Terraform modules
terraform 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.
Scope Terraform operations to specific resources when troubleshooting or recovering safely
terraform 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.
Trace provider behavior, inspect plans and visualize dependency graphs during incident response
TF_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.
Authenticate with Terraform Cloud and manage remote operations and legacy lifecycle actions
terraform 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.
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
terraform initbeginnerInitialize 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 -upgradeintermediateReinitialize the directory and upgrade providers and modules to newer allowed versions.
terraform init -upgradeterraform init -backend-config=backend.hclintermediateInitialize Terraform using backend settings from an external configuration file.
terraform init -backend-config=prod.backend.hclterraform validatebeginnerCheck 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 fmtbeginnerFormat Terraform files in the current directory to the canonical style.
terraform fmtterraform fmt -recursivebeginnerFormat all Terraform files recursively across modules and environment folders.
terraform fmt -recursiveterraform planbeginnerCreate 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=tfplanintermediateSave the generated plan to a file for later review or controlled apply.
terraform plan -out=prod.tfplanterraform applybeginnerApply the current execution plan and reconcile infrastructure to the configuration.
terraform applyterraform apply tfplanintermediateApply 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-approveintermediateApply changes without an interactive approval prompt.
terraform apply -auto-approveterraform destroyintermediateGenerate 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-approveadvancedDestroy all managed resources without asking for confirmation.
terraform destroy -auto-approveterraform state listbeginnerList 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>intermediateDisplay the attributes of a single resource as stored in state.
terraform state show aws_instance.webterraform state mv <source> <dest>advancedMove 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>advancedRemove a resource from state without destroying the real infrastructure object.
terraform state rm aws_security_group.legacyterraform state pullintermediateDownload and print the latest remote state to standard output.
terraform state pullterraform state pushadvancedUpload 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>advancedRewrite provider references in state when migrating resources to a new provider source.
terraform state replace-provider hashicorp/aws registry.acme.corp/acme/awsterraform import <resource_type.name> <id>advancedImport 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 refreshintermediateUpdate state with the real infrastructure data from providers without changing configuration.
terraform refreshterraform force-unlock <lock-id>advancedManually 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.
terraform workspace listbeginnerList all available workspaces and mark the currently selected one.
terraform workspace listterraform workspace new <name>beginnerCreate 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>beginnerSwitch the active workspace for future plan and apply operations.
terraform workspace select productionterraform workspace showbeginnerPrint the name of the currently selected workspace.
terraform workspace showterraform workspace delete <name>intermediateDelete an unused workspace after its state is no longer needed.
terraform workspace delete feature-preview-42terraform plan -var="key=value"beginnerPass a single input variable inline while generating a plan.
terraform plan -var="instance_type=t3.medium"terraform plan -var-file=prod.tfvarsbeginnerLoad 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"intermediateApply infrastructure changes using an inline variable override.
terraform apply -var="environment=production"terraform outputbeginnerShow all root module outputs after an apply completes.
terraform outputterraform output <name>beginnerPrint a single named output value from the current state.
terraform output alb_dns_nameterraform output -jsonintermediateRender 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>intermediatePrint a single output as raw text without quotes or JSON formatting.
terraform output -raw kubeconfig_pathterraform getbeginnerDownload child modules referenced by the current configuration.
terraform getterraform get -updateintermediateRe-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 initbeginnerInitialize the working directory and download required providers and modules.
terraform initterraform plan -target=module.<name>advancedPreview changes for a specific module without planning the whole stack.
terraform plan -target=module.vpcterraform apply -target=module.<name>advancedApply 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.
terraform plan -target=aws_instance.webadvancedCreate a plan limited to a specific resource address.
terraform plan -target=aws_instance.webterraform apply -target=aws_instance.webadvancedApply 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.webadvancedDestroy one targeted resource without tearing down the entire workspace.
terraform destroy -target=aws_instance.webterraform apply -replace=aws_instance.webadvancedForce 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=falseintermediateGenerate a plan without refreshing remote object state first.
terraform plan -refresh=falseterraform apply -parallelism=20advancedControl how many resource operations Terraform performs concurrently during apply.
terraform apply -parallelism=20TF_LOG=DEBUG terraform planintermediateRun 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 applyadvancedRun an apply with full trace logging to diagnose complex provider behavior.
TF_LOG=TRACE terraform applyTF_LOG_PATH=terraform.log terraform planintermediateWrite Terraform debug logs to a file instead of flooding standard output.
TF_LOG_PATH=terraform.log terraform planterraform showbeginnerDisplay the current state or a human-readable representation of Terraform data.
terraform showterraform show tfplanintermediateRender the contents of a saved plan file for review before apply.
terraform show prod.tfplanterraform show -json tfplan | jq .advancedConvert 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 graphintermediateGenerate a dependency graph of resources and modules in DOT format.
terraform graphterraform graph | dot -Tpng > graph.pngadvancedRender the Terraform dependency graph to a PNG for architecture review.
terraform graph | dot -Tpng > graph.pngterraform versionbeginnerShow the installed Terraform CLI version and platform details.
terraform versionterraform providersintermediateList the providers required by the current configuration and modules.
terraform providersterraform providers lockadvancedGenerate 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.
terraform loginbeginnerAuthenticate 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 logoutbeginnerRemove locally stored Terraform Cloud credentials for the current hostname.
terraform logoutterraform whoamiintermediateShow the Terraform Cloud user associated with the current authenticated token.
terraform whoamiterraform workspace listbeginnerList remote workspaces when using a Terraform Cloud or remote backend.
terraform workspace listterraform force-unlockadvancedRelease a stuck remote state lock after verifying no run is still active.
terraform force-unlock 7d4b3770-6f9a-4e94-8e75-5ab49d5d441cterraform taint <resource>advancedMark 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>advancedRemove the tainted marker from a resource so Terraform stops forcing replacement.
terraform untaint aws_instance.web