Terraform Cheat Sheet

Declaratively provision infrastructure with a plan/apply workflow — state, providers, modules, and the destroy loop.

Infrastructure as Code4 sections · 30 commands
⌘K

Core Lifecycle

12
terraform init

Install providers and modules

terraform init -upgrade

Refresh provider/module versions

terraform validate

Syntax and consistency check

terraform fmt -recursive

Format all files canonically

terraform plan

Preview the changes

terraform plan -out=tfplan

Save the plan

terraform apply

Apply the plan (interactive)

terraform apply -auto-approve

Apply without prompting

terraform apply tfplan

Apply a saved plan

terraform destroy

Remove all managed resources

terraform apply -target=RESOURCE

Apply one target (debug)

terraform plan -refresh=false

Skip state refresh

State

7
terraform state list

Resources tracked in state

terraform state show RESOURCE

One resource's attributes

terraform state mv SRC DST

Rename/move in state

terraform state rm RESOURCE

Drop from state (no destroy)

terraform state pull

Print raw state to stdout

terraform state push FILE

Force an uploaded state

terraform refresh

Re-read real resources into state

Outputs & Variables

6
terraform output

Print all outputs

terraform output -json

Outputs as JSON

terraform apply -var 'image_id=ami-xyz'

Override a variable

terraform apply -var-file=vars.tfvars

Load variables from a file

TF_VAR_name=value terraform plan

Variables via environment

terraform plan -json | jq

Machine-readable plan

Workspaces & Backends

5
terraform workspace list

Environments in the current state

terraform workspace new dev

Create an environment

terraform workspace select dev

Switch environments

terraform init -backend-config=backend.tfvars

Point at a remote backend

terraform force-unlock LOCK_ID

Break a stale state lock