[英]Terraform 'for each' loops over elements which are list of strings, not applying
I'm about to create a route table and associate them to subnets, so I've 3 subnets (list of strings)
they're already created, but when I'm trying to associate a route table to subnets, it's associating only with 1st one, and I guess same issues while I'm trying to associate a NAT
gateway which is also a list of strings
.我即将创建一个路由表并将它们关联到子网,所以我已经创建了 3 个子网
(list of strings)
,但是当我尝试将路由表关联到子网时,它只与第一个,当我尝试关联一个也是list of strings
NAT
网关时,我猜想同样的问题。
Here is outputs:这是输出:
+ nat_list = [
+ "nat-0980ffedd5471b76d",
+ "nat-053701e207f6e92b2",
+ "nat-0be06d45baf164edc",
]
+ subnets = [
+ "subnet-04c920f8908d7e502",
+ "subnet-0e9e9333180cab627",
+ "subnet-0caae55b544e4b63d",
]
Here is my main.tf
这是我的
main.tf
resource "aws_route_table" "public" {
count = length(local.azs)
vpc_id = aws_vpc.vpc[0].id
tags = var.tags
} <<< Creating without any problems
resource "aws_route" "public_ipv4" {
for_each = { for route in local.public_ipv4 : route.name => route}
route_table_id = aws_route_table.public.*.id
... ...
nat_gateway_id = ??? lookup(each.value, "nat", "" ) Not working ???
destination_cidr_block = lookup(each.value, "destination_cidr_block", "" )
... ...
}
resource "aws_route_table_association" "public" {
count = length(local.azs)
subnet_id = lookup(var.parameters[count.index], "public_subnet", [])
route_table_id = aws_route_table.public.*.id
}
variable "parameters" {
description = "The route table parameters"
type = list(object({
... ...
public_subnet = list(string)
... ...
}))
default = []
}
Update: Local var更新:本地变量
locals {
public_ipv4 = {[
{
name = "NAT(s) to MyLogSys ${local.counter + 1 }"
nat = aws_nat_gateway.translate.*.id
destination_cidr_block = "100.0.20.0/20"
}
]}
}
I made a research, however it seems there is no similar issue我做了一个研究,但似乎没有类似的问题
Based on a chat discussion.基于聊天讨论。
It was suggested to replace:建议更换:
for_each = { for route in local.public_ipv4 : route.name => route}
with和
for_each = { for idx, route in local.public_ipv4 : idx => route}
where idx will take values 0,1,2,...
based on the size of local.public_ipv4
.其中 idx 将根据
local.public_ipv4
的大小取值0,1,2,...
To protect from accessing elements in the lists which have fewer elements than local.public_ipv4
, element can be used.为了防止访问列表中元素少于
local.public_ipv4
的元素,可以使用 element。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.