TerraformPilot

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

#
CommandPurpose
terraform initDownload providers, configure backend
terraform fmtFormat .tf files canonically
terraform validateCheck syntax & internal consistency
terraform planPreview changes
terraform applyApply changes
terraform destroyTear down managed resources
terraform state listList 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 count or for_eachguide.
  • Pin providers with the dependency lock file — guide.
  • Remote state with locking in S3 + DynamoDB — guide.
  • Modules for reuse — guide.
  • Sensitive valuesguide.
  • Zero-downtime deploymentsguide.
#

See the Glossary for term definitions or browse all Articles.