Language

Loops & Meta-Arguments · Lesson 11 of 30

count & for_each

Source: doonops-curriculum/meta-arguments--count-foreach.md

Doonops lesson

Goal

count vs for_each vs "for" — stop confusing them with Python loops.

Simple explanation

Terraform is NOT Python. To make many similar resources you use count (numbered copies) or for_each (named copies from a map/set).

Technical view

Meta-arguments count and for_each on modules/resources; for expressions in attributes; dynamic blocks; lifecycle ignore_changes.

Think of it like

count = photocopy 3 identical flyers (page 0,1,2). for_each = print one flyer per city name from a list (Mumbai, Delhi).

Steps

  1. Read simple section
  2. Read analogy + diagram
  3. Copy project files
  4. Do local lab

Deep explanation

Layman words first, then technical detail — read slowly

First: Terraform "for loop" confusion

Beginners search "for loop in Terraform" because they know Python. In Terraform you usually mean one of these:

You wantUseLayman
3 identical serverscount = 33 photocopies
1 server per name in a listfor_each = toset(...)1 per label
Transform a list inside one fieldfor = expression in attributeExcel formula inside one cell

count — numbered copies (0, 1, 2…)

resource "aws_subnet" "private" {
  count = 2                    ← make 2 subnets

  cidr_block = var.cidrs[count.index]   ← index 0 then 1
}

Simple: count = "how many clones". count.index = clone number starting 0.

Downside: remove item from middle of list → Terraform may destroy/recreate wrong one (index shift). Exam loves this trap.

for_each — one resource per map key (stable names)

variable "subnets" {
  default = {
    app = "10.0.1.0/24"
    db  = "10.0.2.0/24"
  }
}

resource "aws_subnet" "this" {
  for_each = var.subnets     ← key = app, db

  cidr_block = each.value    ← CIDR for that key
  tags       = { Name = each.key }
}

Simple: each.key = name (app, db). each.value = value (CIDR).

When better: named environments, stable keys, exam answer "use for_each when keys matter".

for expression (inside one attribute — not same as count)

tags = { for k, v in var.extra_tags : k => v if v != "" }

This builds a map — does NOT create 5 resources by itself.

Quick pick guide

  • Need N copies of same thing → count
  • Need named items from map/set → for_each
  • Need to tweak one field from list → for expression

Example (Doonops)

Modern HCL — names are examples, not from any third-party course

Example HCL
HCL
variable "subnets" {
  type = map(string)
  default = {
    app = "10.0.1.0/24"
    db  = "10.0.2.0/24"
  }
}

resource "aws_subnet" "named" {
  for_each   = var.subnets
  vpc_id     = var.vpc_id
  cidr_block = each.value
  tags       = { Name = each.key }
}

Terraform runs on your computer — copy this HCL into a folder, then follow the local lab steps below.

Check

  • count vs for_each one line each
  • what each.key is
  • Python for loop same? (no)

Project files for this lab

Full implementation folder — copy all files, then run terraform commands

Lab project files (full folder)

Copy every file below into one folder — same as a real repo module. Then run the local lab steps.

Suggested folder: Suggested path: ~/doonops-terraform/08-meta-args/

versions.tfSee file purpose in the code below
terraform {
  required_version = ">= 1.9.0"
}

Module check — did you get it?

2–3 quick questions before the next module

Meta-args — quick check

Quick check — did this module stick?

1. for_each is best when…

  • You need identical numbered resources only
  • You iterate over a set/map of named items
  • You never use modules
  • You replace state