Error: Invalid provider configuration reference

is there any workaround available to use variable in provider something like below. If yes please let me know how to do it. Currently terraform is not allowing to use variable in provider name for module. I want to create role in each member account user provides; that is my goal here. User will provide the alias which I will use to create role.

providers = {
    aws = "${lookup(var.names, element(var.accounts, count.index))}"
  }

Hi @nimblenitin,

Terraform currently treats the association between provider configuration and resource as a static relationship determined during graph construction, and so there isn’t any way to decide it dynamically based on expressions.

Because the AWS provider is designed in such a way that each separate account requires its own provider configuration, that does mean that today it isn’t possible to dynamically specify a set of accounts and systematically declare the same infrastructure across all of them.

Instead, the typical answer today is to use a code generator to generate a root module that contains a separate provider block and module block for each of your accounts, each calling into the same module but with different input variables and provider configurations. That then creates the same effect as you described but does so statically in the generated configuration, rather than dynamically based on expression evaluation.

Thanks for the prompt reply. However, I kinda lost you on the typical answer. Can you please give a short example if possible.

Hi @nimblenitin,

The “code generator” I was describing here would be an external program outside of Terraform which generates .tf or .tf.json files based on your set of account ids.

Since that would be a separate program written outside of Terraform, I can’t really give a working example since it would depend on either what language you’d write that custom generator software in or what external templating tool you might use to generate it.

I’m sorry I wasn’t clearer that I was describing using separate software to generate Terraform configuration, rather than a feature of Terraform itself.

1 Like