A provider alias names a non-default provider configuration so individual resources and data blocks can select it explicitly. This solves the practical problem of managing multiple AWS regions or multiple accounts for one vendor in a single Terraform program. section 2.4.4
Treat each provider block as a separate client connection: the alias identifies its settings, such as a different region. Terraform still keeps one configuration as the default, while aliased configurations are selected through the provider meta argument. section 2.4.4 section 2.7.1
Listing 2.22 Using multiple providers and aliases
provider "aws" { #1
region = "us-east-1"
}
provider "aws" {
alias = "west" #2
region = "us-west-2"
}
# Default Subnet Lookup
data "aws_vpc" "default" { #3
default = true
}
data "aws_subnets" "default" { #3
filter {
name = "vpc-id"
values = [data.aws_vpc.default.id]
}
}
# Secondary Subnet Lookup
data "aws_vpc" "backup" {
provider = aws.west #4
default = true
}
data "aws_subnets" "backup" {
provider = aws.west #4
filter {
name = "vpc-id"
values = [data.aws_vpc.defabackupult.id]
}
}
Compare a default AWS configuration with a west-region alias, then follow how VPC and subnet data blocks select the aliased connection. section 2.4.4
Use aliases deliberately when configurations differ:
- Define one provider block for each connection, assigning an alias to every non-default configuration. section 2.4.4
- Add provider = aws.alias to each resource or data block that must use the alternate configuration. section 2.4.4 section 2.7.1
- Pass outer-module aliases through a module's providers argument when a called module must use a specific connection. section 2.8 section 3.1.1
- Leave provider unset when the default configuration is sufficient; the field is useful specifically for multiple configurations of one provider type. section 2.7.1
Regional backup lookup
Keep the default AWS provider in us-east-1 and define an aliased provider for us-west-2. Point the backup VPC and subnet data blocks at aws.west so Terraform discovers infrastructure in the secondary region. The same pattern supports multiregion deployments intended to provide backup capacity. section 2.4.4
Provider aliases also have a JSON representation: repeated provider configurations appear as a list, with the default configuration and aliased configuration represented as separate objects. section 11.2.1
