Provider aliases and mappings
One provider, several configurations
A provider configuration describes the context in which a provider operates. A single provider package can be configured for two regions, for example. An alias gives an additional configuration a name; it does not install a second version of the provider. The examples below use a deliberately nonexistent provider and fictional region names. They illustrate ordinary Terraform module syntax, not a deployable configuration. Do not initialize these examples. The provisional CLI baseline remains 1.14.8, not independently tested.
Select a configuration for a resource
Read the root configuration as a routing table for resource operations. The fictional provider accepts region and the fictional record accepts name.
terraform { required_providers { demo = { source = "example.invalid/learning/demo" } }}
provider "demo" { region = "region-a"}
provider "demo" { alias = "secondary" region = "region-b"}
resource "demo_record" "primary" { name = "catalog"}
resource "demo_record" "replica" { provider = demo.secondary name = "catalog-copy"}The expected mapping is demo_record.primary to region-a and demo_record.replica to region-b. These are different resource addresses even though the same provider package handles both. The unaliased provider supplies the default. If all explicit configurations have aliases, an unqualified resource uses an implied empty default; depending on the provider, that may fail or use ambient settings. Never rely on failure as an account safety control.
Pass configurations into child modules
The child declares the source independently and names an additional configuration it expects from callers. It does not configure credentials or regions itself.
terraform { required_providers { demo = { source = "example.invalid/learning/demo" configuration_aliases = [demo.destination] } }}
resource "demo_record" "source" { name = "source"}
resource "demo_record" "destination" { provider = demo.destination name = "destination"}The caller, using the root configurations above, supplies both slots explicitly.
module "pair" { source = "./modules/pair" providers = { demo = demo demo.destination = demo.secondary }}Read the mapping from left to right: child configuration name on the left, caller configuration reference on the right. The child source uses the primary region and the child destination uses region-b. Aliases do not flow automatically into descendants; forward them explicitly at each relevant module boundary. For simple modules the default configuration can be inherited, but provider requirements are still declared by each module.
Map an alias to an ordinary default
A child that only uses its unaliased demo configuration does not need to declare a second slot simply because its caller uses an alias.
module "secondary_catalog" { source = "./modules/single-record" providers = { demo = demo.secondary }}This separate illustration maps the child default to the root secondary configuration. The single-record module would declare the same provider source and use an ordinary resource without a provider meta-argument. It is not included as an executable dependency.
Avoid dynamic provider selection
Provider configuration selection is resolved as part of graph construction in the ordinary module language. Do not treat aliases as strings, map entries or conditional results.
# INVALID in ordinary .tf modules: provider references are not values.resource "demo_record" "invalid" { for_each = toset(["primary", "secondary"]) provider = each.key == "primary" ? demo : demo.secondary name = each.key}Use separate resource or module blocks for each provider configuration, optionally with a filtered collection for the instances assigned to that block. Keep stable keys when grouping instances. The provider arguments themselves may accept appropriate known values; that is different from computing the provider reference dynamically. Newer or separate configuration languages are outside this example.
Practice
In the supplied offline fixture, trace the four root and child resource addresses to their region. Then swap the two references on the right side of the module mapping and predict the two child destinations. This is a reasoning exercise, not a recommendation to swap live provider settings.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Provider aliases and mappings — knowledge check
Review provider selection, account boundaries and safe lifecycle.
What does an alias identify?
Where does demo_record.replica target in the example?
In a providers map, what is the left side?
What happens if the caller maps demo = demo.secondary?
Can ordinary resource provider selection use a conditional over each.key?