norboten · cheat sheet

count numbers things; for_each names them — cheat sheet

terraform, networking
resource "x" "y" {
  count = length(var.list)          # instances [0], [1], … — for interchangeable things
  name  = var.list[count.index]
}

resource "x" "y" {
  for_each = var.map                # instances ["key"] — for named things
  name     = each.key
  size     = each.value.size
}

moved {
  from = x.y[0]
  to   = x.y["shop"]
}
$ terraform state list                               # [0] = count, ["key"] = for_each
$ terraform fmt -check && terraform validate
$ terraform plan | grep -E '^  # |^Plan:'            # the actions and their reasons
$ terraform plan -var 'sites={docs={port=8102}}'     # rehearse a change without editing files
$ terraform apply
$ terraform plan -detailed-exitcode; echo $?         # 0 = nothing left to do
$ terraform state mv 'x.y[0]' 'x.y["shop"]'          # the one-off alternative to a moved block
$ cmp before after                                   # prove a file did not change