Route53 record identifier and terraform resource identifier

Hey guys,

I am a little confused on how to loop through route53 records with and without identifiers without messing with Terraform resource ids.

I am currently using yamldecode to allow partial record entries for each zone and it works for simple entries, but I couldn’t find a way to deal with entries that should be duplicated separated by the identifier (AWS does not allow duplicated entries without an identifier and does not allow to create an identifier on simple entries).

I’m struggling to find a way to make Terraform create these duplicated records without messing with the Terraform resource ids.

I’ve tried to loop without success on locals, but I currently have this that:

locals {
  # Look for records if they exist.
  records = try(yamldecode(file("${path.root}/zones/${var.zone_name}.yaml")), null)
}

resource "aws_route53_record" "CNAME" {
  for_each              = try(local.records.CNAME, {})
  zone_id               = aws_route53_zone.it.zone_id
  name                  = each.key
  type                  = "CNAME"
  health_check_id       = try(each.value.health_check_id, null)
  ttl                   = try(each.value.alias, null) == null ? try(each.value.ttl, 300) : null
  records               = try(each.value.records, null)
  set_identifier        = try(each.value.set_identifier, null) # How can I loop and glue this with the policy dynamic block?

  dynamic "alias" {
    for_each = try(each.value.alias.*, {})
    content {
      name                   = each.value.alias.name
      zone_id                = try(each.value.alias.zone_id, "Z35SXDOTRQ7X7K")
      evaluate_target_health = try(each.value.alias.evaluate_target_health, true)
    }
  }

  dynamic "latency_routing_policy" {
    for_each = try(each.value.latency_routing_policy.*, {})
    content {
      region = try(each.value.latency_routing_policy.region, "us-east-1")
    }
  }
}

I’ve tried to make this yaml in several ways, but I still couldn’t find a good way to do it.

CNAME:
  test: # this doesn't work
    ttl: 10
    policy: 
      type: latency_based_routing
      identifier:
        test1:
          region: us-east-1
          record: test1.com
        test2:
          region: us-west-1
          record: test2.com

  test3: # This works
    ttl: 300
    records:
    - test4.com

Any ideas on how to loop through this, or a better way to write this yaml?