Module 4: Terraform Configuration
Lab 4B: count, for_each, depends_on, and lifecycle
- Time
- 25 min
- Cost
- $0
- Prerequisites
- None listed
Starting file
Create ~/tf-course/lab4b/main.tf:
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
cd ~/tf-course/lab4b
terraform init && terraform apply -auto-approveExpected output: terraform_data.bootstrap: Creation complete appears before local_file.app_config: Creating....
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"].
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 thefor_eachset. - The summary reads
Plan: 1 to add, 0 to change, 3 to destroy.
Don't apply.
terraform plan -var 'release=v2'Expected output:
terraform_data.releaseshows-/+.local_file.release_notesshows-/+, with a note that replacement was triggered by a change in a referenced resource.terraform_data.cbd_demoshows+/-(create replacement and then destroy).
Apply this one:
terraform apply -auto-approve -var 'release=v2'Change hand_tuned's content to "changed\n" in main.tf, then run:
terraform plan -var 'release=v2'Expected output: No changes. The content difference is ignored. Revert the edit.
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:
terraform destroy -auto-approve -var 'release=v2'This lab has no verification checklist in the course file, so it counts as complete when every step is checked.