Terraform Associate · Domain 5 of 8 · 11% of the exam

Terraform Associate Domain 5: Terraform Modules

22 free practice questions for Terraform Modules in the HashiCorp Certified: Terraform Associate (004) exam. Try each question first, then open "Show answer" for the correct option and explanation.

Terraform Modules practice questions

  1. Question 1. What is a Terraform module?

    1. A provider plugin binary
    2. A set of configuration files in one directory
    3. A saved plan file for an apply
    4. A backend that stores state
    Show answer

    Answer: B. A set of configuration files in one directory

    Every configuration is a module. Modules called from another module are child modules.

  2. Question 2. What is the root module?

    1. The module in the working directory where you run Terraform
    2. The first module listed on the registry
    3. A module named root in the modules folder
    4. The module that holds the provider binaries
    Show answer

    Answer: A. The module in the working directory where you run Terraform

    The root module can call child modules, which can call others.

  3. Question 3. Which block calls a child module?

    1. import "network" { path = ... }
    2. include "network" { file = ... }
    3. use "network" { from = ... }
    4. module "network" { source = ... }
    Show answer

    Answer: D. module "network" { source = ... }

    source is required. Other arguments set the child module's input variables.

  4. Question 4. Which source format refers to a module on the public Terraform Registry?

    1. registry://terraform-aws-modules/vpc?provider=aws
    2. <NAMESPACE>/<NAME>/<PROVIDER>, such as terraform-aws-modules/vpc/aws
    3. module.terraform-aws-modules.vpc.aws, using the module address
    4. https://modules.terraform.io/vpc/aws as a full download URL
    Show answer

    Answer: B. <NAMESPACE>/<NAME>/<PROVIDER>, such as terraform-aws-modules/vpc/aws

    Private registry modules add a hostname, for example app.terraform.io/<ORG>/<NAME>/<PROVIDER>.

  5. Question 5. For which module sources can you set the version argument in a module block?

    1. Any source, including local paths
    2. Only Git sources
    3. Only modules from a registry
    4. Only modules in subdirectories
    Show answer

    Answer: C. Only modules from a registry

    For Git sources, pin a version with ?ref= in the URL. Local paths have no version.

  6. Question 6. How do you use a module from a Git repository at tag v1.2.0?

    1. source = "git::https://example.com/vpc.git?ref=v1.2.0"
    2. source = "https://example.com/vpc.git" with version = "1.2.0"
    3. source = "git://[email protected]"
    4. source = "vpc.git#1.2.0"
    Show answer

    Answer: A. source = "git::https://example.com/vpc.git?ref=v1.2.0"

    ref accepts a branch, tag, or commit. Pinning to a tag or commit keeps runs repeatable.

  7. Question 7. How does Terraform recognize a local module source?

    1. It ends in .tf
    2. It's an absolute path only
    3. It's listed in required_providers
    4. It starts with ./ or ../
    Show answer

    Answer: D. It starts with ./ or ../

    For example, source = "./modules/network". Local modules aren't downloaded, so changes take effect at once.

  8. Question 8. You add a new module block to a configuration. What must you do before terraform plan?

    1. Nothing; plan downloads it
    2. Run terraform init (or terraform get) to install it
    3. Commit the module to version control
    4. Run terraform apply -target on the module
    Show answer

    Answer: B. Run terraform init (or terraform get) to install it

    init installs modules into .terraform/modules. Plan fails with a module not installed error otherwise.

  9. Question 9. How do you pass a value into a child module?

    1. Write it to a shared tfvars file
    2. Set an environment variable for the module
    3. Set the module's input variable as an argument in the module block
    4. Reference it with var. from inside the child
    Show answer

    Answer: C. Set the module's input variable as an argument in the module block

    Arguments in the module block, other than meta-arguments like source and count, set the child's variables.

  10. Question 10. How does the root module use output vpc_id from module "network"?

    1. module.network.vpc_id
    2. output.network.vpc_id
    3. network.outputs.vpc_id
    4. var.network.vpc_id
    Show answer

    Answer: A. module.network.vpc_id

    Only values the child declares as outputs are visible to the caller.

  11. Question 11. Can a child module read the parent module's variables directly?

    1. Yes, all variables are global
    2. Yes, with the parent. prefix
    3. Only if they're marked sensitive
    4. No; values must be passed in as inputs
    Show answer

    Answer: D. No; values must be passed in as inputs

    Each module has its own namespace. That keeps modules reusable and their interface explicit.

  12. Question 12. Can the root module reference a resource inside a child module directly, such as module.network.aws_vpc.this.id?

    1. Yes, any attribute is accessible
    2. No, only through outputs the child declares
    3. Yes, if the resource uses count
    4. Only in terraform console
    Show answer

    Answer: B. No, only through outputs the child declares

    Expose what callers need with output blocks in the child module.

  13. Question 13. Can you use count or for_each on a module block?

    1. No, they can only be used on resources
    2. Only count works, never for_each
    3. Yes, to create several instances of the module
    4. Only when the module source is a local path
    Show answer

    Answer: C. Yes, to create several instances of the module

    Instances are addressed as module.network[0] or module.network["east"].

  14. Question 14. If a child module doesn't receive a providers argument, which provider configurations does it use?

    1. The default provider configurations inherited from the parent
    2. None, so the module fails
    3. Configurations defined in its README
    4. The provider in the most recent state file
    Show answer

    Answer: A. The default provider configurations inherited from the parent

    Child modules shouldn't define provider blocks themselves. They declare required_providers and receive configurations from the caller.

  15. Question 15. Why should you constrain the versions of modules you consume?

    1. Terraform refuses to install modules without one
    2. Pinning a version makes every plan run faster
    3. Without a constraint, modules can't expose outputs
    4. So upstream changes don't alter your infrastructure unexpectedly
    Show answer

    Answer: D. So upstream changes don't alter your infrastructure unexpectedly

    Upgrade deliberately, read the changelog, and review the plan.

  16. Question 16. What naming pattern must a module repository follow to be published on the public Terraform Registry?

    1. module-<NAME>
    2. terraform-<PROVIDER>-<NAME>
    3. <PROVIDER>_<NAME>_module
    4. tf-<NAME>
    Show answer

    Answer: B. terraform-<PROVIDER>-<NAME>

    For example terraform-aws-vpc. The registry also expects the standard module structure and semantic version tags.

  17. Question 17. Where does terraform init put downloaded modules?

    1. In the state file
    2. Next to the root module's main.tf
    3. In .terraform/modules
    4. In the provider plugin cache
    Show answer

    Answer: C. In .terraform/modules

    Like providers, this directory is created by init and shouldn't be committed.

  18. Question 18. Which HCP Terraform feature lets an organization publish and share its own modules internally?

    1. The private registry
    2. Sentinel policies
    3. Run tasks
    4. Variable sets
    Show answer

    Answer: A. The private registry

    The private registry works like the public one but is visible only inside the organization.

  19. Question 19. What does the standard module structure usually include?

    1. Only a single main.tf file
    2. A state file and a lock file
    3. A providers.lock and a Makefile
    4. main.tf, variables.tf, outputs.tf, and a README
    Show answer

    Answer: D. main.tf, variables.tf, outputs.tf, and a README

    The registry uses this layout to generate documentation for inputs and outputs.

  20. Question 20. In state, what is the address of resource aws_vpc.this created by module "network"?

    1. aws_vpc.this.network
    2. module.network.aws_vpc.this
    3. network/aws_vpc/this
    4. module[network].aws_vpc
    Show answer

    Answer: B. module.network.aws_vpc.this

    Nested modules chain addresses, for example module.app.module.network.aws_vpc.this.

  21. Question 21. Can a module block's source argument be set from a variable?

    1. Yes, any expression works
    2. Only if the variable has a default
    3. No, source must be a literal string
    4. Only for registry modules
    Show answer

    Answer: C. No, source must be a literal string

    Terraform installs modules during init, before variables are evaluated.

  22. Question 22. A child module's output is marked sensitive. What happens if the root module outputs that value without sensitive = true?

    1. Terraform reports an error until the root output also sets sensitive = true
    2. The value prints in plain text in the apply output
    3. The value is removed from state so it can't leak
    4. The child module's output silently becomes non-sensitive
    Show answer

    Answer: A. Terraform reports an error until the root output also sets sensitive = true

    Terraform won't silently drop sensitivity, so the root output must also be declared sensitive.

HashiCorp, Terraform, and HCP Terraform are trademarks of HashiCorp, Inc. These questions are original study material, not questions from the real exam.