gpt4 book ai didi

dictionary - 如何在 terraform 中拆分 map 的值以创建列表?

转载 作者:行者123 更新时间:2023-12-01 07:02:03 25 4
gpt4 key购买 nike

我有一个包含许多值的映射变量(NACL 规则)。我正在尝试相应地添加规则

variable "rules" {
default = {
a = "200,false,tcp,allow,0.0.0.0/0,23,23"
b = "100,true,tcp,allow,0.0.0.0/0,1024,65535"
}
}


resource "aws_network_acl_rule" "bar" {
network_acl_id = "<id>"
rule_number = "${split(",",element(values(var.rules),count.index))[0]}"
egress = "${split(",",element(values(var.rules),count.index))[1]}"
protocol = "${split(",",element(values(var.rules),count.index))[2]}"
rule_action = "${split(",",element(values(var.rules),count.index))[3]}"
cidr_block = "${split(",",element(values(var.rules),count.index))[4]}"
from_port = "${split(",",element(values(var.rules),count.index))[5]}"
to_port = "${split(",",element(values(var.rules),count.index))[6]}"
count = "${length(values(var.rules))}"
}

错误: expected "}" but found "["
由于不支持将列表作为值的映射,我试图拆分这些值并进行迭代

最佳答案

另一个更容易阅读的选项是 lookup() :

variable "rules" {
default = [
{
rule_number = 200
egress = false
protocol = "tcp"
rule_action = "allow"
cidr_block = "0.0.0.0/0"
from_port= 23
to_port = 23
},
{
rule_number = 100
egress = true
procotol = "tcp"
rule_action = "allow"
cidr_block = "0.0.0.0/0"
from_port = 1024
to_port = 65535
},
]
}

resource "aws_network_acl_rule" "bar" {
count = "${length(var.rules)}"
network_acl_id = "<id>"
rule_number = "${lookup(var.rules[count.index], "rule_number")}"
egress = "${lookup(var.rules[count.index], "egress")}"
protocol = "${lookup(var.rules[count.index], "protocol")}"
rule_action = "${lookup(var.rules[count.index], "rule_action")}"
cidr_block = "${lookup(var.rules[count.index], "cidr_block")}"
from_port = "${lookup(var.rules[count.index], "from_port")}"
to_port = "${lookup(var.rules[count.index], "to_port")}"
}

关于dictionary - 如何在 terraform 中拆分 map 的值以创建列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45362960/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com