HashiCorp Terraform Associate (004) exam dumps

HashiCorp Terraform Associate (004) practice question 102 of 223

Terraform Associate 004. Associate level, HashiCorp. Free question with the correct answer and a full explanation.

HashiCorp Terraform Associate (004) Question 102

Single answer4c Use variables and outputs

Your team maintains a reusable Terraform module that provisions an application load balancer and returns its DNS name. Another team calls this module from a root module and wants to control the environment-specific naming convention while also displaying the final DNS name after apply. They want a solution that keeps the module reusable across dev, staging, and prod without hardcoding values inside the module. Which approach best meets these requirements?

  1. A

    Define an input variable in the child module for the environment name, use that variable in resource naming, and define an output in the child module for the load balancer DNS name. In the root module, pass the environment value into the module block and reference the child module output.

  2. B

    Reference a root module local value directly from inside the child module for the environment name, and print the load balancer DNS name using terraform console after apply instead of defining an output.

  3. C

    Create the environment name as an output in the root module so the child module can read it automatically, and define the load balancer DNS name only as a local value inside the child module.

  4. D

    Hardcode the environment name inside the child module for consistency, and expose the load balancer DNS name by reading the state file manually from the root module.

Show answer and explanation

Correct answer: A

Explanation

Terraform modules are designed to have clear interfaces: input variables accept values into a module, and outputs return selected values to the caller. In this scenario, the child module should declare a variable such as environment and use it in naming logic, while also declaring an output for the load balancer DNS name. The root module passes the environment-specific value in the module block and can reference the result with module.<module_name>.<output_name>. This is consistent with Terraform documentation and best practices for reusable modules. A key exam concept is understanding that variables flow into modules, outputs flow out of modules, and values are not shared implicitly across module boundaries.

  • A. Correct.

    Correct. This is the standard Terraform pattern for reusable modules: pass configurable values into a child module using input variables and expose values from the child module using outputs. The root module can set different values for different environments in the module block, and then reference the returned value as module... This keeps the module portable and avoids coupling it to a specific environment.

  • B. Incorrect.

    Incorrect. Child modules cannot directly access local values, variables, or resources from the root module unless those values are explicitly passed in as input arguments. Also, using terraform console is not a substitute for a module output when a value should be intentionally exposed for downstream use or visibility after apply.

  • C. Incorrect.

    Incorrect. Outputs do not flow automatically from the root module into child modules. Outputs expose values outward from a module to its caller; they are not an input mechanism. Likewise, if the DNS name needs to be exposed to the calling module, defining it only as a local value in the child module will not make it available outside that module.

  • D. Incorrect.

    Incorrect. Hardcoding the environment name inside the child module reduces reusability and violates the requirement to support multiple environments without changing module code. Reading the state file manually is not the intended mechanism for consuming module values; outputs are the supported and safer interface for exposing data from a module.

Timed practice exam

Take a HashiCorp Terraform Associate (004) practice test under exam conditions

70 questions in 60 minutes, drawn from this bank, with a score report and a per-question review when you finish.

Start timed exam