0
0
Terraformcloud~10 mins

Provider aliases for multi-region in Terraform - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Provider aliases for multi-region
Define default provider
Define provider alias for region A
Define provider alias for region B
Use alias in resource for region A
Use alias in resource for region B
Terraform applies resources in both regions
This flow shows how Terraform defines multiple providers with aliases for different regions and uses them to deploy resources in each region.
Execution Sample
Terraform
provider "aws" {
  region = "us-east-1"
}

provider "aws" {
  alias  = "west"
  region = "us-west-2"
}

resource "aws_s3_bucket" "east_bucket" {
  bucket = "my-east-bucket"
}

resource "aws_s3_bucket" "west_bucket" {
  provider = aws.west
  bucket   = "my-west-bucket"
}
This Terraform code sets up AWS providers for two regions and creates S3 buckets in each region using provider aliases.
Process Table
StepActionProvider UsedResource CreatedRegion
1Initialize default AWS provideraws (default)Noneus-east-1
2Initialize AWS provider with alias 'west'aws.westNoneus-west-2
3Create S3 bucket 'my-east-bucket'aws (default)aws_s3_bucket.east_bucketus-east-1
4Create S3 bucket 'my-west-bucket'aws.westaws_s3_bucket.west_bucketus-west-2
5Apply complete, resources created in both regionsaws (default) & aws.west2 bucketsus-east-1 & us-west-2
💡 All resources created successfully using correct provider aliases for each region.
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
aws providerundefinedconfigured (us-east-1)configured (us-east-1)configured (us-east-1)configured (us-east-1)configured (us-east-1)
aws.west providerundefinedundefinedconfigured (us-west-2)configured (us-west-2)configured (us-west-2)configured (us-west-2)
aws_s3_bucket.east_bucketundefinedundefinedundefinedcreated (my-east-bucket)created (my-east-bucket)created (my-east-bucket)
aws_s3_bucket.west_bucketundefinedundefinedundefinedundefinedcreated (my-west-bucket)created (my-west-bucket)
Key Moments - 2 Insights
Why do we need to use 'provider = aws.west' in the west_bucket resource?
Because the default provider is set to us-east-1, specifying 'provider = aws.west' tells Terraform to use the provider configured for us-west-2, as shown in execution_table step 4.
What happens if we omit the alias and try to create resources in multiple regions?
Terraform will use the default provider for all resources, so all resources will be created in the default region only, not in multiple regions. This is why aliases are necessary (see execution_table steps 3 and 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, which provider is used to create the 'my-west-bucket'?
Aaws.west
Baws (default)
Caws.east
DNo provider specified
💡 Hint
Check step 4 in the execution_table where the provider used is listed.
At which step does Terraform configure the provider for the us-west-2 region?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the provider initialization steps in the execution_table.
If we remove the alias from the second provider block, what will happen to the 'west_bucket' resource creation?
AIt will create the bucket in us-west-2 anyway
BTerraform will throw an error due to duplicate provider blocks
CThe bucket will be created in the default region us-east-1
DThe bucket will not be created at all
💡 Hint
Refer to the key_moments about default provider usage and alias necessity.
Concept Snapshot
Terraform Provider Aliases for Multi-Region

- Define default provider for one region.
- Define additional providers with 'alias' for other regions.
- Use 'provider = aws.alias' in resources to specify region.
- Allows managing resources across multiple regions in one config.
- Avoids conflicts by clearly separating provider configs.
Full Transcript
This visual execution trace shows how Terraform uses provider aliases to manage resources in multiple AWS regions. First, the default AWS provider is configured for the us-east-1 region. Then, a second AWS provider is configured with the alias 'west' for the us-west-2 region. Resources are created using these providers: the east_bucket uses the default provider, and the west_bucket uses the aliased provider. The execution table details each step, showing provider initialization and resource creation with their respective regions. Variable tracking shows the state of providers and resources after each step. Key moments clarify why aliases are necessary and how Terraform decides which provider to use. The quiz tests understanding of provider usage and alias effects. The snapshot summarizes the key points for quick reference.