A required_providers block declares the providers a Terraform configuration needs, including each provider’s source address and acceptable version. It belongs inside the terraform settings block and acts like a dependency declaration. The provider block remains separate: required_providers controls installation, while provider configures details such as the AWS region. section 2.1.3 section 2.3
Listing 2.1 Configuring required providers
terraform { #1
required_providers { #2
aws = { #3
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
provider "aws" { #4
region = "us-east-1" #5
}
A typical AWS configuration pairs an explicit provider source and version constraint with a separate region configuration. section 2.1.3
Use explicit declarations to make provider selection predictable:
- Set source to identify the provider, such as hashicorp/aws. section 2.4.2
- Set version to constrain compatible releases, such as ~> 5.0 or ~> 4.0. section 2.1.3 section 2.4.2
- Declare providers even when Terraform can infer them from resource names; inference assumes the resource’s first name is a local provider name and defaults the namespace to hashicorp. section 2.4.2 section 2.1.3
- Keep provider settings, such as region, in a provider block rather than treating them as installation requirements. section 2.1.3
Configuring a reusable module’s test workspace
Place the required_providers block in the root-level workspace when a reusable module no longer contains provider blocks. Combine it with a region variable and provider block, then run terraform init followed by terraform apply after reviewing the plan. section 3.8.4
