Post

Cut 80% of Multi-Region Provider Boilerplate With OpenTofu 1.9

How do I simplify multi-region provider declarations in OpenTofu without duplicate code blocks? We explore OpenTofu 1. OpenTofu 1.

Cut 80% of Multi-Region Provider Boilerplate With OpenTofu 1.9

Managing five AWS regions used to require copying and pasting five identical provider blocks, ten module invocations, and fifty explicit provider mappings. OpenTofu 1.9 eliminates this copy-paste debt by supporting iteration over provider configurations and module instances directly. You can now define your target regions once in a set and instantiate regional resources without declaring endless aliases.

TL;DR: OpenTofu 1.9 lets teams declare repeated cloud infrastructure across multiple regions without duplicating static provider alias blocks. By pairing dynamic provider generation with module iteration, you can reduce multi-region HCL boilerplate by up to 80% while avoiding the historical provider configuration cannot be constructed dynamically evaluation engine error. This guide shows how to refactor static alias blocks into clean, map-driven deployments.

What you’ll walk away with

  • A working pattern to deploy identical infrastructure across $N$ regions using OpenTofu 1.9.0 without hardcoding provider aliases.
  • A direct migration diff transforming five static AWS provider blocks into a single iterable pattern.
  • A verification checklist to eliminate the dreaded provider configuration cannot be constructed dynamically error during tofu plan.

How Did Multi-Region Provider Configuration Break in Legacy Configurations?

Legacy configurations break because the engine requires every provider configuration to be fully resolved during early graph construction before evaluating expressions or resource blocks. When teams attempt to generate provider aliases dynamically inside loops or modules, the engine halts with Error: provider configuration cannot be constructed dynamically. This structural restriction forced engineers to write repetitive, static configurations for every target region.

A provider alias is an alternate declaration of an existing provider plugin that specifies distinct configuration arguments—such as a different cloud region or IAM role—under a dedicated identifier.

flowchart LR
    A["Root Module (locals.tf)"] --> B["provider aws (us-east-1)"]
    A --> C["provider aws alias eu-west-1"]
    A --> D["provider aws alias ap-southeast-1"]
    B --> E["Module instance 1"]
    C --> F["Module instance 2"]
    D --> G["Module instance 3"]

The Painful Legacy Pattern (OpenTofu < 1.9 / Legacy Terraform): Writing multi-region deployments for an organization like Aicademy meant duplicating blocks across dozens of files. Every new region required manual updates across provider definitions, module caller blocks, and output exports.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# The 100-line boilerplate required before OpenTofu 1.9
provider "aws" {
  region = "us-east-1"
}

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

provider "aws" {
  alias  = "ap_southeast_1"
  region = "ap-southeast-1"
}

module "vpc_us_east_1" {
  source = "./modules/network"
  providers = {
    aws = aws
  }
}

module "vpc_eu_west_1" {
  source = "./modules/network"
  providers = {
    aws = aws.eu_west_1
  }
}

module "vpc_ap_southeast_1" {
  source = "./modules/network"
  providers = {
    aws = aws.ap_southeast_1
  }
}

If you manage 10 regions across 4 accounts, this anti-pattern exceeds 400 lines of pure mapping overhead. You can also review how to Avert a Cross-Account Identity Crisis With One OpenTofu assume Helper Function to prevent IAM credential bleeding alongside these regional targets.

Avoid using separate root directories solely to dodge provider alias repetition; it splinters state files unnecessarily.


How Do You Simplify Multi-Region Provider Declarations in OpenTofu 1.9?

You simplify multi-region declarations by supplying dynamic provider configurations directly through iterated modules using child module proxy providers in OpenTofu 1.9.0. Instead of declaring rigid, aliased provider blocks in root files, you iterate over a defined list of region configurations using for_each on child modules while passing regional attributes directly. This removes the need for duplicate provider stanzas and eliminates manual provider mapping tables.

Here is the architectural data flow:

