Terraform - 循环遍历对象列表的动态块
Terraform - Dynamic block to loop through list of objects
我正在尝试配置此 Terraform,以便添加新的 SG 入口规则所需要做的就是向 .tfvars
文件添加一个块,然后将添加新的入口规则到单个SG。
我有一个 .tfvars
文件,如下所示:
rules = [
{
protocol = "tcp"
port = 22
cidr = ["10.0.0.0/8"],
},
{
protocol = "tcp"
port = 80
cidr = ["10.0.0.0/8"]
},
{
protocol = "tcp"
port = 443
cidr = ["10.0.0.0/8"]
},
{
protocol = "icmp"
port = -1
cidr = ["10.0.0.0/8"]
}
]
我的 Terraform 是:
variable "rules" {
type = list(object({
protocol = string
port = number
cidr = list(string)
}))
description = "Specify the protocol, port range, and CIDRs."
}
resource "aws_security_group" "this" {
...
dynamic "ingress" {
for_each = { for rule in var.rules : rule.id => rule }
content {
from_port = var.rules.port
to_port = var.rules.port
protocol = var.rules.protocol
cidr_blocks = var.rules.cidr
}
}
...
我尝试遍历上面的内容,也尝试使用 for_each = var.rules
,但我只收到以下错误(出现以下每个错误):
protocol = var.rules.protocol
from_port = var.rules.port
to_port = var.rules.port
cidr_blocks = var.rules.cidr
-----------------------------
var.rules is list of object with 4 elements.
This value does not have any attributes.`
我有点卡住了,不知道还能尝试什么,所以有人有什么想法吗?
这里有几个问题。第一个是在您的 for_each
元参数值 for
表达式 lambda 中,您正试图访问 id
的不存在的对象键。此外,您可以在这里轻松地使用 list(object)
而不是像您在转换中尝试做的那样 map(object)
,然后可以直接使用您的输入变量。我们可以丢弃那些并相应地修复值:
for_each = var.rules
第二个问题是动态块中的临时 lambda 迭代器变量是块本身的名称。在这种情况下,您的块被命名为 ingress
。因此,我们需要从它的对象键访问值:
content {
from_port = ingress.value.port
to_port = ingress.value.port
protocol = ingress.value.protocol
cidr_blocks = ingress.value.cidr
}
结合这些修复,我们得出:
dynamic "ingress" {
for_each = var.rules
content {
from_port = ingress.value.port
to_port = ingress.value.port
protocol = ingress.value.protocol
cidr_blocks = ingress.value.cidr
}
}
我正在尝试配置此 Terraform,以便添加新的 SG 入口规则所需要做的就是向 .tfvars
文件添加一个块,然后将添加新的入口规则到单个SG。
我有一个 .tfvars
文件,如下所示:
rules = [
{
protocol = "tcp"
port = 22
cidr = ["10.0.0.0/8"],
},
{
protocol = "tcp"
port = 80
cidr = ["10.0.0.0/8"]
},
{
protocol = "tcp"
port = 443
cidr = ["10.0.0.0/8"]
},
{
protocol = "icmp"
port = -1
cidr = ["10.0.0.0/8"]
}
]
我的 Terraform 是:
variable "rules" {
type = list(object({
protocol = string
port = number
cidr = list(string)
}))
description = "Specify the protocol, port range, and CIDRs."
}
resource "aws_security_group" "this" {
...
dynamic "ingress" {
for_each = { for rule in var.rules : rule.id => rule }
content {
from_port = var.rules.port
to_port = var.rules.port
protocol = var.rules.protocol
cidr_blocks = var.rules.cidr
}
}
...
我尝试遍历上面的内容,也尝试使用 for_each = var.rules
,但我只收到以下错误(出现以下每个错误):
protocol = var.rules.protocol
from_port = var.rules.port
to_port = var.rules.port
cidr_blocks = var.rules.cidr
-----------------------------
var.rules is list of object with 4 elements.
This value does not have any attributes.`
我有点卡住了,不知道还能尝试什么,所以有人有什么想法吗?
这里有几个问题。第一个是在您的 for_each
元参数值 for
表达式 lambda 中,您正试图访问 id
的不存在的对象键。此外,您可以在这里轻松地使用 list(object)
而不是像您在转换中尝试做的那样 map(object)
,然后可以直接使用您的输入变量。我们可以丢弃那些并相应地修复值:
for_each = var.rules
第二个问题是动态块中的临时 lambda 迭代器变量是块本身的名称。在这种情况下,您的块被命名为 ingress
。因此,我们需要从它的对象键访问值:
content {
from_port = ingress.value.port
to_port = ingress.value.port
protocol = ingress.value.protocol
cidr_blocks = ingress.value.cidr
}
结合这些修复,我们得出:
dynamic "ingress" {
for_each = var.rules
content {
from_port = ingress.value.port
to_port = ingress.value.port
protocol = ingress.value.protocol
cidr_blocks = ingress.value.cidr
}
}