- Manage multi-region or multi-account deployments from one codebase.
- Keep resource definitions centralized while controlling where they are created.
- Use distinct credentials, assume-role settings, or endpoints per provider configuration.
- Define one or more provider blocks for the same provider type. Any non-default provider block must include an
alias. - Inside a resource block, set the
providermeta-argument to tell Terraform which provider configuration to use for that resource.
- The first
provider "aws"block (noalias) is the default provider. Any AWS resource that does not specify theprovidermeta-argument uses this configuration. In the example,dev_bucketis created inus-east-1. - The second
provider "aws"block setsalias = "prod"and usesus-west-1. To use that provider, setprovider = aws.prodinside the resource block. Here,prod_bucketis created inus-west-1. - Each provider block may include its own credentials, profile,
assume_roleconfiguration, or provider-specific settings. This allows targeting different accounts, regions, or roles from the same Terraform code.
Default provider behavior
- A provider block without an
aliasis the default. Any resource without aprovidermeta-argument uses that configuration. - Use
aliasand theprovidermeta-argument only when a resource must be created with a provider different from the default.
Remember to configure credentials or role-assumption settings appropriately for each provider block. Each aliased provider can (and usually should) have its own authentication configuration (for example, different access keys, profiles, or
assume_role settings) so Terraform can authenticate to the correct account or region.- When calling a module that needs to target a non-default provider, pass a
providersmap to the module:
- Inside the module, resources referencing
awswill then use the aliased provider passed from the root module. This pattern keeps module code generic while allowing the root module to control provider targets.
- Avoid hard-coding credentials in provider blocks. Prefer profiles, environment variables, or
assume_rolewhere possible. - Keep secrets out of version control. Use your cloud provider’s recommended secret storage or a secrets manager.
- Validate that each provider alias has access only to the necessary accounts and permissions.
- Define multiple provider blocks (one default, additional ones with
alias). - Use
provider = provider_name.aliasinside resource blocks to select the specific provider configuration. - Pass providers into modules via the
providersmap to control provider usage in nested modules.
- Terraform Provider Documentation
- Terraform AWS Provider
- Using multiple provider configurations in Terraform