Terraform is HashiCorp's open-source infrastructure as code (IaC) tool that provisions and manages cloud and on-premises resources using declarative configuration files written in HashiCorp Configuration Language (HCL). It works by building a dependency graph of your infrastructure, determining the optimal execution order, and applying changes through provider-specific APIs to AWS, Azure, GCP, Kubernetes, and hundreds of other platforms. Understanding Terraform's workflow—write configuration, initialize providers, plan changes, apply infrastructure—is essential, but the real power lies in mastering state management, modules, meta-arguments, and the growing set of ephemeral and lifecycle features (including removed, ephemeral, and write-only arguments) that enable you to build scalable, maintainable, and secrets-safe infrastructure with minimal manual intervention.
What This Cheat Sheet Covers
This topic spans 30 focused tables and 255 indexed concepts, 151 flashcards, 10 practice tests with 300 questions. Below is a complete table-by-table outline of this topic, spanning foundational concepts through advanced details.
A jump-to index of every table row in this cheat sheet.
An interactive map of every table and concept in this topic.
Table 1: Core CLI Commands
These are the commands you'll type every day, and the four that matter most, init, plan, apply, destroy, trace the entire Terraform lifecycle from setting up a directory to tearing infrastructure down. The rest handle the supporting work of validating, formatting, importing existing resources, and inspecting state, with a couple of entries flagged deprecated so you reach for their modern replacements instead.
| Command | Example | Description | |
|---|---|---|---|
terraform init | Initializes a working directory, downloads provider plugins, and sets up the backend. | ||
terraform plan -out=tfplan | • Generates an execution plan showing what actions Terraform will take • use -out to save for apply. | ||
terraform apply tfplan | Executes the actions proposed in a plan, creating, updating, or deleting resources to match configuration. | ||
terraform destroy | • Destroys all resources managed by the current configuration • use -target to destroy specific resources. | ||
terraform validate | Validates the syntax and internal consistency of configuration files without accessing remote services. | ||
terraform fmt -recursive | • Formats configuration files to a canonical style • use -recursive to format all subdirectories. | ||
terraform import aws_instance.web i-1234567890abcdef0 | • Brings existing infrastructure under Terraform management by binding it to state • Does not generate matching configuration for you, write the resource block yourself or use -generate-config-out. | ||
terraform state list | • Manages the state file • supports subcommands: list, show, mv, rm, pull, push. | ||
terraform output vpc_id | • Extracts the value of an output variable from the state file • useful for passing values between modules. | ||
terraform show | Displays the current state or a saved plan in human-readable format. | ||
terraform workspace new dev | Manages multiple isolated state files for different environments within the same configuration. | ||
terraform console | Opens an interactive console for evaluating expressions and functions against the current state. | ||
terraform test | Runs automated tests defined in .tftest.hcl files to validate configuration behavior. | ||
terraform graph | dot -Tpng > graph.png | Generates a visual representation of the dependency graph in DOT format. | ||
terraform force-unlock LOCK_ID | • Manually unlocks the state when a previous operation left it locked • use only when safe to do so. | ||
terraform providers lock -platform=linux_amd64 | • Manages provider plugins via subcommands • lock generates multi-platform lock entries; mirror copies providers locally; schema prints provider schemas. | ||
terraform apply -refresh-only | • Updates state to match real infrastructure • deprecated standalone command, use apply -refresh-only. | ||
terraform apply -replace="aws_instance.web" | • Marks a resource for recreation • deprecated, use apply -replace instead. |