World map

 

BM: Baca dalam Bahasa Melayu

World T2: Inputs & Outputs

count: many of the same

You will learn: to make numbered copies with count, and the trap when a list changes.

Checked with OpenTofu 1.13 on 1 Nov 2026

The problem

The fair needs three helpers, not two. Each helper is a random_pet name. How do you get a third?

Pick one answer.

Watch

Type tofu state list. Look at the numbers in square brackets.

Blueprint (your .tf files)init done

  • local_file.stall[0]
  • local_file.stall[1]
  • local_file.stall[2]
  • random_pet.helper[0]
  • random_pet.helper[1]

Built (the state file)state #1

  • local_file.stall[0]
  • local_file.stall[1]
  • local_file.stall[2]
  • random_pet.helper[0]
  • random_pet.helper[1]
Blueprint: 5 resources. Built: 5.

Change a file, then run tofu plan to see what it means.

count: Make N copies of one resource. They are numbered from 0: helper[0], helper[1], helper[2]. makes numbered copies of one block. Numbers start at 0.

Predict

With count = 3, what is the last helper called?

resource "random_pet" "helper" {
  count  = 3
  length = 1
}

Pick one answer.

Try it

Change count = 2 to count = 3 in main.tf. Then tofu apply, and yes.

Blueprint (your .tf files)init done

  • local_file.stall[0]
  • local_file.stall[1]
  • local_file.stall[2]
  • random_pet.helper[0]
  • random_pet.helper[1]

Built (the state file)state #1

  • local_file.stall[0]
  • local_file.stall[1]
  • local_file.stall[2]
  • random_pet.helper[0]
  • random_pet.helper[1]
Blueprint: 5 resources. Built: 5.

Change a file, then run tofu plan to see what it means.

Try it

The stall files use count over the list ["nasi", "mee", "roti"]. Mee closes, so β€œmee” is removed. Read the plan.

Read the plan. What will OpenTofu do to each resource?

+ create~ update in place- destroy-/+ replace

0 of 2 read

  1.   # local_file.stall[1]
    -/+ resource "local_file" "stall" {
          ~ content              = <<-EOT # forces replacement
              - mee
              + roti
            EOT
          ~ content_base64sha256 = "xw5MNjhCTMNH45ODMPkWFjwoOHfZt+UEJabrwAZ67tg=" -> (known after apply)
          ~ content_base64sha512 = "lG9qpqYeP6RSjpXH8yBxi+tyoJUb2f/+AssBJQEUqiAwOZ31uEMdlf1P3UtgRymObu/XdfUPLC0A0b6tBVFjDA==" -> (known after apply)
          ~ content_md5          = "b23fa69b77d3a3bbca08e849c423081a" -> (known after apply)
          ~ content_sha1         = "71498255cfdf84dc20121e7aa04cf834144775d3" -> (known after apply)
          ~ content_sha256       = "c70e4c3638424cc347e3938330f916163c283877d9b7e50425a6ebc0067aeed8" -> (known after apply)
          ~ content_sha512       = "946f6aa6a61e3fa4528e95c7f320718beb72a0951bd9fffe02cb01250114aa2030399df5b8431d95fd4fdd4b6047298e6eefd775f50f2c2d00d1bead0551630c" -> (known after apply)
          ~ id                   = "71498255cfdf84dc20121e7aa04cf834144775d3" -> (known after apply)
            # (3 unchanged attributes hidden)
        }
    

    What happens to local_file.stall[1]?

  2.   # local_file.stall[2]
      - resource "local_file" "stall" {
          - content              = <<-EOT
                roti
            EOT -> null
          - content_base64sha256 = "SEWwusjRzMmcgHMYLkMaJVKyWgYdkr6JxfK/UiP94H8=" -> null
          - content_base64sha512 = "WtTIn23DLCCyc2eounF6CiJuJNNaXkPCnXaioM6bm3p1TjXMei8mJTgxaZ4a0AgG9CUembDZwfUQZ8QZaT/g7Q==" -> null
          - content_md5          = "9840c82b4735606f32cd305f2093596e" -> null
          - content_sha1         = "d04b7ff98f49c98a903d8f3c3dfe1a0d03759751" -> null
          - content_sha256       = "4845b0bac8d1ccc99c8073182e431a2552b25a061d92be89c5f2bf5223fde07f" -> null
          - content_sha512       = "5ad4c89f6dc32c20b27367a8ba717a0a226e24d35a5e43c29d76a2a0ce9b9b7a754e35cc7a2f26253831699e1ad00806f4251e99b0d9c1f51067c419693fe0ed" -> null
          - directory_permission = "0777" -> null
          - file_permission      = "0777" -> null
          - filename             = "stall-2.txt" -> null
          - id                   = "d04b7ff98f49c98a903d8f3c3dfe1a0d03759751" -> null
        }
    

    What happens to local_file.stall[2]?

The summary line shows when you finish.

Check

Only mee closed. Why does the plan touch two files?

Pick one answer.

You did it!

You can now make copies with count, and spot its number trap.

Go deeper (optional)

random_pet.helper[*].id lists every copy’s id; that [*] is a splat. count = var.open ? 1 : 0 turns a resource on or off. For a list that changes, for_each names each copy by its item instead of a number. That is the next lesson.