liveBook · Manning

Provider alias

As explained in Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu

A provider alias is a name assigned to a non-default Terraform provider configuration so resources and data blocks can select it explicitly.

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

How Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu covers it

How deep it goes
Explains aliases through provider blocks, meta-argument selection, module passing, and JSON representation, stopping short of deeper deployment behavior.
Examples
Demonstrates AWS east-west configurations, backup VPC and subnet lookups, and repeated provider configurations in JSON.
What it assumes
Assumes familiarity with Terraform provider, resource, data, and module blocks while establishing the default-versus-alias model.
What you can do after
Operate Terraform configurations that direct selected resources, data blocks, and modules through alternate provider connections.
How it connects
Connects provider aliases to provider configurations, resources, data blocks, modules, and meta arguments.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu. The section links point to the parts of the book it draws on.