Terraform count and for_each Address Practice

Free Terraform practice for the Terraform Associate (004): pick the address of a count or for_each instance, then see which addresses are gone or new when a list changes or a moved block is added. Check my answer names the slip, such as counting from 1 or forgetting that removing a name shifts every later count index.

Practise

Configuration

Answer to see the working.

Names are made up, and the lines the question is about are highlighted. Gone: nothing in the new configuration takes the object, so Terraform plans to destroy it. New: an address with no object yet.

Practise the exam

The Terraform Associate (004) exam content list links Resource Addressing under objective 4b and the moved block reference under 6d. Start with the free real exam questions, then get the full pack.

Terraform Associate 004167 real exam questions · US$39
Free: 42 questionsGet the full pack

Also try the Terraform variable precedence practice and the Terraform version constraint practice for the same exam.

Terraform FAQ

What address does a count instance get in Terraform?

TYPE.NAME[INDEX], with the index starting at 0. With count = 4, aws_instance.server[0] to aws_instance.server[3] are the instances, and aws_instance.server alone refers to the whole block.

What address does a for_each instance get?

TYPE.NAME["KEY"], where the key is the map key or the set member. each.key is that key, and each.value is the map value. A resource named web with the key "example" is web["example"] after its type.

What happens when I remove an item from the middle of a list used with count?

Each instance takes the list item at its count.index, so every later item moves up one index and the last index is no longer in the configuration. With for_each, instances are named by key, so only the removed key goes.

Can for_each use a list?

No. for_each accepts a map or a set of strings, and it does not convert a list to a set for you. Wrap the list in toset(), or give the variable the type set(string).

Can a block use both count and for_each?

No. You cannot use both count and for_each in the same resource or module block.

What happens when I add count to a resource that had none?

Terraform moves the original object to instance [0] by itself, unless a moved block mentions that resource. HashiCorp recommends writing the moved block anyway, so the change is clear to future readers.

What happens when I switch a resource from count to for_each?

By default, Terraform treats a new address as an instruction to destroy the old object and create a new one. A moved block per instance, such as one from aws_instance.c[1] to aws_instance.c["tiny"], keeps the object.

How do I keep the object when I add for_each to a single resource?

Add a moved block that says which key the object takes, for example from aws_instance.a to aws_instance.a["small"]. Without it, the old address is gone.

What is the address of a resource inside a module that uses for_each?

Resources in a child module with several instances are prefixed with module.NAME[KEY]. For module "bucket" with the key "media", every resource address in that instance starts with module.bucket["media"].

Can for_each use a sensitive variable?

No. Terraform returns an error if you use sensitive values as for_each arguments, because it shows the keys in its output.

When should I use count and when for_each?

HashiCorp says to use count for nearly identical instances, and for_each when some arguments need distinct values that cannot be derived from an integer index.

Which Terraform Associate 004 objectives cover this?

The exam content list links Resource Addressing under objective 4b, refer to resource attributes and create cross-resource references, and the moved block reference under 6d. HashiCorp says the 004 exam tests on Terraform version 1.12.

Sources
  • HashiCorp Developer, count meta-argument: instances and the index starting at 0, TYPE.NAME[INDEX] and the block address, module instance prefixes, and never count and for_each together.
  • HashiCorp Developer, for_each meta-argument: a map or a set of strings, no list to set conversion, each.key and each.value, TYPE.NAME["KEY"], sensitive values, and set conversion dropping order and duplicates.
  • HashiCorp Developer, Refactor modules and the moved block reference: a new address means destroy and create by default, moved blocks keep the object, the automatic move to [0] when count is added, and the moved block needed when for_each is added.
  • HashiCorp Developer, Resource address reference: [module path][resource spec], [N] for count, ["KEY"] for for_each, and an address without an index meaning all instances.
  • Exam fit: HashiCorp, Exam Content List, Terraform Associate 004 (objective 4b links Resource Addressing, 6d links the moved block reference; tests on Terraform version 1.12). The same rules are in the v1.12.x pages.
  • Checked 21 November 2026. The tool's own approach: names and values are invented; gone means no address in the new configuration takes the object, so Terraform plans to destroy it, and new means an address with no object yet. Whether a kept instance is updated in place or replaced depends on the provider, so the drill never asks it. Left out: unknown values, impure functions, -target and terraform state mv. An independent re-solver that reads only the configuration, the question and the buttons agreed with the drill on 18,000 generated problems. This is an independent practice tool, not made or endorsed by HashiCorp.

Checked 21 November 2026 against the HashiCorp Terraform documentation and the Terraform Associate (004) exam content list.

More free tools

All free tools