Terraform Quick Reference
This reference collects the commands and syntax patterns you reach for constantly once you're writing real Terraform — not exhaustive documentation, but the working set that covers the vast majority of day-to-day tasks. It is organized as a lookup table you can scan rather than a narrative to read start to finish, so keep it bookmarked rather than trying to memorize it in one sitting.
Cricket analogy: This is like a pocket almanac of playing conditions and DLS tables a captain keeps in their kit bag — not the full laws of cricket to study cover to cover, but the quick lookups needed constantly during a match.
Core CLI Workflow Commands
The essential lifecycle commands appear in nearly every Terraform session: initializing a working directory, previewing changes, and applying them. Each has a handful of flags worth knowing well because they come up constantly in real usage — targeting a specific resource, auto-approving in CI, or saving a plan file to guarantee the apply matches exactly what was reviewed.
Cricket analogy: The essentials every captain reaches for are the toss call, setting the field, and reviewing a decision — each has details worth knowing well, like which fielder to target, or when a DRS review is actually worth burning.
terraform init # download providers/modules, configure backend
terraform init -upgrade # upgrade providers to latest allowed version
terraform validate # check syntax and internal consistency
terraform fmt -recursive # auto-format all .tf files
terraform plan -out=tfplan # save an exact plan to apply later
terraform apply tfplan # apply exactly what was planned/reviewed
terraform apply -auto-approve # skip interactive confirmation (CI use)
terraform destroy -target=aws_instance.web # destroy a single targeted resource
terraform state list # list all resources tracked in state
terraform state show aws_instance.web # show attributes of a tracked resource
terraform state mv A B # rename/move a resource address in state
terraform state rm A # stop tracking a resource without destroying it
terraform import aws_instance.web i-0abc123 # bring existing infra under management
terraform output # print all output values
terraform workspace list # list workspaces
terraform workspace new staging # create and switch to a new workspaceHCL Syntax Cheat Sheet
Beyond commands, a handful of HCL patterns show up in nearly every configuration: variable and output declarations, local values for computed expressions, and the meta-arguments that control how resources are created, replaced, or iterated.
Cricket analogy: Beyond the toss and the innings itself, a handful of recurring elements show up in nearly every match: the team sheet, the run rate calculation, and the fielding restrictions that govern how players are positioned and rotated.
variable "env" {
type = string
default = "dev"
}
locals {
name_prefix = "${var.env}-app"
}
resource "aws_instance" "web" {
for_each = toset(["a", "b", "c"])
ami = data.aws_ami.latest.id
instance_type = "t3.micro"
tags = { Name = "${local.name_prefix}-${each.key}" }
lifecycle {
create_before_destroy = true
prevent_destroy = false
ignore_changes = [tags]
}
}
output "instance_ids" {
value = [for i in aws_instance.web : i.id]
}Meta-Arguments at a Glance
Meta-arguments modify how Terraform manages a resource block rather than configuring the resource itself. count and for_each control iteration, depends_on forces explicit ordering when Terraform can't infer a dependency, and the lifecycle block's three common arguments — create_before_destroy, prevent_destroy, and ignore_changes — govern replacement behavior and drift tolerance.
Cricket analogy: Squad rotation and playing eleven selection control how many players cycle through, a designated substitute forces an explicit swap when the captain can't infer it, and injury replacement rules govern how a player is replaced mid-series.
A fast mental checklist before any production apply: did you run terraform plan and actually read it, is the plan being applied from a saved -out file rather than a fresh unreviewed plan, and is state locking enabled on the backend? Three quick checks catch the majority of preventable incidents.
- terraform init, plan, and apply form the core workflow; use plan -out plus apply tfplan to guarantee what's reviewed is what's applied.
- terraform state list/show/mv/rm let you inspect and safely manipulate tracked resources without hand-editing the state file.
- terraform import brings existing, unmanaged infrastructure under Terraform's control.
- for_each and count control iteration; lifecycle { } arguments control replacement and drift behavior.
- terraform fmt -recursive and terraform validate should run before every commit as cheap sanity checks.
- Workspaces (terraform workspace new/list) provide lightweight environment separation within a single configuration.
Practice what you learned
1. What is the benefit of running terraform plan -out=tfplan and later terraform apply tfplan instead of just terraform apply?
2. Which command stops Terraform from tracking a resource without destroying the underlying infrastructure?
3. What does the lifecycle argument prevent_destroy do?
4. What is the purpose of terraform import?
5. What do terraform fmt -recursive and terraform validate check for, respectively?
Was this page helpful?
You May Also Like
terraform init, plan, and apply
The three-command core workflow of Terraform: init downloads dependencies, plan previews changes, and apply executes them -- forming a safe, repeatable provisioning cycle.
terraform validate and fmt
Understand two everyday Terraform commands — `terraform fmt` for consistent code style and `terraform validate` for catching syntax and configuration errors before you ever run a plan.
State Locking
State locking prevents two Terraform operations from writing to the same state file simultaneously, protecting against corruption and lost updates.
Common Terraform Pitfalls
A field guide to the mistakes Terraform users make most often — from state file mishandling to accidental destroys — and how to avoid each one.
Related Reading
Related Study Notes in DevOps
Browse all study notesNginx Study Notes
DevOps · 30 topics
DevOpsAnsible Study Notes
DevOps · 30 topics
DevOpsAdvanced Kubernetes Study Notes
Kubernetes · 30 topics
DevOpsAdvanced Bash Scripting Study Notes
Bash · 30 topics
DevOpsApache Kafka Study Notes
Kafka · 30 topics
DevOpsDocker & Kubernetes Study Notes
YAML · 40 topics