terraform commands cheatsheet.
Init, plan, apply, manage state and workspaces — the Terraform commands you reach for every day. Tap to copy.
terraform initInitialize a working directoryterraform validateCheck the configuration for errorsterraform planPreview changes before applyingterraform applyApply changes to reach the desired stateterraform apply -auto-approveApply without the interactive promptterraform destroyDestroy all managed infrastructureterraform fmtRewrite files to canonical formatterraform fmt -recursiveFormat files in all subdirectoriesterraform showShow the current state or a saved planterraform outputShow output values from the stateterraform graphGenerate a dependency graphterraform consoleInteractive console to evaluate expressionsterraform state listList resources tracked in the stateterraform state show <addr>Show attributes of a resource in stateterraform state mv <src> <dst>Move or rename an item in the stateterraform state rm <addr>Remove a resource from the stateterraform refreshUpdate state to match real infrastructureterraform import <addr> <id>Import existing infrastructure into stateterraform workspace listList available workspacesterraform workspace new <name>Create a new workspaceterraform workspace select <name>Switch to another workspaceterraform workspace delete <name>Delete a workspaceterraform plan -target=<addr>Plan only a specific resourceterraform apply -var 'key=value'Set a variable on the command lineterraform apply -var-file=<file>Load variables from a fileterraform apply -replace=<addr>Force replacement of a resourceterraform init -upgradeUpgrade providers and modules to newest allowedterraform providersShow providers used by the configurationterraform getDownload and install modulesterraform versionShow the Terraform and provider versionsWhat 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.