100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
HCL

Terraform Quick Reference

A condensed cheat sheet of the most-used Terraform CLI commands, HCL syntax patterns, and meta-arguments for fast lookup during real work.

Interview PrepBeginner8 min readJul 9, 2026
Analogies

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.

bash
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 workspace

HCL 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.

hcl
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

Was this page helpful?

Topics covered

#HCL#TerraformInfrastructureAsCodeStudyNotes#DevOps#TerraformQuickReference#Terraform#Quick#Reference#Core#StudyNotes#SkillVeris