Module 4: Terraform Configuration

Lab 4B: count, for_each, depends_on, and lifecycle

Time
25 min
Cost
$0
Prerequisites
None listed
Objectives
0 of 6 checked

Starting file

Create ~/tf-course/lab4b/main.tf:

~/tf-course/lab4b/main.tfHCL
terraform {
  required_version = "~> 1.12.0"

  required_providers {
    local = {
      source  = "hashicorp/local"
      version = "~> 2.5"
    }
  }
}

variable "users" {
  type    = list(string)
  default = ["alice", "bob", "carol"]
}

variable "release" {
  type    = string
  default = "v1"
}

# ---- count vs for_each ----
resource "local_file" "by_count" {
  count    = length(var.users)
  filename = "${path.module}/count/${var.users[count.index]}.txt"
  content  = "user ${count.index}: ${var.users[count.index]}\n"
}

resource "local_file" "by_key" {
  for_each = toset(var.users)
  filename = "${path.module}/foreach/${each.key}.txt"
  content  = "user: ${each.value}\n"
}

# ---- explicit dependency ----
resource "terraform_data" "bootstrap" {
  input = "bootstrap-complete"
}

resource "local_file" "app_config" {
  filename = "${path.module}/app.conf"
  content  = "ready = true\n"

  # No reference to bootstrap exists, so declare the ordering explicitly.
  depends_on = [terraform_data.bootstrap]
}

# ---- lifecycle ----
resource "terraform_data" "release" {
  triggers_replace = var.release
}

resource "local_file" "release_notes" {
  filename = "${path.module}/release.txt"
  content  = "deployed\n"

  lifecycle {
    replace_triggered_by = [terraform_data.release]
  }
}

resource "terraform_data" "cbd_demo" {
  triggers_replace = var.release

  lifecycle {
    create_before_destroy = true
  }
}

resource "local_file" "hand_tuned" {
  filename = "${path.module}/tuned.txt"
  content  = "initial\n"

  lifecycle {
    ignore_changes = [content]
  }
}

resource "local_file" "protected" {
  filename = "${path.module}/protected.txt"
  content  = "do not delete\n"

  lifecycle {
    prevent_destroy = true
  }
}

Steps

Shell
cd ~/tf-course/lab4b
terraform init && terraform apply -auto-approve

Expected output: terraform_data.bootstrap: Creation complete appears before local_file.app_config: Creating....

Shell
terraform state list | grep -E 'by_count|by_key'

Expected output: local_file.by_count[0] through [2], and local_file.by_key["alice"], ["bob"], ["carol"].

Shell
terraform plan -var 'users=["alice","carol"]'

Expected output:

  • by_count[1] is replaced (-/+), because its content and filename change from bob to carol.
  • by_count[2] is destroyed.
  • Only by_key["bob"] is destroyed in the for_each set.
  • The summary reads Plan: 1 to add, 0 to change, 3 to destroy.

Don't apply.

Shell
terraform plan -var 'release=v2'

Expected output:

  • terraform_data.release shows -/+.
  • local_file.release_notes shows -/+, with a note that replacement was triggered by a change in a referenced resource.
  • terraform_data.cbd_demo shows +/- (create replacement and then destroy).

Apply this one:

Shell
terraform apply -auto-approve -var 'release=v2'

Change hand_tuned's content to "changed\n" in main.tf, then run:

main.tfShell
terraform plan -var 'release=v2'

Expected output: No changes. The content difference is ignored. Revert the edit.

Shell
terraform destroy -var 'release=v2'

Expected output: an error saying local_file.protected has lifecycle.prevent_destroy set and can't be destroyed. Nothing is destroyed.

Cleanup

Change prevent_destroy = true to false, then:

Shell
terraform destroy -auto-approve -var 'release=v2'

Lab 4B complete. Every step is checked.

This lab has no verification checklist in the course file, so it counts as complete when every step is checked.