Module 2: Terraform Fundamentals

Lesson 2.4: Configuring Multiple Providers

Plain language

A provider block configures a provider with settings such as region, credentials, or endpoint. A configuration can use any number of providers, and any number of configurations of the same provider.

  • A provider "aws" block with no alias is the default configuration.
  • Each extra configuration needs a unique alias.
  • A resource uses the default configuration unless you set the provider meta-argument.
HCL
# Optional AWS variant — two regions in one configuration
provider "aws" {
  region = "us-east-1" # default configuration
}

provider "aws" {
  alias  = "west"
  region = "us-west-2" # extra configuration: aws.west
}

resource "aws_s3_bucket" "logs_east" {
  bucket = "example-logs-east-123456" # no provider argument → default (us-east-1)
}

resource "aws_s3_bucket" "logs_west" {
  provider = aws.west # <local name>.<alias>, not a string
  bucket   = "example-logs-west-123456"
}

Under the hood: providers and modules

  • Default configurations are inherited automatically by child modules.
  • Aliased configurations are never inherited. You pass them explicitly with the module's providers map, and the child module declares that it expects them using configuration_aliases.
HCL
# Root module
module "replica" {
  source    = "./modules/bucket"
  providers = {
    aws = aws.west # inside the child, its default "aws" becomes aws.west
  }
}
HCL
# Child module that needs TWO named AWS configurations
terraform {
  required_providers {
    aws = {
      source                = "hashicorp/aws"
      version               = ">= 5.0"
      configuration_aliases = [aws.primary, aws.replica]
    }
  }
}

Two more points:

  • State records the configuration used. Each resource in state records its provider configuration, for example provider["registry.terraform.io/hashicorp/aws"].west. If you delete a provider block while resources still use it, Terraform can't plan to destroy them. Destroy or move those resources first.
  • Avoid provider blocks in reusable child modules. Configure providers in the root and pass them down.