动态块中的索引无效

问题描述 投票:0回答:1

我有以下模块:

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 参数,所以我认为这里也可能是这种情况,但它不起作用。

amazon-web-services terraform terraform-provider-aws
1个回答
1
投票

values
alphanumerical 顺序返回值,而不是按照您定义的顺序。因此,返回的第一个值将是
28883
。随后,
var.healthcheck_map["28883"]
失败,因为
var.healthcheck_map
只有
8883
键,而不是所需的
28883

基本上,

var.listen_map
中的值与
var.healthcheck_map
中的键不匹配。

© www.soinside.com 2019 - 2024. All rights reserved.