What You’ll Learn
In this lesson, you will learn how to configure multiple AWS provider instances with Terraform provider aliases. You will use those aliases to create resources in different AWS regions from one Terraform configuration.
- Define multiple AWS provider configurations with different regions.
- Assign each resource to the correct provider alias.
- Understand why explicit provider assignment matters in multi-region projects.
- Recognize common alias and credential-management mistakes.
The Concept
A Terraform provider is responsible for communicating with an external platform such as AWS. A normal AWS configuration might define one provider for one region:
When a project needs resources in multiple regions, you can define multiple configurations of the same provider. Each additional configuration receives an alias. A resource then selects its region by assigning the appropriate provider configuration:
The alias does not change the AWS provider itself. Instead, it gives Terraform a name for a particular provider configuration, such as aws.primary or aws.secondary.
This is useful for disaster recovery environments, globally distributed applications, regional networking, and services with regional requirements. For example, one Terraform configuration can create a VPC in us-east-1 and another VPC in us-west-2.
Provider aliases select a region, credentials, role, or endpoint configuration. They do not make a resource global. Each resource is still created in the region associated with the provider assigned to it.
Basic Example
The following configuration creates two VPCs in different AWS regions. Both VPC resources use the same AWS provider, but each resource explicitly selects a different aliased provider configuration.
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
variable "primary_region" {
description = "AWS region for the primary VPC"
type = string
default = "us-east-1"
}
variable "secondary_region" {
description = "AWS region for the secondary VPC"
type = string
default = "us-west-2"
}
provider "aws" {
alias = "primary"
region = var.primary_region
}
provider "aws" {
alias = "secondary"
region = var.secondary_region
}
resource "aws_vpc" "primary" {
provider = aws.primary
cidr_block = "10.10.0.0/16"
enable_dns_support = true
enable_dns_hostnames = true
tags = {
Name = "primary-vpc"
Region = var.primary_region
}
}
resource "aws_vpc" "secondary" {
provider = aws.secondary
cidr_block = "10.20.0.0/16"
enable_dns_support = true
enable_dns_hostnames = true
tags = {
Name = "secondary-vpc"
Region = var.secondary_region
}
}
output "primary_vpc_id" {
value = aws_vpc.primary.id
}
output "secondary_vpc_id" {
value = aws_vpc.secondary.id
}Expected Output
After applying the configuration, Terraform creates one VPC in us-east-1 and another in us-west-2. The output values are AWS-generated VPC IDs, so their exact values are not deterministic.
You can initialize and preview the configuration with:
terraform init
terraform planHow the Code Works
The required_providers block identifies the AWS provider and constrains it to a compatible major version. Terraform downloads this provider during terraform init.
Each provider "aws" block configures one provider instance:
alias = "primary"gives the first configuration the nameprimary.region = var.primary_regiontells that configuration to useus-east-1by default.- The
secondaryprovider uses a separate region and alias.
The provider assignment on each resource is the most important part:
provider = aws.primarysends the resource operation to the primary provider configuration.provider = aws.secondarysends the resource operation to the secondary configuration.
Because both provider configurations are aliased, there is no ordinary default AWS provider configuration in this example. That is intentional: every AWS resource must explicitly state its region. This makes accidental placement in the wrong region less likely.
AWS credentials are not included in the configuration. Terraform can use the AWS provider’s normal credential sources, such as environment variables, shared AWS CLI configuration, or an attached IAM role. For safer patterns around credentials and Terraform state, see how to protect sensitive values in Terraform.
Another Example
Aliases are also useful when a regional application needs a copy of a service in each deployment region. This example creates separate DynamoDB tables for an application in two regions.
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
variable "application_name" {
description = "Name used by the regional application tables"
type = string
default = "orders"
}
provider "aws" {
alias = "east"
region = "us-east-1"
}
provider "aws" {
alias = "west"
region = "us-west-2"
}
resource "aws_dynamodb_table" "east" {
provider = aws.east
name = "${var.application_name}-us-east-1"
billing_mode = "PAY_PER_REQUEST"
hash_key = "order_id"
attribute {
name = "order_id"
type = "S"
}
tags = {
Application = var.application_name
Region = "us-east-1"
}
}
resource "aws_dynamodb_table" "west" {
provider = aws.west
name = "${var.application_name}-us-west-2"
billing_mode = "PAY_PER_REQUEST"
hash_key = "order_id"
attribute {
name = "order_id"
type = "S"
}
tags = {
Application = var.application_name
Region = "us-west-2"
}
}
output "regional_table_names" {
value = {
east = aws_dynamodb_table.east.name
west = aws_dynamodb_table.west.name
}
}This pattern could support a regional application that writes orders to the nearest table. Terraform creates two independent DynamoDB tables; it does not automatically replicate their data. Data replication, application routing, and conflict handling require additional AWS services or application logic.
When working with regional infrastructure, always verify resources using the intended region. The same principle applies when you inspect EKS clusters across regions: a command aimed at the wrong region can make a resource appear to be missing.
Common Mistakes
Forgetting the provider assignment
If a resource omits provider = aws.secondary, Terraform may try to use a default provider configuration. In a configuration where every provider block has an alias, that default configuration is not fully configured for AWS and Terraform will report an error or be unable to determine the region.
Make provider selection explicit for every resource that must be regional.
Assuming an alias changes a resource’s name
An alias only selects a provider configuration. It does not automatically add the region to a resource name. Regional services often need unique names, so include a region suffix yourself when the AWS service requires unique names.
Expecting Terraform to replicate data
Creating the same type of resource in two regions does not synchronize the resources. For example, two DynamoDB tables are separate tables. Plan replication, failover, and consistency behavior independently.
Trying to select providers dynamically with arbitrary expressions
Provider selection has restrictions. In particular, you generally cannot create a map of provider aliases and select one dynamically with an expression such as provider = aws.regions[each.key]. Terraform requires provider references to be statically identifiable in resource configuration.
For a small, fixed set of regions, explicit resources are usually clear and maintainable. For larger designs, consider modules with provider configurations passed into them, while keeping each module’s provider requirements explicit.
Try It Yourself
Extend the DynamoDB example so that it also creates a regional table in eu-central-1.
- Add an aliased AWS provider named
central. - Create a third DynamoDB table using
provider = aws.central. - Give the table a unique name containing
eu-central-1. - Add the new table name to the
regional_table_namesoutput.
Challenge
Create a multi-region network configuration for three AWS regions:
us-east-1as the primary region.us-west-2as the secondary region.eu-central-1as an audit region.
Your configuration must:
- Define one aliased AWS provider for each region.
- Create one VPC in each region.
- Use a different CIDR block for every VPC.
- Set DNS support and DNS hostnames to
true. - Add a
Purposetag that identifies each VPC. - Output all three VPC IDs in a map.
Solution
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
provider "aws" {
alias = "primary"
region = "us-east-1"
}
provider "aws" {
alias = "secondary"
region = "us-west-2"
}
provider "aws" {
alias = "audit"
region = "eu-central-1"
}
resource "aws_vpc" "primary" {
provider = aws.primary
cidr_block = "10.10.0.0/16"
enable_dns_support = true
enable_dns_hostnames = true
tags = {
Name = "primary-vpc"
Purpose = "Primary application network"
}
}
resource "aws_vpc" "secondary" {
provider = aws.secondary
cidr_block = "10.20.0.0/16"
enable_dns_support = true
enable_dns_hostnames = true
tags = {
Name = "secondary-vpc"
Purpose = "Disaster recovery network"
}
}
resource "aws_vpc" "audit" {
provider = aws.audit
cidr_block = "10.30.0.0/16"
enable_dns_support = true
enable_dns_hostnames = true
tags = {
Name = "audit-vpc"
Purpose = "Audit and compliance network"
}
}
output "vpc_ids_by_region" {
value = {
primary = aws_vpc.primary.id
secondary = aws_vpc.secondary.id
audit = aws_vpc.audit.id
}
}The solution uses three separate provider instances and assigns each VPC to exactly one of them. The output map provides a convenient way to identify the resulting VPC in each region without relying on resource names alone.
Key Takeaways
- Provider aliases let one Terraform configuration use multiple AWS provider configurations.
- Assign a resource with syntax such as
provider = aws.secondaryto control its region. - Explicit provider assignments help prevent resources from being created in the wrong region.
- Separate regional resources do not automatically share or replicate their data.
- For a fixed set of regions, explicit provider aliases are clear; dynamic provider selection has important Terraform limitations.



