Skip to content
Hands-on IT IT Drills

Appendix A: Cheat Sheet

Everything important from Days 0–15 on a few pages. The number in parentheses is the day you practiced it. When in doubt, tofu -help, tofu <command> -help, and the documentation at opentofu.org/docs are authoritative.

The Workflow

Command What it does Day
tofu init download providers and modules, set up the backend 1
tofu init -upgrade fetch newer allowed provider versions 15
tofu fmt / -check format / check only 13
tofu validate check syntax, types, references 13
tofu plan compare desired and actual state 1
tofu plan -out=x.plan save the plan 13
tofu apply / tofu apply x.plan execute / apply a saved plan 1, 14
tofu destroy tear down everything in the state 1
tofu output / -raw name outputs / one value without quotes 2
tofu console try out expressions 0
tofu test run *.tftest.hcl 13
tofu show -json x.plan plan as JSON (for Conftest) 13

Useful flags:

Flag Effect Day
-auto-approve no confirmation prompt – only in automation 1
-var 'x=1', -var-file=prod.tfvars values from the outside 2
-replace=adresse recreate a resource 3
-target=adresse only this resource – a stopgap 3
-refresh-only only align the state with reality 3
-detailed-exitcode exit 0 = nothing, 1 = error, 2 = changes 14
-lock-timeout=30s wait for a lock 15
-chdir=verzeichnis work in a different directory 10
-generate-config-out=x.tf suggest configuration for imports 11

Reading the Plan

Symbol Meaning Day
+ create 1
~ update in place 1
-/+ delete and recreate (# forces replacement) 1
- delete 1
has moved to rename via moved 11
will be removed from the OpenTofu state removed, destroy = false 11
(sensitive value) / (write-only attribute) value hidden / never stored 12

Language

variable "port" {                             # (2)
  type    = number
  default = 8080
  validation {                                # (13)
    condition     = var.port >= 1024
    error_message = "zu klein"
  }
}
locals { name = "notes-${var.umgebung}" }     # (2)
resource "docker_volume" "db" {               # (1)
  for_each = toset(["a", "b"])                # (4) – count = 2 would work too
  name     = "${local.name}-${each.key}"
  lifecycle {
    prevent_destroy = true                    # (4)
    ignore_changes  = [labels]                # (6)
  }
}
data "docker_image" "api" { name = "notes-api:0.4.0" }        # (4)
module "notes" { source = "git::file:///…//notes-docker?ref=v1.2.0" }   # (5)
output "url" { value = module.notes.url }     # (2)
Expression Result Day
[for k, v in m : "${k}=${v}"] list from a map 4
{ for n in l : n => upper(n) } map from a list 4
dynamic "block" { for_each = … } repeat blocks 4
templatefile("x.tftpl", { … }) fill a file with placeholders 2
yamlencode(…), jsonencode(…) values as YAML/JSON 7
pathexpand("~") home directory 7
sha256(x) checksum, e.g. as an annotation 6
one(menge), strcontains(t, x) single element, contains 13
terraform.workspace name of the workspace 10

State

Command What it does Day
tofu state list all addresses 1
tofu state show adresse one resource in detail 3
tofu state pull the whole state as JSON 3
tofu state rm adresse remove from the state, the resource stays 15
tofu force-unlock ID release an orphaned lock 9, 15
tofu import adresse id import immediately (without a plan) 11
tofu workspace new/select/list/show/delete workspaces 10

Blocks instead of commands – they go through review and plan:

import  { to = docker_volume.x, id = "name" }                  # (11)
moved   { from = docker_volume.alt, to = docker_volume.neu }   # (11)
removed {                                                      # (11)
  from = docker_network.netz
  lifecycle { destroy = false }
}

Backend and encryption (Day 9):

terraform {
  backend "s3" {
    bucket       = "tofu-state"
    key          = "tag05/terraform.tfstate"
    use_lockfile = true
    # endpoints, use_path_style, skip_* for S3-compatible storage
  }
  encryption {
    key_provider "pbkdf2" "lab" { passphrase = var.state_passphrase }
    method "aes_gcm" "lab" { keys = key_provider.pbkdf2.lab }
    state { method = method.aes_gcm.lab }
  }
}

Secrets

Means protects Day
sensitive = true output (plan, log) 12
*.auto.tfvars in .gitignore, TF_VAR_name code 12
ephemeral = true, ephemeral "…" plan and state 12
attributes ending in _wo + _wo_revision state; the value still reaches the target 12
state encryption state in the bucket 9

Quality and CI

Tool Command Day
tflint tflint 13
tofu test tofu test, -filter=tests/x.tftest.hcl 13
Trivy trivy config --severity HIGH,CRITICAL --exit-code 1 . 13
Conftest conftest test -p policy plan.json 13
tea tea pr create, tea pr merge 1, tea actions runs list 14
tea tea actions workflows dispatch x.yml --ref main --follow 14

Error Patterns

Message Cause Day
timeout while waiting for state … 'removed' reference by name instead of ID 1
Resource instance cannot be destroyed prevent_destroy 4
hashicorp/docker: provider registry … does not have required_providers missing 5
Error acquiring the state lock lock active or orphaned 9, 15
decryption failed for all provided methods wrong passphrase 9, 15
Ephemeral value used in non-ephemeral context ephemeral in a normal attribute 12
Output refers to sensitive values output without sensitive = true 12
Conflict. The container name … is already in use resource missing from the state 15
no available releases match the given constraints version doesn’t exist 15
invalid ref module version not released 15

Differences from Terraform

  • Registry: registry.opentofu.org instead of registry.terraform.io – the providers are the same (1).
  • State encryption (encryption) exists only in OpenTofu (9).
  • Commands, language, backends, import/moved/removed, tofu test (in Terraform terraform test), and write-only attributes work in both.