H there,
I’m facing a dependency issue, whilst using depends_on
with for_each
.
I have child module: subnet, which needs some information from aws_vpc_endpoint
resource and is being called from the root module that natively hosts the aws_vpc_endpoint
resource. So, I have this in my code:
// Subnets for TGW attachment only
module "tgw_subnets" {
for_each = toset(keys(var.vpc_list))
....
....
vpc_gw_eps = local.endpoints_gws
depends_on = [aws_vpc_endpoint.gw_eps]
}
where the use of vpc_gw_eps
is throwing in some error that generated in subnet module:
Error: Invalid for_each argument
on .terraform/modules/tgw_subnets/include/subnet/nacl.tf line 35, in resource "aws_network_acl_rule" "gw_eps":
35: for_each = {
36: for ep in local.gw_eps_nacl : "${ep.type}:${ep.rule}" => {
37: "cidr" = ep.cidr,
38: "rule" = ep.rule,
39: "type" = ep.type,
40: }
41: }
The "for_each" value depends on resource attributes that cannot be determined
until apply, so Terraform cannot predict how many instances will be created.
To work around this, use the -target argument to first apply only the
resources that the for_each depends on.
here is my local.endpoints_gws
:
endpoints_gws = {
for vpc in keys(var.vpc_list) : vpc => {
for ep in var.vpc_endpoints_gw : ep => {
"arn" = aws_vpc_endpoint.gw_eps["${vpc}:${ep}"].arn,
"cidr_blocks" = aws_vpc_endpoint.gw_eps["${vpc}:${ep}"].cidr_blocks,
"id" = aws_vpc_endpoint.gw_eps["${vpc}:${ep}"].id,
"prefix_list_id" = aws_vpc_endpoint.gw_eps["${vpc}:${ep}"].prefix_list_id,
"vpc_id" = aws_vpc_endpoint.gw_eps["${vpc}:${ep}"].vpc_id,
}
}
}
which is being passed on to subnet module, being further processed thereafter, as the local variables:
gw_ep_cidrs = flatten([
for ep in keys(var.vpc_gw_eps[var.vpc_name]) :
var.vpc_gw_eps[var.vpc_name][ep].cidr_blocks
])
gw_eps_nacl = flatten([
for type in ["egress", "ingress"] : [
for idx, cdr in sort(local.gw_ep_cidrs) : {
"cidr" = cdr,
"rule" = 200 + (2 * idx),
"type" = type,
}
]
])
and then gw_eps_nacl
is used in the for_each
, where the error actually being generated. I understand why it’s an issue for subnet module but why module.tgw_subnets
is not waiting until aws_vpc_endpoint.gw_eps
is completed? Isn’t that explicit depends_on
for that purpose?
Any idea what’s am I doing wrong? How can I make it working? I’m using
Terraform v0.13.6
.