0
0
Terraformcloud~7 mins

Multiple provider configurations in Terraform - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you need to manage resources in different cloud accounts or regions. Multiple provider configurations let you connect to more than one place at the same time using Terraform.
When you want to create resources in two different AWS regions in the same Terraform project.
When you manage infrastructure for two separate AWS accounts from one Terraform setup.
When you need to test changes in a staging environment while keeping production separate.
When you want to organize resources by provider settings without mixing them.
When you need to use different credentials or settings for the same cloud provider.
Config File - main.tf
main.tf
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.0"
    }
  }
}

provider "aws" {
  region = "us-east-1"
  alias  = "use1"
}

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

resource "aws_s3_bucket" "bucket_use1" {
  provider = aws.use1
  bucket   = "example-bucket-use1"
  acl      = "private"
}

resource "aws_s3_bucket" "bucket_usw2" {
  provider = aws.usw2
  bucket   = "example-bucket-usw2"
  acl      = "private"
}

This Terraform file sets up two AWS providers with different regions using aliases. The provider blocks define connection details for each region. The resource blocks create two S3 buckets, each using a different provider alias to specify which region to create the bucket in.

Commands
This command initializes the Terraform project, downloads the AWS provider plugin, and prepares the working directory.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding hashicorp/aws versions matching "~> 4.0"... - Installing hashicorp/aws v4.60.0... - Installed hashicorp/aws v4.60.0 (signed by HashiCorp) Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure.
This command shows what Terraform will do. It checks the configuration and shows the resources it will create in each AWS region.
Terminal
terraform plan
Expected OutputExpected
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # aws_s3_bucket.bucket_use1 will be created + resource "aws_s3_bucket" "bucket_use1" { + acl = "private" + bucket = "example-bucket-use1" + id = (known after apply) } # aws_s3_bucket.bucket_usw2 will be created + resource "aws_s3_bucket" "bucket_usw2" { + acl = "private" + bucket = "example-bucket-usw2" + id = (known after apply) } Plan: 2 to add, 0 to change, 0 to destroy.
This command applies the changes to create the S3 buckets in both AWS regions without asking for confirmation.
Terminal
terraform apply -auto-approve
Expected OutputExpected
aws_s3_bucket.bucket_use1: Creating... aws_s3_bucket.bucket_usw2: Creating... aws_s3_bucket.bucket_use1: Creation complete after 2s [id=example-bucket-use1] aws_s3_bucket.bucket_usw2: Creation complete after 2s [id=example-bucket-usw2] Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
-auto-approve - Skip interactive approval before applying changes
This command lists all resources Terraform is managing, showing both buckets created in different regions.
Terminal
terraform state list
Expected OutputExpected
aws_s3_bucket.bucket_use1 aws_s3_bucket.bucket_usw2
Key Concept

If you remember nothing else from this pattern, remember: use provider aliases to tell Terraform which provider configuration to use for each resource.

Common Mistakes
Not using aliases for multiple provider blocks of the same type
Terraform cannot distinguish between providers without aliases and will give an error.
Always assign a unique alias to each provider block when using multiple configurations of the same provider.
Forgetting to specify the provider alias in resource blocks
Resources will default to the unnamed provider and may be created in the wrong region or account.
Specify the provider with the alias in each resource block using the syntax: provider = aws.alias_name.
Summary
Define multiple provider blocks with unique aliases to connect to different regions or accounts.
Use the provider alias in resource blocks to control where resources are created.
Run terraform init to prepare, terraform plan to preview, and terraform apply to create resources.