I get an error during plan/apply that appears to be due to unresolved details of an object to build. Below is one of 3 errors (the others confirm AZ-letter is “b” and “c”).
│ Error: Invalid function argument
│
│ on vpc.tf line 63, in resource “aws_nat_gateway” “ngw”:
│ 63: allocation_id = tostring([for eip in aws_eip.nat_ip : eip.id if eip.tags.AZ-letter == each.value.tags.AZ-letter])
│ ├────────────────
│ │ while calling tostring(v)
│ │ aws_eip.nat_ip is object with 3 attributes
│ │ each.value.tags.AZ-letter is “a”
│
│ Invalid value for “v” parameter: cannot convert tuple to string.
Excerpts of my code suggest the if == comparison should be 2 strings. It appears the ‘aws_eip.nat_ip is object with 3 attributes’ isn’t being evaluated to recognise aws_eip.nat_ip.tags.AZ-letter will be a string value. Please provide guidance (or confirmation this is a bug and suggestions maybe how to work around it)
variable azs {
type = list(string)
default = [“ap-southeast-2a”,“ap-southeast-2b”,“ap-southeast-2c”]
}
locals {
subnets = flatten([for env_name, env_setup in var.envs :
[for subnet_type, octet_offset in var.subnet_defs :
[for az in var.azs :
[for subnet_details in tolist([{
subnet_name = “{var.prefix}-{env_name}-subnet-{subnet_type}-{substr(az,length(az)-1,1)}”,
subnet_ip = “{var.upper_network}.{env_setup.start+(octet_offset+(4*(index(var.azs,az)+1)))}.0”,
az = az,
env = env_name,
area = subnet_type,
az_letter = substr(az,length(az)-1,1)
}]) : subnet_details if (env_setup.active_azs[index(var.azs,az)] && contains(env_setup.reqd_defs, subnet_type))
]
]
]
])
}
resource “aws_subnet” “infra_subnets” {
for_each = {for subnet in local.subnets : subnet.subnet_name => subnet if subnet.env == “mgmt”}
vpc_id = aws_vpc.eos-vpc.id
cidr_block = “{each.value.subnet_ip}/{var.mask}”
availability_zone = each.value.az
tags = {
Name = each.value.subnet_name
Environment = each.value.env
AZ-letter = each.value.az_letter
}
}
resource “aws_eip” “nat_ip” {
for_each = aws_subnet.infra_subnets
domain = “vpc”
tags = {
Name = “{var.prefix}-mgmt-nat_ip-{each.value.tags.AZ-letter}”
AZ-letter = each.value.tags.AZ-letter
}
depends_on = [aws_internet_gateway.igw]
}
resource “aws_nat_gateway” “ngw” {
for_each = aws_subnet.infra_subnets
allocation_id = tostring([for eip in aws_eip.nat_ip : eip.id if eip.tags.AZ-letter == each.value.tags.AZ-letter])
subnet_id = each.value.id
tags = {
Name = “{var.prefix}-mgmt-ngw-{each.value.tags.AZ-letter}”
AZ-letter = each.value.tags.AZ-letter
}
depends_on = [aws_internet_gateway.igw, aws_eip.nat_ip]
}