I am provisioning a bunch of resources via a module.
module "test" {
source = "../modules/my-module"
vpc_id = module.vpc.vpc_id
subnet_ids = module.subnets.ids
}
Now, in the calling module ( the root module ), I use the alias of the provider that the child module should use. I would use the module providers
meta-argument :
provider "my_provider" {
alias = default
region = "default"
...
}
provider "my_provider" {
alias = alias1
region = "region1"
...
}
module "test" {
source = "../modules/my-module"
providers = {
my_provider = my_provider.alias1
}
vpc_id = module.vpc.vpc_id
subnet_ids = module.subnets.ids
}
Now, I want to provision the same module, but based on a condition :
module "test" {
source = "../modules/my-module"
providers = {
my_provider = var.region == "region1" ? my_provider.alias1 : my_provider.default
}
vpc_id = module.vpc.vpc_id
subnet_ids = module.subnets.ids
}
And I get thrown The providers argument requires a provider type name, optionally followed by a period and then a configuration alias.
Then I tried treating the providers meta-argument as a normal argument ( I knew it would not work, but still tried anyway ) :
module "test" {
source = "../modules/my-module"
providers = var.region == "region1" ? { my_provider = my_provider.alias1 } : { my_provider = my_provider.default }
vpc_id = module.vpc.vpc_id
subnet_ids = module.subnets.ids
}
This time, the error was more logical - A static map expression is required.
It does seem like the providers
meta-argument for modules are always supposed to be static ( essentially hard-coded ) just like the backend
argument.
Would there be a native way of re-using the same module declaration to switch between 2 different providers?
Or is the only way to achieve this would be code duplication and the count
meta-argument based on the condition?