Courseiva
Interact with Terraform moduleshardMultiple ChoiceObjective-mapped

TF-004 Module Outputs Practice Question

You have two modules that create resources in different providers. Module A creates a VPC in AWS, Module B creates a Kubernetes cluster that requires the VPC ID. You want to ensure Module B runs after Module A but avoid hardcoding the VPC ID. Which approach is most appropriate?

⚠ Common exam trap

Candidates often confuse dependency ordering (via depends_on) with data passing (via outputs/inputs). In Terraform, depends_on only ensures creation order but does not pass data; outputs and input variables are required to share values between modules.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

Output the VPC ID from Module A and pass it as input to Module B.

It establishes an explicit data dependency between modules without hardcoding values. By outputting the VPC ID from Module A and passing it as an input variable to Module B, Terraform's dependency graph automatically ensures Module A is created before Module B, and the VPC ID is dynamically available at plan time.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • Use a data source in Module B to look up the VPC.

    Why it's wrong here

    Using a data source in Module B to look up the VPC created by Module A would fail because data sources query *existing* infrastructure. During the `terraform plan` phase, Module A's VPC resource has not yet been created, only declared within the configuration. Consequently, Module B's data source would attempt to find a non-existent VPC, resulting in a 'resource not found' error and preventing a successful plan from being generated.

  • Output the VPC ID from Module A and pass it as input to Module B.

    Why this is correct

    The correct approach involves Module A exposing the VPC ID through an `output` block, making it accessible to the root module. This output is then referenced in the root module and explicitly passed as an `input variable` to Module B. Terraform's dependency graph automatically infers that Module A must successfully provision the VPC before Module B can consume its ID, ensuring both correct data flow and proper execution order.

  • Use terraform graph to order modules.

    Why it's wrong here

    `terraform graph` is a diagnostic utility that generates a visual representation of the resource and module dependencies within a configuration in DOT format. While it helps operators understand the implicit ordering Terraform will follow, it does not actively enforce or modify the execution order of resources or modules. It merely illustrates the plan Terraform has already determined based on its internal dependency resolution logic, rather than influencing it.

  • Use module dependency via depends_on in the root module.

    Why it's wrong here

    Employing `depends_on` in the root module to establish a dependency between Module B and Module A would ensure Module A completes execution before Module B begins. However, `depends_on` only establishes an ordering constraint; it does not facilitate the transfer of data or attributes between modules. Module B would still lack the dynamically created VPC ID, requiring an alternative mechanism to obtain it, which would likely lead to the same issues as a data source lookup.

About these practice questions

Courseiva writes every TF-004 question from scratch — 428 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This TF-004 practice question is part of Courseiva's free HashiCorp certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the TF-004 exam.