Google_dns_record_set Invalid for_each argument

Hi!

This have been working (Applied) for a long while but when I tried to replace our locals.backend.api to a completly new Cloud Run Service everything fails when planning this. According to show state everything is similar, but I guess not?

  1. Is it a problem when trying to replace our DNS records?
  2. How should I continue to debug this?
 Error: Invalid for_each argument
│ 
│   on ../../modules/services/networking.tf line 60, in resource "google_dns_record_set" "service_mappings_record_sets":
│   60:   for_each = {
│   61:     for dns_record in local.dns_records : "${dns_record.name}.${dns_record.type}" => dns_record if length(dns_record.rrdata) > 0
│   62:   }
│     ├────────────────
│     │ local.dns_records is tuple with 12 elements
│ 
│ The "for_each" value depends on resource attributes that cannot be determined until apply, so Terraform cannot predict how many instances will be created. To work around this, use the -target
│ argument to first apply only the resources that the for_each depends on.
locals {
  backends = {
    api = google_cloud_run_service.newapi.name
    xservice = google_cloud_run_service.xservice.name
  }
}

resource "google_cloud_run_domain_mapping" "service_domain_mappings" {
  for_each = local.backends

  location = var.project_primary_region
  name     = "${each.key}.${var.primary_domain_name}"

  metadata {
    namespace = var.project_id
  }

  spec {
    route_name = each.value
  }
}

locals {
  dns_records = flatten([
    for backend_name, backend in google_cloud_run_domain_mapping.service_domain_mappings : [
      {
        name    = backend_name
        type    = "A"

        rrdata = [
          for rr in backend.status[0].resource_records :
          rr.rrdata if rr.type == "A"
        ]
      },
      {
        name    = backend_name
        type    = "AAAA"

        rrdata = [
          for rr in backend.status[0].resource_records :
          rr.rrdata if rr.type == "AAAA"
        ]
      },
      {
        name    = backend_name
        type    = "CNAME"

        rrdata = [
          for rr in backend.status[0].resource_records :
          rr.rrdata if rr.type == "CNAME"
        ]
      },
    ]
  ])
}

resource "google_dns_record_set" "service_mappings_record_sets" {
  for_each = {
    for dns_record in local.dns_records : "${dns_record.name}.${dns_record.type}" => dns_record if length(dns_record.rrdata) > 0
  }

  managed_zone = var.dns_public_zone_name

  name    = "${each.value.name}.${var.primary_domain_name}."
  type    = each.value.type
  ttl     = 3600
  rrdatas = each.value.rrdata
}