Why should the AWS provider be configured in the root module and not in a reusable child module?
BeginnerScenarioModules and Code Structure
Answer
Situation read → A junior added provider "aws" with a hardcoded region inside a module.
What I'd do → Remove it; let the module inherit the root's default provider, and declare only required_providers.
# module
terraform {
required_providers {
aws = { source = "hashicorp/aws", version = ">= 5.0" }
}
}
Why → Region and credentials are deployment decisions, not module decisions. Modules with embedded providers can't be removed cleanly and break reuse across regions.
Interview tip → In modules, set minimum provider versions with >=; the root pins the exact range.
More on Modules and Code Structure
When do you turn a block of resources into a module, and when do you leave it alone?BeginnerHow do you consume a community module like `terraform-aws-modules/vpc/aws` responsibly?BeginnerAn ECS service module needs the VPC and subnets created by a network module. How do you wire them together?IntermediateHow would you lay out a repo that deploys the same stack to dev, stage and prod?IntermediateHow do you release a shared module so consumers can upgrade at their own pace?IntermediateA module needs to deploy to a second region. Why does putting a provider block inside the module cause trouble?Advanced