Quick start
Terraform Cheatsheet — Commands, Syntax & Patterns at a Glance
Quick reference for Terraform CLI commands, HCL syntax, state operations, lifecycle rules, and common workflows. Bookmark this page for daily use.
- Commands
- HCL idioms
- Printable
A fast reference for everyday Terraform work. For deep dives, follow the linked articles.
CLI essentials
#| Command | Purpose |
|---|---|
terraform init | Download providers, configure backend |
terraform fmt | Format .tf files canonically |
terraform validate | Check syntax & internal consistency |
terraform plan | Preview changes |
terraform apply | Apply changes |
terraform destroy | Tear down managed resources |
terraform state list | List resources tracked in state |
terraform import <addr> <id> | Bring an existing resource under management |
terraform taint <addr> | Mark for recreation on next apply |
terraform workspace select <name> | Switch workspace |
See Terraform commands overview and Format and validate Terraform configuration.
HCL building blocks
#terraform {
required_version = ">= 1.5.0"
required_providers {
aws = { source = "hashicorp/aws", version = "~> 5.0" }
}
backend "s3" {
bucket = "my-tfstate"
key = "prod/terraform.tfstate"
region = "eu-west-1"
}
}
variable "env" { type = string default = "dev" }
resource "aws_s3_bucket" "data" {
bucket = "data-${var.env}"
lifecycle {
prevent_destroy = true
create_before_destroy = true
}
}
output "bucket_id" { value = aws_s3_bucket.data.id }Patterns
#- Multiplex resources with
countorfor_each— guide. - Pin providers with the dependency lock file — guide.
- Remote state with locking in S3 + DynamoDB — guide.
- Modules for reuse — guide.
- Sensitive values — guide.
- Zero-downtime deployments — guide.
Troubleshooting quick links
#- Inconsistent dependency lock file
- S3 backend region mismatch
- InvalidClientTokenId (AWS auth)
- Common errors & solutions
See the Glossary for term definitions or browse all Articles.