Terraform
Terraform Commands Cheat Sheet
Practical Terraform commands for infrastructure as code, providers, resources, state, modules, workspaces, planning, deployment, CI/CD and troubleshooting.
Showing 87 commands.
Terraform Setup and Version
terraform versionDisplay the installed Terraform version and provider information.
terraform -helpDisplay Terraform help and available commands.
terraform <command> -helpDisplay help for a specific Terraform command.
terraform providersDisplay the providers required by the current configuration.
Initialize Terraform
terraform initInitialize the working directory and download required providers and modules.
terraform init -upgradeUpgrade modules and providers to newer allowed versions.
terraform init -reconfigureReconfigure the backend without attempting to migrate existing state.
terraform init -migrate-stateAttempt to migrate existing state to a newly configured backend.
Format and Validate
terraform fmtFormat Terraform configuration files in the current directory.
terraform fmt -recursiveFormat Terraform configuration files recursively in subdirectories.
terraform fmt -checkCheck whether Terraform files are correctly formatted without modifying them.
terraform validateValidate the configuration for syntax and internal consistency.
terraform validate -jsonReturn validation results in JSON format.
Plan and Apply
terraform planCreate an execution plan showing proposed infrastructure changes.
terraform plan -out=tfplanSave the Terraform execution plan to a file.
terraform show tfplanDisplay a saved Terraform plan.
terraform applyCreate or update infrastructure according to the Terraform configuration.
terraform apply tfplanApply a previously saved execution plan.
terraform apply -auto-approveApply changes without asking for interactive approval.
terraform apply -refresh-onlyUpdate state to reflect remote infrastructure without changing infrastructure.
Destroy Infrastructure
terraform destroyDestroy infrastructure managed by the current Terraform configuration.
terraform destroy -auto-approveDestroy managed infrastructure without interactive approval.
terraform plan -destroyCreate a plan showing what would be destroyed.
Variables
terraform plan -var='region=ap-south-1'Pass a variable value directly when creating a plan.
terraform apply -var='environment=prod'Pass a variable value directly during apply.
terraform plan -var-file='prod.tfvars'Load variable values from a specific variable file.
terraform apply -var-file='prod.tfvars'Apply infrastructure using values from a variable file.
terraform consoleOpen an interactive console for evaluating Terraform expressions.
Output Values
terraform outputDisplay all root module output values.
terraform output instance_ipDisplay a specific output value.
terraform output -raw instance_ipDisplay a string output without Terraform formatting.
terraform output -jsonDisplay output values in JSON format.
Terraform State
terraform state listList all resources currently tracked in Terraform state.
terraform state show aws_instance.webDisplay detailed state information for a specific resource.
terraform state pullDownload the current state and print it to standard output.
terraform state push terraform.tfstateUpload a local state file to the configured backend. Use with extreme care.
terraform state mv old.name new.nameMove a resource to a new state address without recreating it.
terraform state rm aws_instance.webRemove a resource from Terraform state without destroying the remote object.
terraform state replace-provider old newReplace provider references in Terraform state.
State Locking and Refresh
terraform plan -refresh-onlyCreate a plan that updates state to match remote infrastructure without changing resources.
terraform apply -refresh-onlyApply refresh-only changes to synchronize Terraform state.
terraform force-unlock <LOCK_ID>Remove a state lock that is confirmed to be stale. Use carefully.
Resources
terraform state listList resources tracked in the current state.
terraform state show <resource>Inspect attributes stored for a specific resource.
terraform plan -target=<resource>Create a plan targeting a specific resource. Use only for exceptional troubleshooting cases.
terraform apply -target=<resource>Apply changes targeting a specific resource. Avoid as a normal workflow.
Import Existing Infrastructure
terraform import aws_instance.web i-1234567890Import an existing infrastructure object into Terraform state.
terraform planReview the configuration and imported state before making further changes.
Modules
terraform getDownload modules referenced by the configuration.
terraform initInitialize the configuration and download required modules.
terraform init -upgradeUpgrade module dependencies within the configured constraints.
terraform providersShow provider requirements for the root module and child modules.
Workspaces
terraform workspace listList available Terraform workspaces.
terraform workspace showDisplay the currently selected workspace.
terraform workspace new devCreate and select a new workspace.
terraform workspace select devSwitch to an existing workspace.
terraform workspace delete devDelete an existing workspace when it is safe to do so.
Providers and Dependencies
terraform providersShow provider requirements for the configuration.
terraform providers schemaDisplay provider and resource schema information.
terraform providers lockGenerate or update provider dependency lock information.
terraform initResolve and install required provider plugins.
Graph and Inspection
terraform graphGenerate a dependency graph of Terraform resources.
terraform showDisplay the current state or a saved plan in human-readable form.
terraform show -jsonDisplay state or plan information as JSON.
Terraform Cloud and Automation
terraform loginAuthenticate Terraform CLI with a Terraform-compatible service.
terraform logoutRemove stored credentials for a Terraform-compatible service.
terraform initInitialize Terraform in a CI/CD environment before planning or applying.
terraform plan -input=falseRun planning without prompting for interactive input, useful in automation.
terraform apply -input=falseRun apply without interactive variable input when values are supplied through automation.
CI/CD Terraform Workflow
terraform fmt -check -recursiveCheck formatting across the Terraform project in CI.
terraform init -input=falseInitialize Terraform non-interactively in CI/CD.
terraform validateValidate Terraform configuration before creating an infrastructure plan.
terraform plan -out=tfplanGenerate a saved infrastructure plan for review or later application.
terraform apply -input=false tfplanApply a previously reviewed Terraform plan in automation.
Terraform Troubleshooting
terraform versionVerify the installed Terraform version when troubleshooting CLI behavior.
terraform providersInspect provider dependencies when provider-related errors occur.
terraform validateCheck the configuration for syntax and configuration errors.
terraform planInspect the proposed changes and identify configuration or state issues.
terraform state listCheck which resources Terraform currently manages.
terraform state show <resource>Inspect detailed state for a problematic resource.
terraform refreshLegacy state refresh command; prefer refresh-only planning and applying in modern workflows.
DevOps Terraform Workflow
terraform fmt -recursiveFormat Terraform files before committing infrastructure changes.
terraform initInitialize providers, modules and backend configuration.
terraform validateValidate Terraform configuration.
terraform planReview the infrastructure changes before applying them.
terraform applyApply the reviewed infrastructure changes.
terraform outputDisplay important infrastructure outputs after deployment.
Recommended DevOps Terraform Workflow
# Format terraform fmt -recursive # Initialize terraform init # Validate terraform validate # Review changes terraform plan # Apply terraform apply # Check outputs terraform output # Inspect state terraform state list
DevOpsCommands Tip
Always review terraform plan before applying infrastructure changes. Protect Terraform state with an appropriate remote backend and state locking mechanism, especially when working with teams and CI/CD pipelines.