The Terraform cheatsheet covers the init/plan/apply/destroy workflow, HCL syntax for variables, outputs, modules, data sources, lifecycle rules, state management commands, workspaces, and for_each/count patterns.
No results found
How to Use This Terraform Cheatsheet
Terraform is HashiCorp's infrastructure-as-code tool that provisions and manages cloud resources using declarative HCL (HashiCorp Configuration Language) configuration files. This cheatsheet covers the core workflow, HCL syntax patterns, and state management commands.
Core Workflow
Every Terraform project follows the same lifecycle: terraform init (download providers) → terraform plan (preview changes) → terraform apply (create/update resources) → terraform destroy (tear down). Always review plan output carefully before applying — look for unexpected replacements or deletions.
Variable Precedence
Terraform variables are set in this order (highest wins): CLI -var flag → TF_VAR_name environment variables → terraform.tfvars → *.auto.tfvars → default in variable declaration. Use terraform.tfvars for environment-specific values, never commit secrets.
Remote State
For team environments, store state remotely in S3 (with DynamoDB locking), GCS, or Terraform Cloud. Remote state prevents concurrent apply conflicts and allows outputs to be shared across modules via terraform_remote_state data source.
Frequently Asked Questions
Is this Terraform cheatsheet free?
Yes, completely free with no signup. All Terraform CLI commands and HCL examples are copyable.
What is the difference between terraform plan and terraform apply?
terraform plan generates an execution plan showing what changes Terraform will make — it reads state and config but makes no actual changes. terraform apply executes the plan, creating, updating, or destroying real infrastructure. Always review the plan output before applying.
What does terraform init do?
terraform init initializes a working directory: it downloads required provider plugins, sets up the backend for state storage, and prepares modules. Run it whenever you clone a new Terraform config or add new providers. It's safe to run multiple times.
How do I import existing infrastructure into Terraform?
Use terraform import resource_type.name resource_id to bring an existing resource under Terraform management. For example: terraform import aws_instance.web i-1234567890abcdef0. After importing, run terraform plan to verify the config matches the actual resource.
What is terraform state and why does it matter?
Terraform state (terraform.tfstate) tracks the real-world resources Terraform manages. It maps config resources to actual cloud resources and stores metadata. Without state, Terraform can't know what already exists. Use remote state (S3, Terraform Cloud) for teams to avoid conflicts.
What is the difference between count and for_each?
count creates N identical copies indexed by number (count.index). for_each creates copies keyed by a map or set, giving each instance a stable identity. for_each is preferred because removing an item in the middle of a count list causes index shifting and unintended resource replacements.