Lesson 3 of 3 Terraform Providers

Configuring Multiple Providers

Real configurations often need several providers, or several configurations of one provider for different regions or accounts. This lesson covers both and how child modules receive provider configurations.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain how one configuration uses several providers.
  • Describe how provider aliases create additional configurations.
  • Explain how child modules receive default and aliased provider configurations.

Several provider types together

A configuration can use as many providers as it needs. Each is declared in required_providers and, if it needs settings, configured with a provider block. Resources from different providers reference each other like any other resources, and the references order the work across providers.

resource "random_pet" "suffix" {
  length = 2
}

resource "aws_s3_bucket" "logs" {
  bucket = "acme-logs-${random_pet.suffix.id}"
}

Here the random provider generates a name and the aws provider uses it, so Terraform creates random_pet.suffix first. Utility providers such as random, tls, time and local are commonly combined with cloud providers in this way.

True or false

Mixing providers

Exam skills 2.3.1

Check how resources from different providers work together.

Log in to try this activity Interactive activities are free with an account.

Provider aliases

Sometimes one provider is needed with different settings, such as two AWS regions. The provider block without an alias argument is the default configuration, and there can be only one per provider. Every other block for the same provider sets a unique alias, and resources that should use it set the provider meta-argument.

provider "aws" {
  region = "eu-west-1"
}

provider "aws" {
  alias  = "dr"
  region = "eu-central-1"
}

resource "aws_s3_bucket" "primary" {
  bucket = "acme-data-primary"
}

resource "aws_s3_bucket" "replica" {
  provider = aws.dr
  bucket   = "acme-data-replica"
}

aws_s3_bucket.primary uses the default configuration in eu-west-1, and aws_s3_bucket.replica uses the aws.dr configuration in eu-central-1. The value of provider is a reference, written without quotes, in the form <PROVIDER NAME>.<ALIAS>. Aliases are also used for several accounts, typically with an assume_role block in each configuration.

Some providers add their own shortcuts. Version 6 of the AWS provider, for example, accepts a region argument on most resources, so a single provider configuration can place resources in several regions. Aliases remain the general Terraform mechanism that works for every provider and every setting, such as accounts and endpoints, and they are what exam questions about multiple provider configurations test.

Scenario questions

Using an alias

Exam skills 2.3.2

Write and read provider aliases correctly.

Log in to try this activity Interactive activities are free with an account.

Providers and child modules

A child module automatically inherits the default configuration of each provider from its caller, but never an aliased one. To give a module a different configuration, the caller passes it with the providers meta-argument, mapping the module's provider names to the caller's configurations.

module "replica_bucket" {
  source = "./modules/bucket"
  providers = {
    aws = aws.dr
  }
  name = "acme-data-replica"
}

Inside the module, resources use the default aws provider as usual, which is now the caller's aws.dr. A module that itself needs two configurations of the same provider, such as one that sets up replication between two regions, declares them with configuration_aliases in its required_providers entry, and its callers must pass both through providers.

# Inside modules/replication/versions.tf
terraform {
  required_providers {
    aws = {
      source                = "hashicorp/aws"
      configuration_aliases = [aws.source, aws.destination]
    }
  }
}
Default configuration
Declared withprovider block without alias
Selected by resourcesAutomatically, by type prefix
Inherited by child modulesYes, implicitly
Aliased configuration
Declared withprovider block with alias = "name"
Selected by resourcesprovider = aws.name
Inherited by child modulesNo, pass it with providers = { ... }

The following diagram shows an aliased provider passed to a child module.

Passing an aliased provider to a module. The root has a default aws provider for us-east-1 and an alias aws.west for us-west-2. Child modules inherit only the default configuration, so both module calls created resources in us-east-1 until the second call passed providers = { aws = aws.west }. The plan then shows its resources being replaced in the new region.
The module source path is illustrative.
True or false

Providers in child modules

Exam skills 2.3.3

Check how provider configurations reach child modules.

Log in to try this activity Interactive activities are free with an account.
Worked example

Deploying to two regions

A team calls the same internal module twice, once for us-east-1 and once for us-west-2. Both module instances are created in us-east-1. The root module has a default aws provider for us-east-1 and an aliased one, aws.west, for us-west-2.

The decisive fact is that aliased configurations are not inherited.

Both module calls inherited the default configuration because neither passed a provider. Adding providers = { aws = aws.west } to the second module block makes its resources use us-west-2. Because the resources' region changes, the plan shows them being replaced in the new region, so the team should review it before applying.

Check yourself

Does a child module automatically use an aliased provider configuration from its parent?

Show answer

No. Child modules inherit only default configurations; aliased ones must be passed with the providers meta-argument.

Exam Tips

Remember for the exam

  • One configuration can use many providers; references order work across them.
  • alias creates extra configurations; select with provider = name.alias.
  • Child modules inherit default configurations only; pass aliases with providers.
  • configuration_aliases declares aliases a module expects.