Provider configuration, aliases and multi-region

Intermediate · 10 min

Configure a provider once, add aliased configurations for a second region or account, choose one per resource, and pass them into modules without surprises.

Before thisProviders and the registrycount and for_each

WemaDevOps · 12 min · from 1:22

Provider configuration, aliases and multi-region

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by WemaDevOpsReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A `provider` block configures one connection to an API: region, endpoint, credentials. A block without `alias` is the default, used by every resource of that provider that does not say otherwise.
  • `alias = "west"` adds another configuration of the same provider. A resource chooses it with `provider = aws.west`.
  • Modules receive the root's default configuration implicitly. To pass an alias, the caller uses a `providers` map and the module declares it in `configuration_aliases`.
  • Define provider configurations in the root module, not inside reusable modules.
  • OpenTofu 1.9 added `for_each` on aliased provider blocks, so one block can cover a list of regions. Terraform has no such feature, to my knowledge as of this writing.

The certificate that lives somewhere else

A website runs in eu-west-1. It sits behind a CDN, and the CDN only accepts TLS certificates created in us-east-1. A disaster-recovery bucket must be in a second region. And an audit log goes to a bucket in a separate security account. A single provider block holds one region and one set of credentials, so one block cannot serve any of this.

Terraform’s answer is to allow several configurations of the same provider and let each resource choose. This is by far the most common use of the provider meta-argument.

Default and alias

Written from the Terraform provider-block docs. I did not run it, since there is no Terraform or AWS access here:

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

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

resource "aws_s3_bucket" "site" {
  bucket = "acme-site"
}

resource "aws_acm_certificate" "cdn" {
  provider    = aws.us_east_1
  domain_name = "example.com"
  validation_method = "DNS"
}

The first block has no alias. It is the default for aws. The second has an alias and is used only by resources that ask for it with provider = aws.us_east_1. The bucket has no provider argument, so it uses the default, and the certificate uses the alias.

The docs spell out the rule for resources that name nothing: a resource uses the default configuration of the provider matching its type-name prefix, so aws_instance goes to the default aws. I wrote that rule as a small resolver to confirm I had it right. It is a stand-in of mine, not Terraform:

aws_s3_bucket.primary    provider=(none)    -> uses config aws
aws_s3_bucket.replica    provider=aws.west  -> uses config aws.west
random_pet.name          provider=(none)    -> uses config random
aws_instance.web         provider=(none)    -> uses config aws

The trap documented in the same page: if every aws block has an alias, Terraform creates an implied empty default. A resource that names nothing then uses that empty configuration, and fails if the provider needs arguments. Keep one non-aliased block.

Passing providers into modules

A module inherits the root’s default provider configuration automatically. For an aliased one, you must pass it. The docs describe two sides:

  • the caller passes a providers map in the module block, such as providers = { aws.west = aws.west },
  • the module declares what it expects, in required_providers, with configuration_aliases = [aws.west]. Without that declaration, referencing the alias is an error.

My resolver’s last three lines state the rules in prose:

module "dr" { source = "./dr" }                       -> child inherits default aws only
module "dr" { providers = { aws = aws.west } }        -> child's default aws is us-west-2
module "dr" { providers = { aws.west = aws.west } }   -> works only if child declares configuration_aliases = [aws.west]

The middle form is a handy trick. The module can be written for one region, with no aliases, and the caller decides which region that is by remapping its default. That keeps modules simple.

The docs say child modules do not inherit source or version requirements either, so a module declares those itself, and the docs advise against defining provider blocks inside child modules. Treat a provider block in a shared module as a smell.

Multi-account, same trick

Aliases are not only for regions. The assume_role setting on the provider block points an alias at another account:

provider "aws" {
  alias  = "logging"
  region = "eu-west-1"
  assume_role {
    role_arn = "arn:aws:iam::222222222222:role/terraform"
  }
}

Same caveat, written from the AWS provider docs, not run. The account number is a placeholder. How credentials are obtained is the topic of authenticating providers without static keys.

A limit, and an OpenTofu difference

Provider configurations cannot take count or for_each in Terraform. So “one provider per region in a list” means writing a block per region, or a module call per region. OpenTofu 1.9 changed that. Its changelog lists for_each in provider configuration blocks, so one aliased block can cover a map of regions and resources pick an instance:

provider "aws" {
  alias    = "by_region"
  for_each = var.aws_regions
  region   = each.key
}

That snippet is quoted from OpenTofu’s v1.9 changelog. I did not run it. It is an OpenTofu feature. I found no equivalent in the Terraform changelogs I searched, but I did not search every release, so check before relying on that. A configuration using it will not run under Terraform, which matters if you promised portability in the pinning lesson.

Habits

  • Name aliases for what they are, us_east_1 or logging, not second.
  • Keep the number of aliases small. Each one is a separate set of credentials and state interactions.
  • Do not put provider configuration in modules you publish. Document which aliases the module expects.
  • Remember that destroying a resource needs its provider configuration to still exist. Deleting the alias block while resources still use it leaves an orphaned state entry. The sensible order is remove the resources first, apply, then remove the provider block.

About the video

The embed is the main tutorial of WemaDevOps’s “How to use a provider alias? - Terraform Provider Meta-Argument Tutorial Part 1”, December 2024, from 1:22 to the outro (11:49). It is from a small channel with a couple of hundred views, which was the best focused treatment I found. I considered a longer, popular video on providers and multi-region, but YouTube refuses to embed it, so it is not used. I judged this one from the title and description only, as it has no chapters beyond intro, tutorial and outro. English was judged from title, channel and English auto-captions.

Where this fits

Now that a configuration can talk to several APIs, the next risk is that the plugins change under you: version constraints and the lock file.

Check yourself

  1. 01A CloudFront certificate must live in us-east-1 but the rest of your stack is in eu-west-1. How do you express that in one configuration?

    Declare a second `aws` provider with `alias = "us_east_1"` and `region = "us-east-1"`, then put `provider = aws.us_east_1` on the certificate resource only.

  2. 02A resource has no `provider` argument and every `aws` block in the root has an alias. What happens?

    Terraform uses an implied empty default configuration. Since the AWS provider needs a region, the resource fails with an error. Always keep one non-aliased block.

  3. 03You call a module that needs two regions. How does it receive them?

    The module declares `configuration_aliases` in its `required_providers`, and the caller passes `providers = { aws = aws, aws.west = aws.west }` in the `module` block.

  4. 04Why is putting a `provider` block inside a reusable module discouraged?

    The module then carries its own region and credentials, so callers cannot choose them. It also makes it hard to remove the module cleanly. Providers belong in the root, and modules inherit or are passed them.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark