条件表达式中的 zipmap 函数给出 true 和 false 结果表达式必须具有一致的类型错误

zipmap function in condition expression giving true and false result expressions must have consistent types error

变量: ingress_ports_udp 是字符串类型,值以逗号分隔。 ingress_cidr_udp 是字符串类型,值以逗号分隔。

代码:

  dynamic "ingress" {
    iterator           = rule
    for_each           = zipmap(split(",", var.ingress_ports_udp),split(",", var.ingress_cidr_udp)) != "" ? zipmap(split(",", var.ingress_ports_udp),split(",", var.ingress_cidr_udp)) : ""
    content {
      from_port        = rule.key
      to_port          = rule.key
      protocol         = "udp"
      cidr_blocks      = rule.value
    }
  }

问题: 表达式 zipmap(split(",", var.ingress_ports_udp),split(",", var.ingress_cidr_udp)) != "" 出现错误? zipmap(split(",", var.ingress_ports_udp),split(",", var.ingress_cidr_udp)) : ""

"msg": "Failed to validate Terraform configuration files:\r\n\u001b[31m\n\u001b[1m\u001b[31mError: \u001b[0m\u001b[0m\u001b[1mInconsistent conditional result types\u001b[0m\n\n\u001b[0m  on security-group.tf line 9, in resource \"aws_security_group\" \"default-sg\":\n   9:     for_each           = zipmap(split(\",\", var.ingress_ports_udp),split(\",\", var.ingress_cidr_udp)) != \"\" ? \u001b[4mzipmap(split(\",\", var.ingress_ports_udp),split(\",\", var.ingress_cidr_udp)) : \"\"\u001b[0m\n\u001b[0m\nThe true and false result expressions must have consistent types. The given\nexpressions are map of string and string, respectively.\n\u001b[0m\u001b[0m\n\u001b[31m\n\u001b[1m\u001b[31mError: \u001b[0m\u001b[0m\u001b[1mInconsistent conditional result types\u001b[0m\n\n\u001b[0m  on security-group.tf line 20, in resource \"aws_security_group\" \"default-sg\":\n  20:     for_each           = zipmap(split(\",\", var.ingress_ports_udp),split(\",\", var.ingress_cidr_udp)) != \"\" ? \u001b[4mzipmap(split(\",\", var.ingress_ports_udp),split(\",\", var.ingress_cidr_udp)) : \"\"\u001b[0m\n\u001b[0m\nThe true and false result expressions must have consistent types. The given\nexpressions are map of string and string, respectively.\n\u001b[0m\u001b[0m\n"

要求: 如果 zipmap(split(",", var.ingress_ports_udp),split(",", var.ingress_cidr_udp)) 为空,我不想 运行 动态入口块,即如果 ingress_ports_udp 和 ingress_cidr_udp 字符串类型为空。 请帮忙

您不能为 false 使用空字符串,因为它的类型与 true 条件的结果不同。您可以查看:

for_each           = zipmap(split(",", var.ingress_ports_udp),split(",", var.ingress_cidr_udp)) != "" ? zipmap(split(",", var.ingress_ports_udp),split(",", var.ingress_cidr_udp)) : {}