All Cheatsheets

Terraform Cheatsheet

Terraform commands for init, plan, apply, state management, workspaces, modules, and production workflows.

8 sections61 commandsClick any row to copy

Setup & Authentication

terraform version
terraform -install-autocomplete
export AWS_ACCESS_KEY_ID=xxx
export AWS_SECRET_ACCESS_KEY=xxx
export AWS_DEFAULT_REGION=us-east-1
aws configure

Show installed Terraform version

Enable shell tab completion

Set AWS credentials via env var

Set AWS secret via env var

Set default AWS region

Configure AWS credentials interactively

Core Workflow

terraform init
terraform init -upgrade
terraform init -reconfigure
terraform validate
terraform fmt
terraform fmt -check
terraform fmt -recursive
terraform plan
terraform plan -out=tfplan
terraform plan -var-file=prod.tfvars
terraform apply
terraform apply tfplan
terraform apply -auto-approve
terraform destroy
terraform destroy -auto-approve

Initialize working directory, download providers

Upgrade providers to latest allowed version

Reinitialize backend configuration

Check configuration for syntax errors

Reformat .tf files to canonical style

Check formatting without modifying files

Format all .tf files in subdirectories too

Preview changes before applying

Save plan to file for later apply

Plan using specific variable file

Apply changes to infrastructure

Apply a previously saved plan file

Apply without interactive confirmation prompt

Destroy all managed infrastructure

Destroy without confirmation (careful!)

State Management

terraform show
terraform state list
terraform state show aws_instance.web
terraform state pull
terraform state push terraform.tfstate
terraform state rm aws_instance.web
terraform state mv aws_instance.old aws_instance.new
terraform refresh
terraform force-unlock LOCK_ID

Show current state or a saved plan

List all resources in state

Show details of a specific resource

Download and print remote state

Upload local state to remote backend

Remove resource from state (keeps real infra)

Rename resource in state without recreating

Sync state with real infrastructure

Release a stuck state lock

Variables & Outputs

terraform apply -var="region=us-east-1"
terraform apply -var-file=staging.tfvars
export TF_VAR_region=us-east-1
terraform output
terraform output instance_ip
terraform output -json
terraform output -raw instance_ip

Pass single variable on command line

Pass all variables from a .tfvars file

Set variable via environment variable

Show all output values from state

Show a specific output value

Show all outputs in JSON format

Show raw output value (no quotes)

Targeting & Importing

terraform plan -target=aws_instance.web
terraform apply -target=aws_instance.web
terraform destroy -target=aws_instance.web
terraform import aws_instance.web i-1234567890abcdef0
terraform import -var-file=prod.tfvars aws_s3_bucket.main my-bucket

Plan only for a specific resource

Apply only a specific resource

Destroy only a specific resource

Import existing resource into state

Import resource using variable file

Workspaces

terraform workspace list
terraform workspace show
terraform workspace new staging
terraform workspace select production
terraform workspace delete staging

List all workspaces

Show current workspace name

Create and switch to new workspace

Switch to an existing workspace

Delete a workspace (must not be current)

Modules

terraform get
terraform init -upgrade
terraform plan -target=module.vpc
terraform apply -target=module.vpc
terraform destroy -target=module.vpc
terraform state list | grep module.vpc

Download and install modules from config

Update modules to latest allowed versions

Plan changes only for a specific module

Apply changes only for a specific module

Destroy only a specific module

List all resources inside a module

Debugging & Logs

TF_LOG=DEBUG terraform plan
TF_LOG=TRACE terraform apply
TF_LOG_PATH=./terraform.log terraform plan
terraform graph
terraform graph | dot -Tsvg > graph.svg
terraform console
terraform providers
terraform providers lock

Run with debug-level logging

Run with full trace logging (most verbose)

Write logs to a file instead of stdout

Output resource dependency graph (DOT format)

Render dependency graph as SVG image

Open interactive REPL to test expressions

List all required providers for current config

Lock provider versions in .terraform.lock.hcl