HashiCorp Terraform Associate (004) exam dumps

HashiCorp Terraform Associate (004) practice question 37 of 223

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

HashiCorp Terraform Associate (004) Question 37

Single answer2c Write Terraform configuration using multiple providers

Your team maintains a Terraform configuration that must create networking resources in two AWS regions: a VPC in us-east-1 and a disaster recovery subnet in us-west-2. A developer wrote the following configuration, but terraform plan shows that both resources will use the default AWS provider configuration in us-east-1:

provider "aws" { region = "us-east-1" }

provider "aws" { alias = "west" region = "us-west-2" }

resource "aws_vpc" "primary" { cidr_block = "10.0.0.0/16" }

resource "aws_subnet" "dr" { vpc_id = "vpc-1234567890abcdef0" cidr_block = "10.1.1.0/24" }

You need to ensure only the DR subnet is managed in us-west-2 while the VPC remains in us-east-1. Which change is the best solution?

  1. A

    Add provider = aws.west inside the aws_subnet" "dr" resource block.

  2. B

    Add region = "us-west-2" directly inside the aws_subnet" "dr" resource block.

  3. C

    Change the aliased provider block to provider "aws.west" { region = "us-west-2" } and leave the resources unchanged.

  4. D

    Add providers = { aws = aws.west } inside the aws_subnet" "dr" resource block.

Show answer and explanation

Correct answer: A

Explanation

Terraform supports multiple configurations for the same provider by using provider aliases. In this scenario, the default aws provider is configured for us-east-1, and a second aliased provider aws.west is configured for us-west-2. Resources automatically use the default provider unless you override that behavior with the provider meta-argument, such as provider = aws.west.

This is a common real-world pattern when managing infrastructure across regions, accounts, or endpoints. The key distinction is:

  • Use provider = aws.alias_name in a resource to select a specific aliased provider.
  • Use providers = { ... } only in a module block to pass provider configurations into child modules.

This aligns with Terraform language documentation on provider configuration, aliases, and resource meta-arguments. Best practice is to keep provider selection explicit when a resource must be created outside the default provider context, which improves readability and reduces the risk of deploying to the wrong region.

  • A. Correct.

    Correct. When a configuration uses multiple instances of the same provider, Terraform selects the default provider instance unless a resource explicitly references an aliased provider. Adding provider = aws.west to the aws_subnet resource tells Terraform to use the aliased AWS provider configured for us-west-2, while aws_vpc.primary continues using the default us-east-1 provider.

  • B. Incorrect.

    Incorrect. Terraform resource blocks do not support arbitrary provider-specific configuration such as region unless it is a valid argument for that resource type, and aws_subnet does not accept a top-level Terraform meta-argument named region. Region selection is controlled by the provider configuration, not by setting region directly in most AWS resource blocks.

  • C. Incorrect.

    Incorrect. That is not valid Terraform syntax for provider configuration. Provider aliases are defined with provider "aws" { alias = "west" ... }, not by changing the provider type name to aws.west. Even if the syntax were corrected, the aws_subnet.dr resource would still need to reference the aliased provider explicitly unless it were passed through a module providers map.

  • D. Incorrect.

    Incorrect. The providers argument is used in module blocks to remap provider configurations for resources inside the child module. It is not a valid argument inside a resource block. A common misconception is that provider inheritance and remapping work the same way for resources and modules, but resources use the singular provider meta-argument.

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