Terraform - 动态块中的无效索引

Terraform - invalid index in dynamic block

我有以下模块:

module "nlb_mqtt_public" {
  source             = "..." # module source
  ... # other fields

  vpc_id             = module.vpc_base.vpc_id

  listen_map = {
    "8884"  = "8883"
    "50858" = "8883"
    "60858" = "60858" 
    "60859" = "60859" 
    # "28883" = "28883"
    "443" = "28883"
  }

  healthcheck_map = {
    "8883" = {
      interval = "10"
      path     = "/heartbeat"
      port     = "9090"
      protocol = "HTTP"
    }
  }
}

而在我在源代码中引用的外部模块中,这部分失败了:

resource "aws_lb_target_group" "this-health" {
  count             = length(var.healthcheck_map) >= 1 ? length(distinct(values(var.listen_map))) : 0
  name              = "${random_string.target.keepers.name}-port-${element(values(var.listen_map), count.index)}-${random_string.target.result}"
  protocol          = "TCP"
  port              = element(distinct(values(var.listen_map)), count.index)
  vpc_id            = var.vpc_id
  proxy_protocol_v2 = random_string.target.keepers.enable_proxy_protocol_v2

  dynamic "health_check" {
    for_each = [var.healthcheck_map[element(distinct(values(var.listen_map)), count.index)]] # it fails here
    content {
      enabled             = lookup(health_check.value, "enabled", null)
      healthy_threshold   = lookup(health_check.value, "healthy_threshold", null)
      interval            = lookup(health_check.value, "interval", null)
      matcher             = lookup(health_check.value, "matcher", null)
      path                = lookup(health_check.value, "path", null)
      port                = lookup(health_check.value, "port", null)
      protocol            = lookup(health_check.value, "protocol", null)
      timeout             = lookup(health_check.value, "timeout", null)
      unhealthy_threshold = lookup(health_check.value, "unhealthy_threshold", null)
    }
  }

  lifecycle {
    create_before_destroy = true
  }
}

并且在这个

data "template_file" "healthcheck_port" {
  count    = length(var.healthcheck_map) >= 1 ? length(distinct(values(var.listen_map))) : 0
  template = var.healthcheck_map[element(distinct(values(var.listen_map)), count.index)]["port"]
}

显示以下错误:

Error: Invalid index
  on .terraform/modules/nlb_mqtt_public/main.tf line 51, in resource "aws_lb_target_group" "this-health":
  51:     for_each = [var.healthcheck_map[element(distinct(values(var.listen_map)), count.index)]]
    |----------------
    | count.index is 0
    | var.healthcheck_map is map of map of string with 1 element
    | var.listen_map is map of string with 5 elements
The given key does not identify an element in this collection value.
Error: Invalid index
  on .terraform/modules/nlb_mqtt_public/main.tf line 51, in resource "aws_lb_target_group" "this-health":
  51:     for_each = [var.healthcheck_map[element(distinct(values(var.listen_map)), count.index)]]
    |----------------
    | count.index is 2
    | var.healthcheck_map is map of map of string with 1 element
    | var.listen_map is map of string with 5 elements
The given key does not identify an element in this collection value.
Error: Invalid index
  on .terraform/modules/nlb_mqtt_public/main.tf line 51, in resource "aws_lb_target_group" "this-health":
  51:     for_each = [var.healthcheck_map[element(distinct(values(var.listen_map)), count.index)]]
    |----------------
    | count.index is 3
    | var.healthcheck_map is map of map of string with 1 element
    | var.listen_map is map of string with 5 elements
The given key does not identify an element in this collection value.
Error: Invalid index
  on .terraform/modules/nlb_mqtt_public/main.tf line 150, in data "template_file" "healthcheck_port":
 150:   template = var.healthcheck_map[element(distinct(values(var.listen_map)), count.index)]["port"]
    |----------------
    | count.index is 3
    | var.healthcheck_map is map of map of string with 1 element
    | var.listen_map is map of string with 5 elements
The given key does not identify an element in this collection value.
Error: Invalid index
  on .terraform/modules/nlb_mqtt_public/main.tf line 150, in data "template_file" "healthcheck_port":
 150:   template = var.healthcheck_map[element(distinct(values(var.listen_map)), count.index)]["port"]
    |----------------
    | count.index is 0
    | var.healthcheck_map is map of map of string with 1 element
    | var.listen_map is map of string with 5 elements
The given key does not identify an element in this collection value.
Error: Invalid index
  on .terraform/modules/nlb_mqtt_public/main.tf line 150, in data "template_file" "healthcheck_port":
 150:   template = var.healthcheck_map[element(distinct(values(var.listen_map)), count.index)]["port"]
    |----------------
    | count.index is 2
    | var.healthcheck_map is map of map of string with 1 element
    | var.listen_map is map of string with 5 elements
The given key does not identify an element in this collection value.

我不明白为什么。 healthcheck_map 变量定义为 map(map(string)) 类型,默认为 {}。

之前,我在另一个tf文件中遇到了一些问题,错误提示我使用-target参数,所以我认为这里也可能是这种情况,但它不起作用。

values returns 值以 字母数字 顺序排列,而不是按照您定义它们的顺序排列。因此,返回的第一个值将是 28883。随后,var.healthcheck_map["28883"] 失败,因为 var.healthcheck_map 只有 8883 密钥,而不是所需的 28883.

基本上,您在 var.listen_map 中的值与 var.healthcheck_map 中的键不匹配。