Hi @nivimorbgp,
It looks like you’re intending to declare multiple instances of this ./iam
module, each with a different provider configuration.
To do this, it’s important to first recognize that each module has its own “view” of the provider configurations, and so they will each have their own set of supported configuration addresses – with or without aliases – and the providers
argument serves to translate from the calling module’s addresses to the addresses expected by the module you’re calling.
Because your ./iam
module only needs to work with one provider configuration, it makes sense to use that module’s default (unaliased) provider configuration to represent the one it should use. Your intermediate module has to declare three different aliases because it needs to work with multiple configurations at the same time, but that’s not necessary for the ./iam
module which only needs one.
With that said then, I would expect the top-level module block to be the same as what you shared:
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
}
}
}
provider "aws" {
alias = "dev"
# ...
}
provider "stg" {
alias = "stg"
# ...
}
provider "prod" {
alias = "prod"
# ...
}
module "my_module" {
source = "./modules/aws/my_module"
providers = {
aws.dev = aws.dev
aws.stg = aws.stg
aws.prod = aws.prod
}
}
Inside this ./modules/aws/my_module
you’d declare the requirement for these three additional (aliased) provider configurations, and then pass them down individually to the three ./iam
calls:
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
configuration_aliases = [
aws.dev,
aws.stg,
aws.prod,
]
}
}
}
module "set_iam_dev" {
source = "./iam"
providers = {
aws = aws.dev
}
environment = "dev"
}
module "set_iam_stg" {
source = "./iam"
providers = {
aws = aws.stg
}
environment = "stg"
}
module "set_iam_prod" {
source = "./iam"
providers = {
aws = aws.prod
}
environment = "prod"
}
The providers
argument in the module "set_iam_prod"
block (for example) says “the default aws
provider configuration in the child module is the one called aws.prod
in this module”, which I believe is the effect you intended.
Finally, inside the iam
module itself you don’t need to declare any required configuration_aliases
because it only uses the default (unaliased) provider, which from that module’s perspective is just called aws
and will be selected by default for any resource which doesn’t have a provider
argument to override that selection. That module need only declare that it needs that provider and will call it aws
:
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
}
}
}
# (and then AWS provider resource and data blocks,
# as usual.)