[英]Unable to loop over list using for_each
I want to reserve an IP and then use it.我想保留一个IP然后使用它。 If I create a separate
google_compute_address
block for each IP, it works well.如果我为每个 IP 创建一个单独的
google_compute_address
块,它运行良好。 But since I want to make the code as dry and optimized as possible, I am learning how to loop and use for_each
但由于我想让代码尽可能干燥和优化,我正在学习如何循环和使用
for_each
My main.tf looks like this我的main.tf看起来像这样
module "nat" {
source = "../../modules/nat"
reserved_ips = [
{
name = "gke-frontend-prod-lb"
ip = "10.238.232.10"
},
{
name = "gke-frontend-test-lb"
ip = "10.238.232.11"
}
]
}
As you can see, I would like to form a list of reserved IPs having name and IP.如您所见,我想形成一个包含名称和 IP 的保留 IP 列表。
Now lets look at my module现在让我们看看我的模块
My variables.tf looks like我的variables.tf看起来像
variable "reserved_ips" {
type = list(object({
name = string
ip = string
}))
description = <<EOF
Reserved IPs.
EOF
}
And the main.tf of my module looks like我的模块的main.tf看起来像
locals {
ips = {
# for_each needs transform to map
for ip in var.reserved_ips : "${ip.name}" => "${ip.ip}"
}
}
resource "google_compute_address" "gke-frontend" {
for_each = local.ips
name = "${each.value.name}"
subnetwork = "mysubnet"
address_type = "INTERNAL"
address = "${each.value.ip}"
}
But running the code gives me但是运行代码给了我
Error: Unsupported attribute
on ../../modules/nat/main.tf line 11, in resource "google_compute_address" "gke-frontend":
11: name = "${each.value.name}"
|----------------
| each.value is "10.238.232.10"
This value does not have any attributes.
Error: Unsupported attribute
on ../../modules/nat/main.tf line 11, in resource "google_compute_address" "gke-frontend":
11: name = "${each.value.name}"
|----------------
| each.value is "10.238.232.11"
This value does not have any attributes.
Error: Unsupported attribute
on ../../modules/nat/main.tf line 14, in resource "google_compute_address" "gke-frontend":
14: address = "${each.value.ip}"
|----------------
| each.value is "10.238.232.10"
This value does not have any attributes.
Error: Unsupported attribute
on ../../modules/nat/main.tf line 14, in resource "google_compute_address" "gke-frontend":
14: address = "${each.value.ip}"
|----------------
| each.value is "10.238.232.11"
This value does not have any attributes.
Im confused as to what am I missing here exactly.我很困惑我在这里到底错过了什么。
The issue is that your ips
local converts the list to a map(string)
(ie a map with string values)问题是您的
ips
local 将列表转换为map(string)
(即带有字符串值的地图)
locals {
ips = {
# for_each needs transform to map
for ip in var.reserved_ips : "${ip.name}" => "${ip.ip}"
}
}
Notice that on the right-side of =>
you have "${ip.ip}"
.请注意,在
=>
的右侧有"${ip.ip}"
。
When for_each
loops over a map
it assigns each.key
to each key (a string
) and each.value
to each corresponding value in the map (in this case "${ip.ip}
is also a string
).当
for_each
在环路map
它分配each.key
每个键(一个string
)和each.value
到每个相应值在地图(在这种情况下"${ip.ip}
也是一个string
)。
So, I think what you want in this case is something like the following所以,我认为在这种情况下你想要的是以下内容
# ...
name = each.key
# ...
address = each.value
# ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.