Current Status
While a longtime requested feature in Terraform, it is not possible to use count or for_each in the provider configuration block in Terraform.
Background
Much of the reasoning behind these limitations is shared in the following discussions:
- https://github.com/hashicorp/terraform/issues/19932
- https://github.com/hashicorp/terraform/issues/25244
- https://github.com/hashicorp/terraform/issues/24476
- https://discuss.hashicorp.com/t/is-anyone-aware-of-how-to-instantiate-dynamic-providers/34776
- https://discuss.hashicorp.com/t/dynamic-provider-support/7449
Workarounds
- Use external tooling (e.g. scripting with Bash or PowerShell) to generate
*.tffiles with all of the neededproviderconfiguration blocks. - Use the
tfeprovider to dynamically create Terraform Cloud/Enterprise Workspaces per distinct provider configuration. These Workspaces can then be configured with the needed environment variables to configure each distinct provider configuration. For example, if needing to create multiple AWS Provider configurations per region, the AWS Provider can make use of theAWS_DEFAULT_REGIONenvironment variable to set the working region. - Use the CDK for Terraform. This tool allows you to generate and run Terraform configurations using the language of your choice, granting the flexibility to use language features like "for" loops to define dynamic provider configurations.