Terraform VPC splits across accounts — simple HCL beats clever locals

Contents

  1. 1. Option 1: group in code
  2. 2. Option 2: group in config

I’ve been neck-deep in DevOps lately, and one thing keeps coming back: Infrastructure as Code reads better when it’s boring.

Let me show you what I mean—one migration, two ways to wire it.

Say you’ve got several networks in one account and you want each VPC living in its own account going forward.

1
2
3
4
5
6
7
8
9
10
11
12
# network.auto.tfvars
vpcs = {
network1 = {
name = "vpc-1"
}
network2 = {
name = "vpc-2"
}
network3 = {
name = "vpc-3"
}
}

You’ll reuse a module per account. Fair.

And no, you can’t dynamically pick a provider at runtime—Terraform doesn’t do that.

The real question is how you shard that vpcs map so each module gets the right slice.

Two patterns I’ve seen a lot of teams reach for:

Option 1: group in code

Tag every VPC with an acct_key, then let locals do the grouping.

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# network.auto.tfvars
vpcs = {
network1 = {
acct_key = "a"
name = "vpc-1"
}
network2 = {
acct_key = "a"
name = "vpc-2"
}
network3 = {
acct_key = "b"
name = "vpc-3"
}
}

## main.tf
locals {
# group by acct_key
grouped_networks_as_array = {
for k, v in var.vpcs :
v.acct_key => { "${k}" = v }...
}
grouped_networks_as_object = {
for k, v in local.grouped_networks_as_array :
k => merge({}, v...)
}
}

output "group_result" {
value = local.grouped_networks_as_object
}
provider "external" {
alias = "acct_a"
}
provider "external" {
alias = "acct_b"
}
module "acct_a_vpcs_1" {
source = "./vpc"
vpcs = local.grouped_networks_as_object.a
providers = {
external = external.acct_a
}
}
module "acct_b_vpcs_1" {
source = "./vpc"
vpcs = local.grouped_networks_as_object.b
providers = {
external = external.acct_b
}
}

You need two for expressions because Terraform’s native ... merge gives you arrays keyed the same way; what you want is one object per account so you can still do module.acct_a_vpcs.vpcs.network1 on outputs.

Plan output looks like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
+ group_result = {
+ a = {
+ network1 = {
+ acct_key = "a"
+ name = "vpc-1"
}
+ network2 = {
+ acct_key = "a"
+ name = "vpc-2"
}
}
+ b = {
+ network3 = {
+ acct_key = "b"
+ name = "vpc-3"
}
}
}

Classic engineer move: config barely changes, locals eat the complexity. Ship it.

…except there’s a second way.

Option 2: group in config

Structure the vars file the way the accounts actually look.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# network.auto.tfvars
accounts = {
acct_a = {
vpcs = {
network1 = {
name = "vpc-1"
}
network2 = {
name = "vpc-2"
}
}
}
acct_b = {
vpcs = {
network3 = {
name = "vpc-3"
}
}
}
}

Wiring is almost insultingly clear:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
module "acct_a_vpcs" {
source = "./vpc"
vpcs = var.accounts.acct_a.vpcs
providers = {
external = external.acct_a
}
}

module "acct_b_vpcs" {
source = "./vpc"
vpcs = var.accounts.acct_b.vpcs
providers = {
external = external.acct_b
}
}

Both get the job done. For day-to-day ops I’d take option two every time: less HCL to squint at, grouping lives where humans already think in accounts.

IaC should be obvious on a tired Friday, not a puzzle you cracked once in a PR review.

Clever locals have their place—like merging module outputs when something cross-account needs a flat map of VPC IDs or route table IDs:

1
2
3
4
5
6
locals {
combined_acct_vpcs = merge(
module.acct_a_vpcs.vpcs,
module.acct_b_vpcs.vpcs
)
}

That’s the kind of abstraction I’d keep. The rest? I’d rather duplicate a line in tfvars than maintain a grouping algorithm nobody remembers.

Questions? Leave a comment below or email: newbvirgil@gmail.com