簡體   English   中英

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字母數字順序返回值,而不是按照您定義它們的順序。 因此,返回的第一個值將是28883 隨后, var.healthcheck_map["28883"]失敗,因為var.healthcheck_map只有8883鍵,而不是所需的28883

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM