Courseiva

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

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?

⚠ Common exam trap

A common misconception is that required_providers inside a module can select an alias, when in fact it only declares provider requirements and version constraints.

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

✓

Use the providers argument in the module block: providers = { aws = aws.uswest }.

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.

Answer analysis

Option-by-option breakdown

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

  • ✗

    Set required_providers inside the module to include the alias.

    Why it's wrong here

    Setting `required_providers` inside a module defines the module's provider dependencies, specifying attributes like source and version constraints. However, it does not establish a mapping between the module's internal provider name and a specific, potentially aliased, provider configuration from the calling root module. `required_providers` declares *what* providers the module needs, not *how* those needs are fulfilled by specific configurations passed down.

  • ✓

    Use the providers argument in the module block: providers = { aws = aws.uswest }.

    Why this is correct

    The `providers` argument within a `module` block is the correct and explicit mechanism for passing specific provider configurations from the calling module to a child module. By using `providers = { aws = aws.uswest }`, the child module's expectation for an `aws` provider is satisfied by the `uswest` aliased `aws` provider configuration defined in the root module. This ensures the module operates with the intended, aliased provider configuration.

  • ✗

    Include a provider block inside the module block with alias = 'uswest'.

    Why it's wrong here

    Including a `provider` block directly inside a `module` block is syntactically invalid in Terraform. Provider configurations, defined by `provider` blocks, are top-level constructs within a Terraform configuration file or nested within a `terraform` block for specific settings. Attempting to nest a `provider` block within a `module` call will result in a parsing error, as it violates Terraform's configuration language structure.

  • ✗

    Do nothing; Terraform automatically uses the default provider.

    Why it's wrong here

    Doing nothing means the module will attempt to use the default, unaliased provider configuration (e.g., `aws`) available in the calling module. If the module's resources are explicitly configured to use an aliased provider (e.g., `provider = aws.uswest`), this will lead to errors because the required aliased configuration was not provided. Terraform does not automatically infer or map a specific aliased provider when one is explicitly expected by the module.

About these practice questions

One of 434 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

2 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. 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 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: Option D is correct because Terraform allows the same module to be instantiated multiple times within a configuration, and each instance can receive distinct values for its input variables, enabling reuse of the module's logic with different parameters. Option E is correct because the module source attribute supports various source types, including Git repository URLs, and you can pin a specific commit SHA (for example, using a ref query parameter or the ?ref= syntax) to ensure the exact code revision is used. Option A is incorrect because the count meta-argument is supported on module blocks, allowing multiple instances of a module to be created. Option B is incorrect because module outputs are not automatically available as inputs to other modules; they must be explicitly referenced and passed as input variables. Option C is incorrect because version constraints are only required for certain source types such as the Terraform Registry, and are not mandatory for all module sources (e.g., local paths).

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.