Skip to main content

Terraform CLI & Core Workflow Cheat Sheet

Essential Commands, Block Types & Developer Workflow — from Write to Apply.


Core Terraform Workflow​

The core developer lifecycle from writing code to provisioning and tearing down infrastructure.


HCL Block Types​

Block TypeDescription
resourceCreates & manages infrastructure objects (e.g., VMs, networks).
dataReads and fetches existing external data or resources.
variableParameterizes configuration with input arguments.
outputExposes values after apply for users or other configurations.
localsDefines computed intermediate values and expressions.
moduleConsumes reusable packages of Terraform configurations.
providerConfigures target platform plugins (e.g., AWS, Azure, GCP).
terraformConfigures global settings, backend storage, and required_providers.

Essential CLI Commands​

CommandPurpose
terraform initInitialize working directory, download modules, and install provider plugins.
terraform fmtRewrites configuration files to canonical formatting style.
terraform validateValidates configuration syntax and internal consistency.
terraform planGenerates and previews the execution plan without modifying infrastructure.
terraform applyProvisions or updates the infrastructure as specified in configuration.
terraform destroyDestroys all infrastructure managed by this configuration.
terraform outputReads and extracts output values from the state file.
terraform consoleLaunches an interactive console for evaluating expressions and state.
terraform graphOutputs the visual dependency graph in Graphviz (DOT) format.
terraform providersDisplays a tree of providers required by the configuration.

Variable Definition Precedence​

Terraform loads variables according to a strict order of precedence. Values loaded later override those loaded earlier.

Note: CLI flags (-var and -var-file) always override file-based and environment definitions. Auto-loaded files are processed in alphabetical order.


Useful CLI Flags & Environment Variables​

Common CLI Flags​

FlagDescription
-auto-approveSkips interactive approval prompt before applying changes.
-target=<resource>Focuses execution only on a specific resource address.
-var="k=v"Sets an input variable value inline via the CLI.
-var-file=<path>Loads variable definitions from a specified .tfvars file.
-refresh-onlyChecks state against remote objects without proposing changes.
-out=<path>Writes the generated plan file to disk (e.g., plan.out).

Environment Variables​

VariableDescriptionExample / Values
TF_LOGConfigures logging verbosity.TRACE, DEBUG, INFO, WARN, ERROR
TF_LOG_PATHWrites log output to a designated file./path/to/tf.log
TF_VAR_<name>Supplies an input variable value via environment.TF_VAR_region="us-east-1"

HCP Terraform / Terraform Cloud Block​

terraform {
cloud {
organization = "org"

workspaces {
name = "my-app"
}
}
}

Original by Bryan Krausen (btk.me/btk)