← Back to DevOpsCommands

Terraform

Terraform Commands Cheat Sheet

Practical Terraform commands for infrastructure as code, providers, resources, state, modules, workspaces, planning, deployment, CI/CD and troubleshooting.

19 categories87+ commands

Showing 87 commands.

Terraform Setup and Version

terraform version

Display the installed Terraform version and provider information.

terraform -help

Display Terraform help and available commands.

terraform <command> -help

Display help for a specific Terraform command.

terraform providers

Display the providers required by the current configuration.

Initialize Terraform

terraform init

Initialize the working directory and download required providers and modules.

terraform init -upgrade

Upgrade modules and providers to newer allowed versions.

terraform init -reconfigure

Reconfigure the backend without attempting to migrate existing state.

terraform init -migrate-state

Attempt to migrate existing state to a newly configured backend.

Format and Validate

terraform fmt

Format Terraform configuration files in the current directory.

terraform fmt -recursive

Format Terraform configuration files recursively in subdirectories.

terraform fmt -check

Check whether Terraform files are correctly formatted without modifying them.

terraform validate

Validate the configuration for syntax and internal consistency.

terraform validate -json

Return validation results in JSON format.

Plan and Apply

terraform plan

Create an execution plan showing proposed infrastructure changes.

terraform plan -out=tfplan

Save the Terraform execution plan to a file.

terraform show tfplan

Display a saved Terraform plan.

terraform apply

Create or update infrastructure according to the Terraform configuration.

terraform apply tfplan

Apply a previously saved execution plan.

terraform apply -auto-approve

Apply changes without asking for interactive approval.

terraform apply -refresh-only

Update state to reflect remote infrastructure without changing infrastructure.

Destroy Infrastructure

terraform destroy

Destroy infrastructure managed by the current Terraform configuration.

terraform destroy -auto-approve

Destroy managed infrastructure without interactive approval.

terraform plan -destroy

Create 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 console

Open an interactive console for evaluating Terraform expressions.

Output Values

terraform output

Display all root module output values.

terraform output instance_ip

Display a specific output value.

terraform output -raw instance_ip

Display a string output without Terraform formatting.

terraform output -json

Display output values in JSON format.

Terraform State

terraform state list

List all resources currently tracked in Terraform state.

terraform state show aws_instance.web

Display detailed state information for a specific resource.

terraform state pull

Download the current state and print it to standard output.

terraform state push terraform.tfstate

Upload a local state file to the configured backend. Use with extreme care.

terraform state mv old.name new.name

Move a resource to a new state address without recreating it.

terraform state rm aws_instance.web

Remove a resource from Terraform state without destroying the remote object.

terraform state replace-provider old new

Replace provider references in Terraform state.

State Locking and Refresh

terraform plan -refresh-only

Create a plan that updates state to match remote infrastructure without changing resources.

terraform apply -refresh-only

Apply 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 list

List 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-1234567890

Import an existing infrastructure object into Terraform state.

terraform plan

Review the configuration and imported state before making further changes.

Modules

terraform get

Download modules referenced by the configuration.

terraform init

Initialize the configuration and download required modules.

terraform init -upgrade

Upgrade module dependencies within the configured constraints.

terraform providers

Show provider requirements for the root module and child modules.

Workspaces

terraform workspace list

List available Terraform workspaces.

terraform workspace show

Display the currently selected workspace.

terraform workspace new dev

Create and select a new workspace.

terraform workspace select dev

Switch to an existing workspace.

terraform workspace delete dev

Delete an existing workspace when it is safe to do so.

Providers and Dependencies

terraform providers

Show provider requirements for the configuration.

terraform providers schema

Display provider and resource schema information.

terraform providers lock

Generate or update provider dependency lock information.

terraform init

Resolve and install required provider plugins.

Graph and Inspection

terraform graph

Generate a dependency graph of Terraform resources.

terraform show

Display the current state or a saved plan in human-readable form.

terraform show -json

Display state or plan information as JSON.

Terraform Cloud and Automation

terraform login

Authenticate Terraform CLI with a Terraform-compatible service.

terraform logout

Remove stored credentials for a Terraform-compatible service.

terraform init

Initialize Terraform in a CI/CD environment before planning or applying.

terraform plan -input=false

Run planning without prompting for interactive input, useful in automation.

terraform apply -input=false

Run apply without interactive variable input when values are supplied through automation.

CI/CD Terraform Workflow

terraform fmt -check -recursive

Check formatting across the Terraform project in CI.

terraform init -input=false

Initialize Terraform non-interactively in CI/CD.

terraform validate

Validate Terraform configuration before creating an infrastructure plan.

terraform plan -out=tfplan

Generate a saved infrastructure plan for review or later application.

terraform apply -input=false tfplan

Apply a previously reviewed Terraform plan in automation.

Terraform Troubleshooting

terraform version

Verify the installed Terraform version when troubleshooting CLI behavior.

terraform providers

Inspect provider dependencies when provider-related errors occur.

terraform validate

Check the configuration for syntax and configuration errors.

terraform plan

Inspect the proposed changes and identify configuration or state issues.

terraform state list

Check which resources Terraform currently manages.

terraform state show <resource>

Inspect detailed state for a problematic resource.

terraform refresh

Legacy state refresh command; prefer refresh-only planning and applying in modern workflows.

DevOps Terraform Workflow

terraform fmt -recursive

Format Terraform files before committing infrastructure changes.

terraform init

Initialize providers, modules and backend configuration.

terraform validate

Validate Terraform configuration.

terraform plan

Review the infrastructure changes before applying them.

terraform apply

Apply the reviewed infrastructure changes.

terraform output

Display 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.