Looping through several route53 zones and records

Hello guys,

I have to use a really nasty Jenkins library and I can only load Terraform code and variables on a certain way (I cannot have one variable file per zone for example).

I’m trying to import a bunch of zones and records created by another tool to Terraform but I’m having some issues on how to loop the right records with the right zones.

resource "aws_route53_zone" "zone" {
  for_each = var.zones
  name     = each.key
  comment  = each.value.comment
}

resource "aws_route53_record" "record" {
  for_each  = aws_route53_zone.zone
  zone_id   = each.value.zone_id
  type      = <Trying to loop inside zone.records and get the type>
  name      = <Trying to loop inside zone.records.type and get the name>
  ttl       = <Trying to loop inside zone.records.type.name and get the ttl>
  records   = <Trying to loop inside zone.records.type.name and get the records list>
}

My variable file looks like this:

zones = {
  "testzone.com" = {
    comment = "my comment"
    records = {
      TXT = {
        test = {
          ttl     = 300
          records = ["lalala"]
        }
        anothertest = {
          ttl     = 100
          records = ["blablabla"]
        }
      }

      CNAME = {
        www = {
          ttl     = 300
          records = ["test.com"]
        }
        foo = {
          ttl = 100
          records = ["bar.com"]
      }
    }
  }
}

I’m not sure on how to loop through all this, or if there’s a better way to organize these variables.

I’ve tried to create some local variables and loop everything from there, but I couldn’t do it properly, I also thought about loading the records from a JSON with the zone name, but I also didn’t have much luck with that.