Terraform Commands

terraform commands cheatsheet.

Init, plan, apply, manage state and workspaces — the Terraform commands you reach for every day. Tap to copy.

Core workflow
terraform initInitialize a working directory
terraform validateCheck the configuration for errors
terraform planPreview changes before applying
terraform applyApply changes to reach the desired state
terraform apply -auto-approveApply without the interactive prompt
terraform destroyDestroy all managed infrastructure
Formatting & inspection
terraform fmtRewrite files to canonical format
terraform fmt -recursiveFormat files in all subdirectories
terraform showShow the current state or a saved plan
terraform outputShow output values from the state
terraform graphGenerate a dependency graph
terraform consoleInteractive console to evaluate expressions
State
terraform state listList resources tracked in the state
terraform state show <addr>Show attributes of a resource in state
terraform state mv <src> <dst>Move or rename an item in the state
terraform state rm <addr>Remove a resource from the state
terraform refreshUpdate state to match real infrastructure
terraform import <addr> <id>Import existing infrastructure into state
Workspaces
terraform workspace listList available workspaces
terraform workspace new <name>Create a new workspace
terraform workspace select <name>Switch to another workspace
terraform workspace delete <name>Delete a workspace
Targeting & variables
terraform plan -target=<addr>Plan only a specific resource
terraform apply -var 'key=value'Set a variable on the command line
terraform apply -var-file=<file>Load variables from a file
terraform apply -replace=<addr>Force replacement of a resource
Providers & modules
terraform init -upgradeUpgrade providers and modules to newest allowed
terraform providersShow providers used by the configuration
terraform getDownload and install modules
terraform versionShow the Terraform and provider versions

What Terraform actually does

Terraform is HashiCorp's infrastructure-as-code tool: you describe the servers, networks and services you want in .tf configuration files, and Terraform figures out the API calls needed to make reality match. The workflow is deliberately small. terraform init prepares the directory — downloading providers and modules and wiring up the backend — and it's the first thing you run in any new configuration or after cloning a project.

Plan before you apply

The habit that keeps Terraform safe is reading the plan. terraform plan compares your configuration against the recorded state and prints exactly what it would create, change or destroy without touching anything. Only once that looks right do you run terraform apply, which executes the same diff and updates the state. In automation you'll often reach for terraform apply -auto-approve, but interactively the confirmation prompt is a useful last chance to catch a surprise destroy.

State is the source of truth

Terraform tracks everything it manages in a state file, and the state subcommands let you inspect and repair it. terraform state list shows every tracked resource, terraform state show <addr> prints one in detail, and terraform state mv and terraform state rm fix up addresses after refactors. When infrastructure already exists outside Terraform, terraform import <addr> <id> brings it under management without recreating it.

Workspaces, targeting and variables

A few flags handle the real-world edge cases. Workspaces let one configuration back several environments — list, create and switch with terraform workspace. When a plan is huge you can narrow it with -target=<addr>, force a rebuild with -replace=<addr>, and feed inputs with -var or -var-file. These pair naturally with the AWS & gcloud CLI for the underlying cloud and the Docker commands when you build the images your infrastructure runs.

FAQ

What's the difference between terraform plan and apply?
terraform plan shows the changes Terraform would make without touching anything; terraform apply actually creates, updates or destroys resources to match your configuration.
What does terraform init do?
terraform init prepares a working directory — it downloads the providers and modules your configuration needs and configures the backend, and must be run before plan or apply.

More cheatsheets