HashiCorp Terraform Associate (004) exam dumps

HashiCorp Terraform Associate (004) practice question 36 of 223

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

HashiCorp Terraform Associate (004) Question 36

Single answer2c Write Terraform configuration using multiple providers

A platform team manages infrastructure in two AWS regions from a single Terraform root module. Most resources should be created in us-east-1, but an S3 bucket used for cross-region log aggregation must be created in us-west-2. The team wants to keep one AWS provider as the default for the primary region and use a second configuration only for the bucket. Which Terraform configuration approach best meets this requirement?

  1. A

    Define one default aws provider for us-east-1 and a second aws provider with alias = "west" for us-west-2. Then set provider = aws.west on the S3 bucket resource.

  2. B

    Define two aws provider blocks with different regions and no aliases. Terraform will automatically choose the correct provider based on the resource type.

  3. C

    Define one aws provider for us-east-1 and set region = "us-west-2" directly inside the aws_s3_bucket resource for the bucket that must be created in the secondary region.

  4. D

    Define a second provider block inside the aws_s3_bucket resource so that only that resource uses us-west-2 while the rest of the configuration uses the default provider.

Show answer and explanation

Correct answer: A

Explanation

The best solution is to use multiple provider configurations with an alias. In Terraform, provider settings such as AWS region belong in provider blocks, not in individual resources. When you need the same provider type configured in more than one way, such as AWS in two regions, you define one default provider and one or more aliased provider configurations. Then you attach the non-default configuration to specific resources using the provider meta-argument. This pattern is documented in Terraform's provider configuration guidance and is a common real-world approach for multi-region and multi-account deployments. Key best practices include keeping one default provider when most resources use the same configuration, using aliases for exceptions, and explicitly assigning the aliased provider to resources or modules that need it.

  • A. Correct.

    Correct. Terraform supports multiple configurations of the same provider by using provider aliases. A common pattern is to keep one unaliased provider as the default and add another provider block such as provider "aws" { alias = "west" region = "us-west-2" }. Resources that must use the secondary configuration explicitly reference it with provider = aws.west. This is the standard and supported way to use multiple AWS regions from one root module.

  • B. Incorrect.

    Incorrect. When multiple configurations of the same provider are needed, Terraform requires aliases to distinguish them unless there is only a single configuration. Terraform does not infer which provider configuration to use from the resource type. Without aliases, the configuration is invalid or ambiguous for this use case.

  • C. Incorrect.

    Incorrect. Resource blocks do not accept arbitrary provider configuration arguments such as region for AWS resources. Region is configured in the provider, not per resource in this way. This option reflects a common misconception that provider settings can be overridden directly inside any resource block.

  • D. Incorrect.

    Incorrect. Provider blocks cannot be nested inside resource blocks. Provider configurations are declared at the module level. A resource can select among existing provider configurations using the provider meta-argument, but it cannot define its own provider configuration inline.

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