Skip to content

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?

  1. ASet source to a list of three directories
  2. BUse for_each on the module block over a map of per-environment settings
  3. CAdd a dynamic "module" block inside the root module
  4. 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.

Question 2 · choose 1

A configuration needs two separate copies of a bucket module, one for logs and one for backups, with different inputs. How is this written?

  1. AOne module block whose label is repeated twice, once for each copy
  2. BTwo module blocks with the same source and different labels
  3. CTwo module blocks with the same label in different files
  4. DOne module block whose source lists the module twice
Show the answer and why
  • AOne module block whose label is repeated twice, once for each copy

    Incorrect

    Block labels must be unique; a repeated label is an error.

  • BTwo module blocks with the same source and different labels

    Correct

    You can use the same source address in several module blocks as long as each block has a unique label.

  • CTwo module blocks with the same label in different files

    Incorrect

    All files form one module, so the labels still collide.

  • DOne module block whose source lists the module twice

    Incorrect

    source takes one location.

A module is a reusable definition; each module block is one named use of it.

Question 3 · choose 1

In Terraform's terminology, what is the root module?

  1. AThe configuration in the working directory
  2. BThe first module listed in the public registry for each provider
  3. CThe module with the most resources in a configuration
  4. DAny module that calls no other modules
Show the answer and why
  • AThe configuration in the working directory

    Correct

    Terraform refers to the configuration in the working directory's root as the root module.

  • BThe first module listed in the public registry for each provider

    Incorrect

    Registry listings have no root module.

  • CThe module with the most resources in a configuration

    Incorrect

    Size does not matter; location does.

  • DAny module that calls no other modules

    Incorrect

    That describes a leaf module, not the root.

Every configuration has exactly one root module: the directory you run in.

Question 4 · choose 1

Can a child module call its own child modules?

  1. ANo; only the root module may contain module blocks
  2. BYes, but only modules from the same Git repository
  3. CYes; a child module can call its own nested child modules
  4. DNo; nested calls are turned into resources by terraform init
Show the answer and why
  • ANo; only the root module may contain module blocks

    Incorrect

    Nested module calls are allowed.

  • BYes, but only modules from the same Git repository

    Incorrect

    Nested calls can use any source type.

  • CYes; a child module can call its own nested child modules

    Correct

    The root module can call a child module that calls its own nested child module.

  • DNo; nested calls are turned into resources by terraform init

    Incorrect

    init installs nested modules like any others.

Nesting works at any level; each module calls only its own children.

Question 5 · choose 1

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?

  1. Anetwork-terraform-google
  2. Bgoogle/network
  3. Ctf-module-network
  4. 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.

Question 6 · choose 1

A bastion host module should be created only when var.enable_bastion is true. What does the module block need?

  1. Aenabled = var.enable_bastion
  2. Bcount = var.enable_bastion ? 1 : 0
  3. Cdepends_on = [var.enable_bastion]
  4. Dfor_each = var.enable_bastion
Show the answer and why
  • Aenabled = var.enable_bastion

    Incorrect

    Module blocks have no enabled argument.

  • Bcount = var.enable_bastion ? 1 : 0

    Correct

    Module blocks accept count, so 0 instances means the module creates nothing.

  • Cdepends_on = [var.enable_bastion]

    Incorrect

    depends_on references resources and modules.

  • Dfor_each = var.enable_bastion

    Incorrect

    for_each needs a map or a set of strings, not a bool.

count on a module block makes the whole module optional.

Practise domain 5 →Practise all domains →