Home > Back-end >  Create CALCULATED R53 health check based on previously created child health checks with for_each
Create CALCULATED R53 health check based on previously created child health checks with for_each

Time:03-10

Hello stackoverflow community.

I have 5 FQDNs (myurl{1..5}.mydomain.com) for which I need to create 3 Route53 health checks per FQDN (so 15 in total). Two IPs are behind each FQDN, e.g. myurl1.mydomain.com have IPs: 123.123.123.123, 124.124.124.124. End goal:

  • 2 health checks with each IP for the specific FQDN
  • 1 CALCULATED health check which is monitoring the above two

First point is accomplished by:

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

Output is:

# 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"
    }

However I'm struggling with the CALCULATED Route53 health check. How to structure the CALCULATED aws_route53_health_check resource, how to pass the correct (the ones which are for the respective FQDN) health check ids as child_healthchecks. I've tried with:

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

and this resulted in:

|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]

CodePudding user response:

It should be:

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

not

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