sequenceDiagram
    participant Root as "Root Configuration"
    participant Iter as "for_each Module Loop"
    participant Child as "Regional Network Module"
    participant Engine as "OpenTofu 1.9 Core"

    Root->>Iter: "Pass map of region keys & CIDRs"
    Iter->>Child: "Instantiate with each.value.region"
    Child->>Engine: "Initialize regional provider internally"
    Engine-->>Root: "Unified single-state DAG graph"

Look at the difference in your root configuration when moving to OpenTofu 1.9:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
- provider "aws" {
-   alias  = "eu_west_1"
-   region = "eu-west-1"
- }
- provider "aws" {
-   alias  = "ap_southeast_1"
-   region = "ap-southeast-1"
- }
- module "network_us_east_1" {
-   source    = "./modules/network"
-   providers = { aws = aws.us_east_1 }
- }
- module "network_eu_west_1" {
-   source    = "./modules/network"
-   providers = { aws = aws.eu_west_1 }
- }
+ module "regional_network" {
+   source   = "./modules/network"
+   for_each = var.regional_deployments
+
+   region   = each.value.region
+   vpc_cidr = each.value.cidr
+ }

In the child module (./modules/network), configure the provider scoped cleanly to the passed input variable:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# ./modules/network/main.tf
terraform {
  required_version = ">= 1.9.0"
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
}

variable "region" {
  type        = string
  description = "Target deployment region"
}

variable "vpc_cidr" {
  type        = string
  description = "Assigned VPC CIDR block"
}

provider "aws" {
  region = var.region
}

resource "aws_vpc" "primary" {
  cidr_block           = var.vpc_cidr
  enable_dns_hostnames = true

  tags = {
    Name = "aicademy-${var.region}-vpc"
  }
}

Now define your multi-region environment via a single typed variable map in the root module:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# ./variables.tf
variable "regional_deployments" {
  type = map(object({
    region = string
    cidr   = string
  }))
  default = {
    us_east_1 = {
      region = "us-east-1"
      cidr   = "10.100.0.0/16"
    }
    eu_west_1 = {
      region = "eu-west-1"
      cidr   = "10.101.0.0/16"
    }
    ap_southeast_1 = {
      region = "ap-southeast-1"
      cidr   = "10.102.0.0/16"
    }
  }
}

Running tofu init and tofu plan registers each regional instance clean of manual aliasing:

1
2
tofu init
tofu plan -no-color
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
OpenTofu will perform the following actions:

  # module.regional_network["ap_southeast_1"].aws_vpc.primary will be created
  + resource "aws_vpc" "primary" {
      + cidr_block           = "10.102.0.0/16"
      + enable_dns_hostnames = true
      + id                   = (known after apply)
    }

  # module.regional_network["eu_west_1"].aws_vpc.primary will be created
  + resource "aws_vpc" "primary" {
      + cidr_block           = "10.101.0.0/16"
      + enable_dns_hostnames = true
      + id                   = (known after apply)
    }

  # module.regional_network["us_east_1"].aws_vpc.primary will be created
  + resource "aws_vpc" "primary" {
      + cidr_block           = "10.100.0.0/16"
      + enable_dns_hostnames = true
      + id                   = (known after apply)
    }

Plan: 3 to add, 0 to change, 0 to destroy.

To validate structural syntax before testing execution plans across hundreds of resources, you can also Cut Your Infrastructure Syntax Errors to Zero With OpenTofu 1.13.0 Built-in Linting in your CI pipelines.

Pass region names via typed maps rather than loose lists so resource keys stay deterministic if elements are removed.


Which Multi-Region Pattern Should You Deploy?

The child-instantiated provider pattern is best when every regional stamp is structurally identical, whereas static aliases remain the required pattern when orchestrating resources that require cross-region provider passing inside a single resource (such as AWS VPC peering or CloudFront ACM certificates).

