Skip to content

Terraform Cheat Sheet

A cheat sheet for the basic Terraform workflow and the most commonly used CLI commands.

Initialization and Lifecycle

terraform version
terraform init
terraform fmt -recursive
terraform validate
terraform plan
terraform apply
terraform destroy

Working with Variables

terraform plan -var 'region=eu-central-1'
terraform plan -var-file=dev.tfvars
export TF_VAR_region=eu-central-1

State

terraform state list
terraform state show aws_instance.web
terraform state rm aws_instance.old
terraform import aws_s3_bucket.logs my-bucket-name
terraform output
terraform output vpc_id

Workspaces

terraform workspace list
terraform workspace new dev
terraform workspace select prod
terraform workspace show

Useful Commands

terraform providers
terraform console
terraform graph
terraform plan -out=tfplan
terraform apply tfplan
terraform show
terraform show -json tfplan

Basic Template

terraform {
  required_version = ">= 1.6.0"

  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
}

provider "aws" {
  region = var.region
}

variable "region" {
  type    = string
  default = "eu-central-1"
}

Practice

  • Always run fmt, validate, and plan before apply.
  • Remote state and locking are mandatory for team workflows.
  • terraform import brings a resource into state, but it does not write the HCL configuration for you.
  • Do not edit the state file manually unless absolutely necessary.

References