← All insightsCheat Sheet

Terraform cheat sheet: the complete command reference

CodeWizard Engineering · 14 August 2026 · 4 min read · Beginner

This is a complete, hands-on Terraform cheat sheet: install, configure, operate, automate and secure it - with copy-paste commands grouped by task. Bookmark it for day-to-day work, incidents and interviews alike.

Terraform describes infrastructure as declarative HCL, builds a dependency graph, and shows a plan of exactly what will change before it applies - across AWS, Azure, GCP and hundreds of providers.

Why you reach for it

  • Every infrastructure change is reviewed as code in a pull request
  • terraform plan shows the diff before anything is touched
  • One workflow and language across clouds and SaaS providers

Installation

bash
brew install terraform      # or download the binary from releases
terraform -install-autocomplete
terraform version

Configuration reference

A minimal AWS example with remote state and a variable.

hcl
terraform {
  required_providers { aws = { source = "hashicorp/aws", version = "~> 5.0" } }
  backend "s3" {
    bucket = "acme-tfstate"
    key    = "prod/network.tfstate"
    region = "eu-west-1"
  }
}

variable "env" { type = string, default = "prod" }

resource "aws_s3_bucket" "assets" {
  bucket = "acme-${var.env}-assets"
  tags   = { Environment = var.env }
}

Core commands you use daily

bash
terraform init
terraform fmt -recursive && terraform validate
terraform plan -out tfplan
terraform apply tfplan

Core workflow

bash
terraform init
terraform fmt -recursive
terraform validate
terraform plan -out tfplan
terraform apply tfplan
terraform destroy

State operations

bash
terraform state list
terraform state show aws_s3_bucket.assets
terraform state mv aws_s3_bucket.a aws_s3_bucket.b
terraform import aws_s3_bucket.assets acme-assets
terraform output -json

Targeting & workspaces

bash
terraform plan -target=module.network
terraform workspace new staging
terraform workspace select prod

Use it in CI/CD

Plan on every PR, apply only on main.

yaml
plan:
  stage: test
  script:
    - terraform init -input=false
    - terraform plan -input=false -out=tfplan
apply:
  stage: deploy
  script: terraform apply -input=false -auto-approve tfplan
  rules:
    - if: '$CI_COMMIT_BRANCH == "main"'

Best-practice checklist

  • Remote, locked state; separate state per environment/component
  • Pin provider and module versions; commit .terraform.lock.hcl
  • Keep modules small and composable; review plans in the PR
  • Use workspaces or directories, never edit prod state by hand

Common pitfalls to avoid

  • Local state on a laptop - use remote state with locking (S3+DynamoDB, TFC)
  • Giant monolithic states that take minutes to plan and block the whole team
  • Running apply manually without reviewing the plan
  • Hardcoded secrets in .tf files instead of a secret manager

Security quick wins

  • Scan with tfsec/Checkov in CI to catch open security groups and public buckets
  • Pull secrets from Vault/SSM at apply time instead of committing them
  • Restrict the CI role with least-privilege IAM for the apply step

Works well with

  • OpenTofu - The open-source, community-governed fork of Terraform - same HCL, MPL-licensed.
  • Pulumi - Infrastructure as code in real languages - TypeScript, Python, Go and C# - with a state backend.
  • Ansible - Agentless configuration management and provisioning over plain SSH, described in readable YAML.
A cheat sheet is muscle memory on paper - keep it close until the commands live in your fingers.

Want senior help putting Terraform to work - or a team bootcamp on it? CodeWizard runs Israel's largest DevOps community and embeds named senior engineers with a transparent hours log. Book a free consult or explore the Academy.

Like where this is going?

Get hands-on help from the engineers who write these posts.

Book a free consult