Skip to main content
In this lesson you’ll learn how to use the provider meta-argument in Terraform to assign resources to specific provider configurations. This lets a single Terraform configuration target different accounts, regions, or roles (for example, separate AWS accounts or regions for dev and prod) without duplicating whole configurations. Why this matters
  • 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.
How it works
  • 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 provider meta-argument to tell Terraform which provider configuration to use for that resource.
Example: multi-region AWS providers and S3 buckets
Explanation of the example
  • The first provider "aws" block (no alias) is the default provider. Any AWS resource that does not specify the provider meta-argument uses this configuration. In the example, dev_bucket is created in us-east-1.
  • The second provider "aws" block sets alias = "prod" and uses us-west-1. To use that provider, set provider = aws.prod inside the resource block. Here, prod_bucket is created in us-west-1.
  • Each provider block may include its own credentials, profile, assume_role configuration, or provider-specific settings. This allows targeting different accounts, regions, or roles from the same Terraform code.
When to use this pattern Default provider behavior
  • A provider block without an alias is the default. Any resource without a provider meta-argument uses that configuration.
  • Use alias and the provider meta-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.
Using aliased providers with modules
  • When calling a module that needs to target a non-default provider, pass a providers map to the module:
  • Inside the module, resources referencing aws will 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.
Security and best practices
  • Avoid hard-coding credentials in provider blocks. Prefer profiles, environment variables, or assume_role where 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.
Quick demo summary
  • Define multiple provider blocks (one default, additional ones with alias).
  • Use provider = provider_name.alias inside resource blocks to select the specific provider configuration.
  • Pass providers into modules via the providers map to control provider usage in nested modules.
Links and references

Watch Video