Courseiva
Interact with Terraform moduleshardMultiple SelectObjective-mapped

Terraform Module Configuration: Input Variables, Outputs, and Best Practices

Which THREE statements about module configuration are correct?

Quick Answer

The correct statements are that module sources can be local paths or remote URLs, a module block can contain multiple resources and child modules, and module inputs can be optional if the module defines a default value. This is because Terraform modules are designed as reusable containers that encapsulate infrastructure logic, and input variables with default values allow callers to omit those arguments without causing errors. On the HashiCorp Terraform Associate TF-003 exam, this topic tests your understanding of module configuration best practices and input variables, often appearing in multiple-select questions where common traps include assuming all outputs are unknown during planning or that every source type supports version constraints. A key memory tip is to remember "defaults make inputs optional" and that local paths cannot be versioned, so always check source type before applying version constraints.

⚠ Common exam trap

A common misconception is that version constraints can be applied to any module source, but they only work with registry modules that support semantic versioning.

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

Module sources can be local paths or remote URLs.

Terraform module sources can be either local file paths (e.g., `./modules/network`) or remote URLs (e.g., `git::https://example.com/repo.git`, `registry.terraform.io/hashicorp/consul/aws`). The `source` argument supports multiple protocols including local, Git, HTTP, and the Terraform Registry, making it flexible for version control and distribution.

Answer analysis

Option-by-option breakdown

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

  • Module sources can be local paths or remote URLs.

    Why this is correct

    Terraform supports various source types including local, registry, Git, HTTP, etc.

  • Version constraints can be specified for any module source.

    Why it's wrong here

    Version constraints are supported only for registry and some remote sources; local paths do not support them.

  • The outputs of a module are available after apply only.

    Why it's wrong here

    Outputs may be known during planning if they depend on known values; they are not limited to after apply.

  • A module block can contain multiple resources and child modules.

    Why this is correct

    Modules encapsulate any number of resources and can call other modules.

  • Module inputs can be optional if the module uses a default.

    Why this is correct

    If a variable has a default value, it becomes optional for the caller.

About these practice questions

One of 428 original TF-004 practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

Same concept, more angles

5 more ways this is tested on TF-004

These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.

Variation 1. Which TWO statements about Terraform module structure and best practices are correct? (Choose two.)

hard
  • A.A module should define variables for any values that need to be customized by the calling configuration.
  • B.A module should assume the calling configuration will provide all necessary provider configurations.
  • C.A module must have all Terraform code in a single main.tf file.
  • D.A module can be sourced from the same repository as the root module using a relative path.
  • E.A module should rely on direct attribute references to resources in the calling configuration.

Why A: Terraform modules are designed to be reusable and configurable. **Statement A**: By defining input variables for any customizable values (e.g., resource names, sizes, regions), the module abstracts away hardcoded details and allows the calling configuration to pass in specific values via variable assignments. This follows the principle of encapsulation, where the module's internal logic remains unchanged while its behavior adapts to the caller's needs. **Statement D**: Modules can be sourced from various locations, including local paths relative to the calling configuration. This is a common pattern for organizing smaller, internal modules within the same repository or project directory, allowing for modularity without requiring separate version control repositories for every small module.

Variation 2. Which TWO statements about Terraform modules are correct?

easy
  • A.The count meta-argument is not supported on module blocks.
  • B.Module outputs are automatically available as inputs to other modules in the same configuration.
  • C.Module sources must include a version constraint to ensure reproducibility.
  • D.A module can be called multiple times in the same configuration with different input variables.
  • E.The source attribute of a module can be a Git repository URL with a specific commit SHA.

Why D: Terraform modules can be called multiple times with different input variables, enabling reuse. Option E is correct because module sources can be Git repository URLs with a specific commit SHA (e.g., using `?ref=<sha>`). Option A is incorrect because the `count` meta-argument is supported on module blocks in Terraform 0.13 and later. Option B is incorrect because module outputs are not automatically available; they must be referenced explicitly as `module.<module_name>.<output_name>`. Option C is incorrect because version constraints are optional for reproducibility; they are recommended but not mandatory.

Variation 3. You are developing a module. Which TWO actions are recommended best practices?

medium
  • A.Use the same provider configuration as the root module.
  • B.Use local values to simplify expressions.
  • C.Hardcode default values for all variables.
  • D.Define outputs for all resources created.
  • E.Use absolute paths in module source.

Why B: Options B and D are correct. Using local values (B) simplifies expressions and improves readability within a module, as recommended by Terraform best practices. Defining outputs (D) for resources that need to be consumed by the root module or other modules enables proper data flow and reusability. Option A is incorrect because relying on the same provider configuration as the root module reduces module portability; explicit provider configurations are preferred. Option C is incorrect because hardcoding default values reduces flexibility for module consumers; variables should have sensible defaults but allow overrides. Option E is incorrect because absolute paths make modules less portable; relative paths or module registries are preferred.

Variation 4. After running `terraform plan`, the user receives an error: `Error: Missing required variable`. The variable 'vpc_cidr' is provided. What is the most likely cause?

medium
  • A.The module requires a variable 'environment' that is not passed.
  • B.The module block syntax is incorrect.
  • C.The variable 'vpc_cidr' is misspelled.
  • D.The variable 'vpc_cidr' conflicts with a provider variable.

Why A: The error 'Missing required variable' indicates that a variable required by the module has not been provided. Even though 'vpc_cidr' is supplied, the module likely defines a required input variable named 'environment' without a default value, and the user did not pass it in the module block. Terraform enforces that all required variables without defaults must be explicitly set by the caller.

Variation 5. A module requires a specific provider configuration with aliases. The root module has two provider configurations: provider 'aws' (default) and provider 'aws' with alias = 'uswest'. The module uses the us-west alias. How should the module block be configured to ensure the correct provider is used?

hard
  • A.Set required_providers inside the module to include the alias.
  • B.Use the providers argument in the module block: providers = { aws = aws.uswest }.
  • C.Include a provider block inside the module block with alias = 'uswest'.
  • D.Do nothing; Terraform automatically uses the default provider.

Why B: Terraform uses the `providers` argument in a module block to explicitly map provider configurations from the root module into the module. Since the root module has two AWS provider configurations (default and `uswest` alias), and the module requires the us-west alias, the mapping `providers = { aws = aws.uswest }` ensures the module uses the aliased provider. Without this explicit mapping, Terraform would default to the root module's default provider, which may not have the correct region or settings.

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.