0
0
Terraformcloud~10 mins

Multiple provider configurations in Terraform - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Multiple provider configurations
Define default provider
Define additional provider with alias
Use provider in resource with or without alias
Terraform applies resources using specified providers
Resources created in correct cloud accounts/regions
This flow shows how Terraform sets up multiple providers, assigns aliases, and uses them in resources to manage infrastructure across different accounts or regions.
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 configures two AWS providers for different regions and creates S3 buckets in each region using the appropriate provider.
Process Table
StepActionProvider ConfiguredResource CreatedRegion Used
1Load default provideraws (default)Noneus-east-1
2Load aliased provideraws.westNoneus-west-2
3Create resource east_bucketaws (default)aws_s3_bucket.east_bucketus-east-1
4Create resource west_bucketaws.westaws_s3_bucket.west_bucketus-west-2
5Apply completeBoth providers usedBoth buckets createdus-east-1 and us-west-2
💡 All resources created using their specified providers; execution ends.
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
provider.aws.regionundefinedus-east-1us-east-1us-east-1us-east-1us-east-1
provider.aws.west.regionundefinedundefinedus-west-2us-west-2us-west-2us-west-2
resource.aws_s3_bucket.east_bucketundefinedundefinedundefinedcreatedcreatedcreated
resource.aws_s3_bucket.west_bucketundefinedundefinedundefinedundefinedcreatedcreated
Key Moments - 3 Insights
Why do we need to use an alias for the second provider?
Because Terraform needs a way to distinguish between multiple configurations of the same provider. The alias lets us tell Terraform which provider to use for each resource, as shown in steps 2 and 4 of the execution_table.
What happens if we don't specify the provider for a resource?
Terraform uses the default provider configuration. In the example, the east_bucket resource uses the default provider (step 3), so it is created in us-east-1.
Can we have more than two provider configurations?
Yes, you can define as many provider configurations with different aliases as needed, each with its own settings. Just assign the correct alias to resources to control where they are created.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, which provider is used to create the west_bucket resource?
Aaws (default)
Baws.west
CNo provider specified
Daws.east
💡 Hint
Check step 4 in the execution_table where the west_bucket resource is created.
At which step does Terraform configure the provider for the us-west-2 region?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at the provider configuration steps in the execution_table.
If we remove the alias from the second provider, what will happen when creating west_bucket?
ATerraform will create west_bucket in us-east-1
BTerraform will create west_bucket in us-west-2
CTerraform will throw an error due to duplicate provider
DTerraform will ignore west_bucket resource
💡 Hint
Consider how Terraform distinguishes multiple provider configs; see key_moments about alias usage.
Concept Snapshot
Terraform Multiple Provider Configurations:
- Define default provider without alias.
- Define additional providers with unique aliases.
- Assign provider to resources using 'provider = provider.alias'.
- Enables managing resources across regions/accounts.
- Avoids conflicts by clear provider references.
Full Transcript
This lesson shows how Terraform handles multiple provider configurations. First, a default provider is defined for one region. Then, an additional provider with an alias is defined for another region. Resources specify which provider to use by referencing the alias or default. Terraform applies resources to the correct regions accordingly. Key points include the need for aliases to distinguish providers and how resources without a provider use the default. This allows managing infrastructure across multiple cloud regions or accounts in one Terraform project.