0

我试图循环一个模块,该模块等于地图出现在嵌套地图列表中的次数,如下所示:

变量.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"
        }
      ]
    }
  ]
}

主文件

  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)
        }
      }
    }
  }
}

执行“terraform plan”时,它只显示最后一个“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。

任何帮助将不胜感激。

4

1 回答 1

0

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

于 2021-04-15T16:35:40.953 回答