Terraform Module Count With Nested List of Maps

I am attempting to loop over a module equal to the number of times a map appears inside a nested list of maps as follows:

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)

module

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

When performing a “terraform plan”, it displays only the last “http_tcp_listener” value. The variable for the module must be in format “[{port=80, protocol=“TCP”},{port=7364, protocol=“TCP”}]” hence, everything after each iteration of “http_tcp_listener”.

During troubleshooting, Terraform seems to think that the variable is a tuple with one element per the error:

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.

If I manually change one of the keys from “http_tcp_listener” to “http_tcp_listener1”, and reflect this in the main.tf lookup value, it will display that value. i.e, if I rename the first key and reference it, terraform plan will display ports 80 and 7364 instead of 8080 and 7365.

Any help would be greatly appreciated.