【发布时间】:2021-10-20 03:51:36
【问题描述】:
我参考了以下链接并在输入 cidr 块中进行了一些修改
Nested For_each or count with dynamic for aws_security_group_rule in terraform
端口 22 的 CIDR 应为 ["10.0.0.1/24"] 端口 443 和 80 的 CIDR 应为 ["172.31.96.0/20","sg-09eadd831567d7dsb","172.31.160.0/20"]
ingress_ports_tcp = [[22], [443,80]]
ingress_cidr_tcp = [["172.31.32.0/20"],["172.31.96.0/20","sg-09eadd831567d7dsb","172.31.160.0/20"]]
所做的更改: 我也在 ingress_cidr_tcp 变量中添加了安全组 ID
locals {
my_rules = merge([
for idx_port, ports in var.ingress_ports_tcp:
{ for port in ports:
{ for idx_cidr, cidrs in var.ingress_cidr_tcp[idx_port]:
"${idx_port}-${port}-${idx_cidr}" => {
"port" = port
"cidrs" = length(regexall("[0-9].+\\..*",cidrs[idx_cidr])) > 0 ? cidrs[idx_cidr] : null
"security_group_id" = length(regexall("sg-.*",cidrs[idx_cidr])) > 0 ? cidrs[idx_cidr] : null
}
}
}
]...)
}
我想要下面的输出
{
"0-22-0" = {
"cidrs" = [
"172.31.32.0/20",
]
"port" = 22
}
"1-443-0" = {
"cidrs" = [
"172.31.96.0/20",
]
"port" = 443
}
"1-443-1" = {
"security_group_id" = [
"sg-09eadd831567d7dsb",
]
"port" = 443
}
"1-443-2" = {
"cidrs" = [
"172.31.160.0/20",
]
"port" = 443
}
"1-80-0" = {
"cidrs" = [
"172.31.96.0/20",
]
"port" = 80
}
"1-80-1" = {
"security_group_id" = [
"sg-09eadd831567d7dsb",
]
"port" = 80
}
"1-80-2" = {
"cidrs" = [
"172.31.160.0/20",
]
"port" = 80
}
错误:
Invalid 'for' expression\
Key expression is required when building an object.
【问题讨论】:
-
效果很好:)。但是你能解释一下为什么它在我的代码中不起作用,逻辑或语法出了什么问题。
-
在您的
for port in ports:之后,您需要一些带有密钥的东西,例如mykey => {your map}
标签: amazon-web-services terraform terraform-provider-aws