Domain 5 of 8 · Chapter 3 of 4

Using modules in configuration

Unlock the complete study guide + 1,040 practice questions across 16 full exams.

Bundled into the existing HashiCorp Certified: Terraform Associate premium course — no separate purchase.

14-day money-back guarantee — no questions asked.

Included in this chapter:

  • Calling a child module
  • Running many instances with count and for_each
  • Wiring a module: providers and depends_on
  • Exam-pattern recognition

Plain call vs count vs for_each on a module

AspectPlain module blockcountfor_each
Instances createdExactly oneA whole number NOne per map or set element
Instance addressmodule.NAMEmodule.NAME[0], [1], …module.NAME["key"]
Key available insideNonecount.index (0-based)each.key and each.value
Accepted value typen/aWhole numberMap or set of strings, not a list
Best fitA single instanceNearly identical copiesCopies with stable identities

Decision tree

More than one instance? One module blockmodule.NAME Integer index enough? count = Nmodule.NAME[0], [1] Stable string keys? for_each map/setmodule.NAME["key"] Build a keyed map, then for_each No Yes Yes No Yes No

Cheat sheet

  • A module block needs a local name and a source
  • Inputs are passed as arguments matching child variable names
  • Modules must be installed before plan or apply
  • count creates numbered module instances
  • for_each creates keyed module instances
  • count and for_each cannot both be set
  • The providers meta-argument maps parent providers into a child
  • depends_on on a module applies to all its resources

Unlock with Premium — includes all practice exams and the complete study guide.

References

  1. Terraform language: module block (calling modules)
  2. Terraform tutorial: Use registry modules in configuration
  3. Terraform language: the count meta-argument
  4. Terraform language: the for_each meta-argument
  5. Terraform language: providers within modules
  6. Terraform language: the depends_on meta-argument