根据之前使用 for_each 创建的儿童健康检查创建 CALCULATED R53 健康检查

Create CALCULATED R53 health check based on previously created child health checks with for_each

你好 Whosebug 社区。

我有 5 个 FQDN (myurl{1..5}.mydomain.com),我需要为每个 FQDN 创建 3 个 Route53 健康检查(总共 15 个)。每个 FQDN 后面有两个 IP,例如myurl1.mydomain.com 拥有 IP:123.123.123.123, 124.124.124.124。最终目标:

第一点是通过以下方式完成的:

data "dns_a_record_set" "mywiz" {
  for_each = toset(var.urls)
  host     = "${each.value}.mydomain.com"
}

resource "aws_route53_health_check" "hc-1" {
  for_each          = data.dns_a_record_set.sort(mywiz)
  fqdn              = each.value["host"]
  ip_address        = each.value["addrs"][0]
  port              = "443"
  type              = "HTTPS"
  failure_threshold = "3"
  request_interval  = "30"
  tags = {
    "Name" = "r53-hc-gfp-${each.key}-1"
  }

  lifecycle {
    ignore_changes = [tags]
  }
}

resource "aws_route53_health_check" "hc-2" {
  #count             = length(var.urls)
  for_each          = data.dns_a_record_set.mywiz
  fqdn              = each.value["host"]
  ip_address        = each.value["addrs"][1]
  port              = "443"
  type              = "HTTPS"
  failure_threshold = "3"
  request_interval  = "30"
  tags = {
    "Name" = "r53-hc-gfp-${each.key}-2"
  }

  lifecycle {
    ignore_changes = [tags]
  }
}

输出为:

# aws_route53_health_check.hc-1["myurl1"] will be created
  + resource "aws_route53_health_check" "hc-1" {
      + arn               = (known after apply)
      + disabled          = false
      + enable_sni        = (known after apply)
      + failure_threshold = 3
      + fqdn              = "myurl1.mydomain.com"
      + id                = (known after apply)
      + ip_address        = "123.123.123.123"
      + measure_latency   = false
      + port              = 443
      + request_interval  = 30
      + tags              = {
          + "Name" = "r53-hc-gfp-myurl1-1"
        }
      + tags_all          = {
          + "CreatedBy"    = "foobar"
          + "CreatedDate"  = "2022-03-10T07:48:05Z"
          + "LaunchSource" = "Terraform"
          + "Name"         = "r53-hc-gfp-myurl1-1"
          + "Notes"        = "Created for GFP"
        }
      + type              = "HTTPS"
    }

  # aws_route53_health_check.hc-2["myurl1"] will be created
  + resource "aws_route53_health_check" "hc-2" {
      + arn               = (known after apply)
      + disabled          = false
      + enable_sni        = (known after apply)
      + failure_threshold = 3
      + fqdn              = "myurl1.mydomain.com"
      + id                = (known after apply)
      + ip_address        = "124.124.124.124"
      + measure_latency   = false
      + port              = 443
      + request_interval  = 30
      + tags              = {
          + "Name" = "r53-hc-gfp-myurl1-2"
        }
      + tags_all          = {
          + "CreatedBy"    = "foobar"
          + "CreatedDate"  = "2022-03-10T07:48:05Z"
          + "LaunchSource" = "Terraform"
          + "Name"         = "r53-hc-gfp-myurl1-2"
          + "Notes"        = "Created for GFP"
        }
      + type              = "HTTPS"
    }

但是,我正在为计算的 Route53 运行状况检查而苦苦挣扎。如何构建 CALCULATED aws_route53_health_check 资源,如何将 correct(用于相应 FQDN 的)健康检查 ID 作为 child_healthchecks 传递。我试过:

resource "aws_route53_health_check" "hc-status" {
  for_each               = aws_route53_health_check.hc-1
  type                   = "CALCULATED"
  failure_threshold      = "1"
  child_healthchecks     = [aws_route53_health_check.hc-1.id[each.key]
  child_health_threshold = "1"
  tags = {
    "Name" = "r53-hc-gfpstatus-${each.key}"
  }

  lifecycle {
    ignore_changes = [tags]
  }
}

这导致:

|Error: Missing resource instance key
│ 
│   on main.tf line 58, in resource "aws_route53_health_check" "hc-status":
│   58:   child_healthchecks     = [aws_route53_health_check.hc-1.id[each.key]]
│ 
│ Because aws_route53_health_check.hc-1 has "for_each" set, its attributes must be accessed
│ on specific instances.
│ 
│ For example, to correlate with indices of a referring resource, use:
│     aws_route53_health_check.hc-1[each.key]

应该是:

 child_healthchecks     = [aws_route53_health_check.hc-1[each.key].id]

没有

child_healthchecks     = [aws_route53_health_check.hc-1.id[each.key]]