带有嵌套地图列表的 Terraform 模块计数

Terraform Module Count With Nested List of Maps

我正在尝试遍历一个模块,次数等于地图在嵌套地图列表中出现的次数,如下所示:

vars.tf

  variable "http_tcp_listeners" {
  description = "A list of maps describing the HTTP listeners or TCP ports for this NLB"
  type        = any
  default = [
    {
      "http_tcp_listener" = [
        {
          port   = "80"
          protocol  = "TCP"
        },
        {
          port   = "7364"
          protocol  = "TCP"
        }
      ]
    },
    {
      "http_tcp_listener" = [
        {
          port   = "8080"
          protocol  = "TCP"
        },
        {
          port   = "7365"
          protocol  = "TCP"
        }
      ]
    }
  ]
}

main.tf

  module "create_network_lb" {
  count              = length(var."http_tcp_listeners")
  source             = "../../modules/lb"
  subnets            = tolist(data.aws_subnet_ids.private_compute[0].ids)
  vpc_id             = sort(data.aws_vpcs.platform_private_vpc.ids)[0]
  target_groups      = lookup(var.target_groups[count.index], "target_group", null)
  http_tcp_listeners = lookup(var.http_tcp_listeners[count.index], "http_tcp_listener", null)

模块

resource "aws_lb_listener" "frontend_http_tcp" {
  count = var.create_lb ? length(var.http_tcp_listeners) : 0

  load_balancer_arn = aws_lb.default[0].arn
  port     = var.http_tcp_listeners[count.index]["port"]
  protocol = var.http_tcp_listeners[count.index]["protocol"]

  dynamic "default_action" {
    for_each = length(keys(var.http_tcp_listeners[count.index])) == 0 ? [] : [var.http_tcp_listeners[count.index]]

   
    content {
      type             = lookup(default_action.value, "action_type", "forward")
      target_group_arn = contains([null, "", "forward"], lookup(default_action.value, "action_type", "")) ? aws_lb_target_group.main[lookup(default_action.value, "target_group_index", count.index)].id : null

      dynamic "redirect" {
        for_each = length(keys(lookup(default_action.value, "redirect", {}))) == 0 ? [] : [lookup(default_action.value, "redirect", {})]

        content {
          path        = lookup(redirect.value, "path", null)
          host        = lookup(redirect.value, "host", null)
          port        = lookup(redirect.value, "port", null)
          protocol    = lookup(redirect.value, "protocol", null)
          query       = lookup(redirect.value, "query", null)
          status_code = redirect.value["status_code"]
        }
      }

      dynamic "fixed_response" {
        for_each = length(keys(lookup(default_action.value, "fixed_response", {}))) == 0 ? [] : [lookup(default_action.value, "fixed_response", {})]

        content {
          content_type = fixed_response.value["content_type"]
          message_body = lookup(fixed_response.value, "message_body", null)
          status_code  = lookup(fixed_response.value, "status_code", null)
        }
      }
    }
  }
}

执行“地形规划”时,它仅显示最后一个“http_tcp_listener”值。模块的变量必须采用格式“[{port=80, protocol="TCP"},{port=7364, protocol="TCP"}]”,因此,每次迭代“http_tcp_listener”后的所有内容.

在故障排除期间,Terraform 似乎认为该变量是一个元组,每个错误都有一个元素:

Error: Invalid index

  on main.tf line 86, in module "create_network_lb":
  86:   http_tcp_listeners = [lookup(var.http_tcp_listeners[1], "http_tcp_listener")]
    |----------------
    | var.http_tcp_listeners is tuple with 1 element

The given key does not identify an element in this collection value.

如果我手动将其中一个键从“http_tcp_listener”更改为“http_tcp_listener1”,并将其反映在 main.tf 查找值中,它将显示该值。即,如果我重命名第一个键并引用它,terraform plan 将显示端口 80 和 7364 而不是 8080 和 7365。

如有任何帮助,我们将不胜感激。

通过重新创建数据结构并使用 for_each 调用模块来解决。详情 .