HashiCorp Terraform Associate (004) Question 33
Single answer2b Describe how Terraform uses providersA team is building a Terraform configuration that creates AWS VPC resources and also manages DNS records in Cloudflare. A new engineer adds aws_vpc and cloudflare_record resources, but does not declare any provider requirements in the root module. When another engineer runs terraform init on a clean workstation, Terraform attempts to determine which plugins to install. To make provider installation predictable and ensure Terraform uses the intended source addresses for both providers, what should the team do?
- A
Add a required_providers block in the terraform block that declares the aws and cloudflare providers with their source addresses and version constraints.
- B
Add provider blocks only, because provider blocks automatically tell Terraform which provider plugin source to install and lock the version.
- C
Run terraform refresh first so Terraform can discover the correct providers from the existing infrastructure before initialization.
- D
Define all resources with fully qualified provider names inside each resource block, so Terraform does not need provider requirements.
Show answer and explanation
Correct answer: A
Explanation
Terraform uses providers as plugins that define resource types and data sources for external platforms such as AWS and Cloudflare. During terraform init, Terraform reads the configuration to determine which providers are required and then installs them from their source addresses. Best practice is to declare these requirements explicitly in the terraform { required_providers { ... } } block, including source and version constraints. Provider blocks serve a different purpose: they configure provider instances after Terraform has identified and installed the provider. This distinction is important on the Terraform Associate exam and in practice, because predictable provider installation improves reproducibility across developer workstations and CI pipelines. See the Terraform documentation for provider requirements, provider configuration, and terraform init behavior.
- A. Correct.
Correct. Terraform uses the required_providers block in the terraform block to identify provider local names, source addresses such as hashicorp/aws or cloudflare/cloudflare, and optional version constraints. This is the recommended way to make provider installation deterministic during terraform init, especially when using providers outside the default HashiCorp namespace assumptions or when a team needs consistent versions across environments.
- B. Incorrect.
Incorrect. A provider block configures a provider instance, such as region, credentials, or aliases, but it does not replace required_providers for declaring the provider source address and version constraints. Relying only on provider blocks can lead to ambiguity or unexpected provider selection behavior, particularly for non-HashiCorp providers.
- C. Incorrect.
Incorrect. terraform refresh does not discover and install providers before initialization in the way described. Provider plugins must already be available for Terraform to interact with infrastructure. Initialization is the step that installs required providers based on configuration metadata.
- D. Incorrect.
Incorrect. Resource blocks use a provider type prefix like aws_vpc or cloudflare_record, but that does not substitute for declaring provider requirements. Terraform still needs provider requirement metadata to know which plugin source to download and which versions are acceptable.