Is a variable provider alias possible in a module call?

Hello all,

I have a use case where I want to create a resource (module) in each of my AWS accounts and ideally, I don’t want to repeat the module definition all over again for each account (is identical, just the provider is different). This is not a use case for workspaces, because I don’t want to create the resources one at a time for each account.

Here is roughly what I’m after:

module “iam_role_content” {
source = “…/…/iam_role_with_policy”
for_each = [“west”, “central”]

providers = {
aws = “aws.${each.key}”
}
}

Any idea how to do this (or a different approach)?

Thanks a lot!

Hi @pavelpichrt

This unfortunately is not possible yet in Terraform. You will need to create individual instances of the modules and pass in the desired provider for now. You can see more details in the Terraform enhancement request here: Ability to pass providers to modules in for_each · Issue #24476 · hashicorp/terraform · GitHub