004 · Domain 5: Terraform modules · 12% of the exam
Task 5.3: Use modules in configuration
Calling a module, passing arguments, using its outputs, count and for_each on module blocks, module composition, and moving resources into modules without destroying them.
Study it
Using modules: calls, for_each, composition and refactoring
Lesson coming
Sample questions
Try each one before opening the answer. Every option is explained, with the HashiCorp documentation page that proves it.
Question 1 · choose 1
A team calls the same app module three times, once each for dev, stage and prod, with three copies of the module block that differ only in a few inputs. How can one module block do this without changing the module itself?
ASet source to a list of three directories
BUse for_each on the module block over a map of per-environment settings
CAdd a dynamic "module" block inside the root module
DCall the module once and let its variables accept lists of environments
Show the answer and why
ASet source to a list of three directories
Incorrect
source takes a single module location, not a list.
BUse for_each on the module block over a map of per-environment settings
Correct
Module blocks accept for_each (or count), creating one module instance per element.
CAdd a dynamic "module" block inside the root module
Incorrect
dynamic generates nested blocks inside resources, not module calls.
DCall the module once and let its variables accept lists of environments
Incorrect
Possible only by rewriting the module; for_each needs no module changes.
count and for_each work on module blocks just as on resources.
A team wants to publish its module for the google provider, called "network", to the public Terraform Registry. What must the GitHub repository be named?
Anetwork-terraform-google
Bgoogle/network
Ctf-module-network
Dterraform-google-network
Show the answer and why
Anetwork-terraform-google
Incorrect
The order is terraform, provider, name.
Bgoogle/network
Incorrect
That is closer to a source address than a repository name.
Ctf-module-network
Incorrect
The name must include terraform and the provider.
Dterraform-google-network
Correct
Public registry module repositories must use the name terraform-<PROVIDER>-<NAME>.
terraform-<PROVIDER>-<NAME> is how the registry discovers modules on GitHub.