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 254 indexed concepts. Below is a complete table-by-table outline of this topic, spanning foundational concepts through advanced details.
Table 1: Core CLI Commands
| 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 — creates, updates, or deletes 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 importing it into state. | |
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. |