HashiCorp Terraform Associate (004) Question 37
Single answer2c Write Terraform configuration using multiple providersYour 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?
- A
Add
provider = aws.westinside theaws_subnet" "dr"resource block. - B
Add
region = "us-west-2"directly inside theaws_subnet" "dr"resource block. - C
Change the aliased provider block to
provider "aws.west" { region = "us-west-2" }and leave the resources unchanged. - D
Add
providers = { aws = aws.west }inside theaws_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_namein a resource to select a specific aliased provider. - Use
providers = { ... }only in amoduleblock 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.westto theaws_subnetresource tells Terraform to use the aliased AWS provider configured for us-west-2, whileaws_vpc.primarycontinues using the default us-east-1 provider. - B. Incorrect.
Incorrect. Terraform resource blocks do not support arbitrary provider-specific configuration such as
regionunless it is a valid argument for that resource type, andaws_subnetdoes not accept a top-level Terraform meta-argument namedregion. Region selection is controlled by the provider configuration, not by settingregiondirectly 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 toaws.west. Even if the syntax were corrected, theaws_subnet.drresource would still need to reference the aliased provider explicitly unless it were passed through a moduleprovidersmap. - D. Incorrect.
Incorrect. The
providersargument is used inmoduleblocks 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 singularprovidermeta-argument.