Terraform Cheatsheet

Basics

Use this Terraform reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.

What Terraform Does

Terraform is an infrastructure-as-code tool that provisions and manages cloud resources through declarative config files (.tf). You describe what you want; Terraform figures out how to create, update, or destroy it.

Core Workflow

terraform init      # download providers, set up backend
terraform plan      # diff: what will change?
terraform apply     # make it so
terraform destroy   # tear everything down

Always run plan before apply in production. Review the diff carefully.

Project Structure

project/
├── main.tf          # core resources
├── variables.tf     # input variable declarations
├── outputs.tf       # output value declarations
├── providers.tf     # provider configuration
├── terraform.tfvars # variable values (gitignore secrets!)
└── .terraform/      # local plugin cache (never commit)

Multiple .tf files in a directory form a single root module — Terraform merges them all.

HCL Syntax Basics

# Block syntax
resource "aws_instance" "web" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t3.micro"

  tags = {
    Name = "web-server"
    Env  = "prod"
  }
}

# String interpolation
name = "server-${var.environment}"

# Multi-line string
user_data = <<-EOT
  #!/bin/bash
  yum install -y httpd
EOT

# Comments
# single-line comment
/* multi-line
   comment */

Addressing Resources

Every resource has a unique address used in plan output, state commands, and depends_on.

Address formExample
<type>.<name>aws_instance.web
module.<name>.<type>.<name>module.vpc.aws_vpc.main
data.<type>.<name>data.aws_ami.ubuntu
var.<name>var.instance_type
local.<name>local.common_tags
output.<name>output.instance_ip

Terraform Block

terraform {
  required_version = ">= 1.10.0"

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

  backend "s3" {
    bucket = "my-tf-state"
    key    = "prod/terraform.tfstate"
    region = "us-east-1"
  }
}

Version Constraints

ConstraintMeaning
= 1.6.0exactly 1.6.0
!= 1.5.0anything except 1.5.0
>= 1.6.01.6.0 or higher
~> 1.6>= 1.6.0, < 2.0.0
~> 1.6.0>= 1.6.0, < 1.7.0
>= 1.4, < 2.0range (AND)

Locals

locals {
  env         = var.environment
  name_prefix = "myapp-${local.env}"
  common_tags = {
    Project     = "myapp"
    Environment = local.env
    ManagedBy   = "terraform"
  }
}

# Reference:
resource "aws_instance" "web" {
  tags = local.common_tags
}

Lifecycle Meta-Argument

resource "aws_instance" "web" {
  ami           = var.ami_id
  instance_type = "t3.micro"

  lifecycle {
    create_before_destroy = true   # zero-downtime replacement
    prevent_destroy       = true   # block accidental deletion
    ignore_changes        = [tags] # ignore drift on these attrs
  }
}

depends_on

resource "aws_instance" "app" {
  # explicit dependency when implicit reference isn't possible
  depends_on = [aws_iam_role_policy_attachment.app_policy]
}

Useful Init Flags

terraform init -upgrade            # upgrade providers to latest allowed
terraform init -reconfigure        # ignore existing backend config
terraform init -backend-config=backend.hcl  # external backend config file
terraform init -migrate-state      # migrate state to new backend