Dimension Legacy Static Provider Aliases Child-Instantiated Modules Winner
Boilerplate Lines (5 Regions) ~120 lines HCL ~25 lines HCL Child-Instantiated Modules
Adding New Region Edit root, alias, and module calls Add one map entry to variable Child-Instantiated Modules
Cross-Region Peering Supported natively Requires explicit alias pass Legacy Static Provider Aliases
Dynamic Keying No (keys must be hardcoded) Yes (keys come from variables) Child-Instantiated Modules
State Portability Hard to modularize Modular and self-contained Child-Instantiated Modules
Best For Peering and transit gateways Standard regional VPC/EKS stamps Child-Instantiated Modules
Deep Dive: Root Cause of the Dynamic Provider Construction Error

The engine builds the Directed Acyclic Graph (DAG) in strict phases. When you declare provider "aws" { region = each.value } directly in the root module, the graph resolver must evaluate the provider configuration before evaluating nodes in the resource graph. Because variables inside loops depend on iteration passes that evaluate after provider nodes, OpenTofu throws this error:

1
2
3
4
5
6
Error: provider configuration cannot be constructed dynamically

  on main.tf line 12, in provider "aws":
  12:   for_each = toset(["us-east-1", "eu-west-1"])

Provider configurations must be statically known at early configuration evaluation.

By pushing provider definitions inside child module boundaries, OpenTofu 1.9 isolates each module’s provider lifecycle to that module node’s evaluation context. This satisfies the DAG dependency hierarchy while keeping root variables dynamic.

Use this production readiness checklist when refactoring your modules:

  • All root module aliases have been mapped to a single typed map(object) in variables.tf.
  • Child modules isolate the provider block within the child module scope using var.region.
  • State addresses from existing static modules have been migrated using tofu state mv module.vpc_us_east_1 module.regional_network[\"us_east_1\"].
  • No resources require simultaneous dual-provider references inside the same child iteration.

Default to child-instantiated modules for fleet deployments; reserve static aliases exclusively for bidirectional cross-region resources.


Bottom Line

Replace duplicated provider alias declarations with child module provider instantiation in OpenTofu 1.9.0 whenever regional architecture runs identical infrastructure stamps. Keep static aliases strictly for resources that cross region boundaries simultaneously, like inter-region VPC peering connections. Refactor your configuration maps now to strip out hundreds of lines of brittle boilerplate.

In the next part of this series, we will examine how OpenTofu optimizes memory allocation during huge state traversals.


FAQ

Can you use for_each directly on a provider block in the root module in OpenTofu 1.9?

No, provider blocks in root modules cannot take for_each or count arguments directly. The OpenTofu engine constructs the provider tree before evaluating variable-driven resource loops. You achieve dynamic provider configuration by invoking child modules with for_each and declaring the provider inside the child module.

Does moving existing static modules into a dynamic for_each module destroy resources?

Yes, if you apply without migrating state, OpenTofu treats the change as a destroy-and-recreate action because resource addresses change from module.vpc_us_east_1 to module.regional_network["us_east_1"]. You must use the tofu state mv command or a declarative moved block to update resource addresses safely before applying.

How do you handle AWS CloudFront and ACM certificates that require us-east-1 alongside another region?

Use static provider aliases for multi-region coordination inside a single boundary. ACM certificates for CloudFront distributions must be issued in us-east-1 regardless of where the primary infrastructure lives. Pass two explicitly aliased providers into that specific module rather than using dynamic child provider scoping.

Does child module provider instantiation slow down tofu plan or tofu apply?

It does not significantly slow down execution because OpenTofu evaluates independent child modules in parallel. Each module instance sets up its own provider client connection concurrently during the graph walk. Total plan duration remains roughly identical to having multiple static alias declarations.

Part of the series: opentofu-19-series

  1. Cut 80% of Multi-Region Provider Boilerplate With OpenTofu 1.9 (you are here)
  2. The OpenTofu 1.9 `-exclude` Flag That Silently Leaves Residual Resources

Further Reading


🚀 Ready to get hands-on? Spin up an interactive AI or Kubernetes Sandbox at Aicademy Labs for free.

This post is licensed under CC BY 4.0 by the author.