~/cheatsheets (main) $ terraform -help

terraform cheat sheet

Terraform describes infrastructure as code: init downloads providers, plan shows what will change, apply makes it so, and the state file remembers what exists. These are the commands you run every day, grouped by task. OpenTofu accepts the same commands: switch to tofu or a tf alias and every command updates. Click a command to copy it.

cli
print

terraform core workflow

7 cmds

# Init, plan, apply, destroy.

Download providers and set up the backend
Show what will change
Apply the changes
Apply without the confirmation prompt
Save a plan, then apply exactly that plan
Preview a destroy
Destroy everything in the configuration

terraform plan and apply options

8 cmds

# Variables, targets and replacements.

Set a variable
Use a variables file
Plan only one resource and its dependencies
Force a resource to be recreated
Sync state with real infrastructure only
Exit code 2 when there are changes (for CI)
Wait for the state lock
Run more operations at once

terraform init and providers

8 cmds

# Backends, provider versions and modules.

Upgrade providers and modules within their constraints
Reconfigure the backend, ignoring the old one
Move state to a new backend
Pass backend settings from a file
List the providers in use
Lock provider hashes for several platforms
Update modules
Show the CLI and provider versions

terraform state

8 cmds

# What Terraform thinks exists.

List resources in state
Show one resource
Show the whole state or a saved plan
Rename or move a resource
Move a resource into a module
Stop managing a resource without destroying it
Download the remote state
Release a stuck lock

terraform import

3 cmds

# Bring existing infrastructure under Terraform.

Import a resource into state
Write config for import blocks
Replace a provider source in state

terraform workspaces

5 cmds

# Several states for one configuration.

List workspaces
Show the current workspace
Create a workspace
Switch workspace
Delete a workspace

terraform outputs and console

5 cmds

# Read values back out.

Show all outputs
Print one output without quotes
Outputs as JSON
Try expressions against the state
Draw the dependency graph

terraform format and test

5 cmds

# Keep the configuration clean and correct.

Format files in the current folder
Check formatting everywhere (for CI)
Validate the configuration
Run .tftest.hcl tests
Log in to HCP Terraform

terraform environment and debugging

5 cmds

# Environment variables that change how the CLI behaves.

Debug logs
Trace logs to a file
Set a variable from the environment
Pick the workspace from the environment
Run in another folder