Terraform Cheat Sheet
Terraform HCL syntax, resource blocks, state management, and CLI commands.
2 PagesIntermediateApr 18, 2026
Resource Block
Declare a piece of infrastructure.
hcl
resource "aws_instance" "web" { ami = "ami-0abcd1234" instance_type = "t3.micro" tags = { Name = "web-server" }}
Variables & Outputs
Parameterize configuration and expose values.
hcl
variable "region" { type = string default = "us-east-1"}output "instance_ip" { value = aws_instance.web.public_ip}
State Management
Terraform tracks real infra in a state file.
bash
terraform init # Initialize backend & providersterraform plan # Preview changesterraform apply # Apply changesterraform destroy # Tear down infrastructure
Modules
Reuse configuration across environments.
hcl
module "vpc" { source = "./modules/vpc" cidr_block = "10.0.0.0/16"}
count, for_each & depends_on
Create multiple resources and control ordering.
hcl
# for_each over a set/map (stable addressing)resource "aws_iam_user" "team" { for_each = toset(["ada", "linus", "grace"]) name = each.key}# count for N identical copiesresource "aws_instance" "web" { count = 3 ami = var.ami instance_type = "t3.micro"}# explicit dependency when it can't be inferredresource "aws_s3_bucket_policy" "p" { bucket = aws_s3_bucket.b.id policy = data.aws_iam_policy_document.d.json depends_on = [aws_s3_bucket_public_access_block.b]}
Lifecycle & Dynamic Blocks
Fine-tune resource replacement and generate nested blocks.
hcl
resource "aws_instance" "web" { ami = var.ami instance_type = "t3.micro" lifecycle { create_before_destroy = true prevent_destroy = false ignore_changes = [tags["LastScanned"]] } dynamic "ebs_block_device" { for_each = var.extra_disks content { device_name = ebs_block_device.value.name volume_size = ebs_block_device.value.size } }}
Workspaces & Targeted Ops
Manage multiple environments and surgical changes.
bash
# Workspaces (separate state per environment)terraform workspace new stagingterraform workspace select stagingterraform workspace list# Plan/apply a single resourceterraform apply -target=aws_instance.web# Import existing infra into stateterraform import aws_s3_bucket.b my-existing-bucket# Preview destroy and format/validateterraform plan -destroyterraform fmt -recursive && terraform validate
Useful Built-in Functions
Common expressions used in variables and locals.
- lookup(map, key, default)- read a map value with a fallback
- coalesce(a, b, ...)- return the first non-null, non-empty argument
- try(expr, fallback)- catch errors in an expression and return a default
- for expressions- transform lists/maps, e.g. [for s in var.list : upper(s)]
- templatefile(path, vars)- render an external template with variables
- jsonencode / jsondecode- convert between HCL values and JSON strings
- cidrsubnet(prefix, newbits, num)- compute a subnet CIDR from a base range
Pro Tip
Store state remotely (e.g. S3 + DynamoDB lock table) for any team-shared infrastructure — local state doesn’t survive collaboration.
Was this cheat sheet helpful?
Explore Topics
#Terraform#TerraformCheatSheet#CloudComputing#Intermediate#ResourceBlock#VariablesOutputs#StateManagement#Modules#InfrastructureAsCode#CommandLine#CheatSheet#SkillVeris