Terraform & Infrastructure as Code›Modules · Cheat sheet & self-check
Learning Hub / Delivery & Infrastructure as Code / Terraform & Infrastructure as Code

Modules · wrap-up

Cheat sheet & self-check

Every command from this section on one page.

01 · IaC principles & Terraform basics

The cycle

terraform initDownload providers and modules, set up the backend
terraform fmt -recursiveFormat all files
terraform validateCheck syntax and references
terraform planShow what would change
terraform applyMake the changes (after confirmation)
terraform destroyRemove everything this configuration manages

Look around

terraform showCurrent state in readable form
terraform outputOutput values
terraform consoleTry expressions interactively

02 · State management

Inspect state

terraform state listEvery resource address in state
terraform state show 'aws_s3_bucket.logs'Recorded attributes of one resource
terraform plan -refresh-onlyShow how reality differs from state, without proposing changes

Change state (prefer code blocks)

import { to = aws_s3_bucket.logs id = "acme-logs" }Adopt an existing resource (Terraform 1.5+)
moved { from = aws_s3_bucket.old to = aws_s3_bucket.logs }Rename or move without destroying
removed { from = aws_s3_bucket.logs lifecycle { destroy = false } }Stop managing a resource without deleting it (1.7+)
terraform plan -generate-config-out=generated.tfGenerate config for import blocks

03 · Variables, outputs & data sources

Setting values (lowest → highest precedence)

default in the variable blockFallback
TF_VAR_name environment variableEnvironment
terraform.tfvars / *.auto.tfvarsLoaded automatically
-var-file=prod.tfvars / -var name=valueCommand line, in the order given

Expressions

var.name, local.name, module.x.outputReferences
for_each = toset(var.names)One resource per item
{ for k, v in var.map : k => upper(v) }Transform a map
coalesce(var.a, "default")First non-null/non-empty value

04 · Modules

Using modules

source = "./modules/bucket"A local module
source = "git::https://github.com/acme/tf-modules.git//bucket?ref=v1.4.0"A module from Git, pinned to a tag
source = "terraform-aws-modules/vpc/aws" version = "~> 5.0"A registry module with a version constraint
terraform get -updateRefresh downloaded modules

Module layout

main.tf variables.tf outputs.tf versions.tf README.mdConventional files
examples/ tests/Usage examples and tests

05 · Provisioners & bootstrap

Bootstrap options (best first)

Pre-built image (Packer, image pipeline)Everything baked in; fastest, most reproducible boots
user_data = templatefile("cloud-init.yaml", {…})First-boot configuration via cloud-init
Configuration management (Ansible…)Ongoing configuration of long-lived hosts
provisioner "remote-exec" / "local-exec"Last resort: imperative steps Terraform can't track

Useful pieces

terraform_data with triggers_replaceRe-run something when inputs change (replaces null_resource)
cloud-init status --waitOn the machine: wait for cloud-init to finish
sudo cat /var/log/cloud-init-output.logOn the machine: what cloud-init did

06 · Terraform for bare metal / libvirt

Host setup (Ubuntu)

sudo apt install -y qemu-kvm libvirt-daemon-system genisoimageKVM, libvirt, and a tool to build cloud-init ISOs
sudo usermod -aG libvirt $USERAllow your user to manage VMs (log in again)
virsh list --allVMs defined on this host
virsh net-dhcp-leases defaultIP addresses handed out to VMs

Troubleshooting

virsh console node-0Serial console (Ctrl+] to exit)
virsh domblklist node-0Disks attached to a VM
sudo virsh pool-list --allStorage pools (the provider needs one, usually 'default')

07 · Production patterns

Quality gates

terraform fmt -check -recursive && terraform validateFormatting and syntax
tflint --recursiveLint with provider-aware rules
checkov -d . / trivy config .Security misconfiguration scanning
terraform testRun .tftest.hcl tests (Terraform 1.6+)

Policy on plans

terraform plan -out=tfplan && terraform show -json tfplan > plan.jsonMachine-readable plan
conftest test plan.json -p policy/Evaluate OPA/Rego policies against the plan
terraform plan -detailed-exitcodeDrift check: exit 2 = changes