Reading in DNS zone ID's to use populating records

I’m looking for some help with my code that is creating DNS records from a CSV. Currently, I have to specify the DNS zone ID in my CSV - and whilst it works, it isn’t very intuitive.

Here is my code:

locals {
csv_simpledns = file("${path.module}/dns_simple_records.csv")
simpledns_instances = csvdecode(local.csv_simpledns)

csv_weighteddns = file("${path.module}/dns_weighted_records.csv")
weighteddns_instances = csvdecode(local.csv_weighteddns)

csv_pub_dnszones = file("${path.module}/dns_pub_zones.csv")
dns_pub_zone_instances = csvdecode(local.csv_pub_dnszones)

csv_priv_dnszones = file("${path.module}/dns_priv_zones.csv")
dns_priv_zone_instances = csvdecode(local.csv_priv_dnszones)
}

This will create our public zones

resource “aws_route53_zone” “public_zones” {
for_each = { for inst in local.dns_pub_zone_instances : inst.key => inst }
name = each.value.name
tags = var.tags
}

This will create our private zones

resource “aws_route53_zone” “private_zones” {
for_each = { for inst in local.dns_priv_zone_instances : inst.key => inst }
name = each.value.name
tags = var.tags

vpc {
vpc_id = each.value.vpc_id
}
}

This will implement simple DNS routing records through iterating through the CSV

resource “aws_route53_record” “simple” {
for_each = { for inst in local.simpledns_instances : inst.key => inst }
zone_id = each.value.zone_id
name = each.value.name
type = each.value.type
ttl = each.value.ttl
records = split(",", “${each.value.records}”)

}

Read in the current DNS Zones

data “aws_route53_zone” “main” {
for_each = { for inst in local.dns_pub_zone_instances : inst.key => inst }
#name = each.value.name
private_zone = false
}

This will implement weighted DNS routing records through iterating through the CSV

resource “aws_route53_record” “weighted” {
for_each = { for inst in local.weighteddns_instances : inst.key => inst }

zone_id = each.value.zone_id

Commented line above works when providing the zone ID in the CSV file

zone_id     = "${lookup(data.aws_route53_zone.main.zone_id, each.value.name, null)}"

attempt to lookup the zone_id corresponding to the name

name        = each.value.name
type        = each.value.type
ttl         = each.value.ttl
records     = split(",", "${each.value.records}")

weighted_routing_policy {
weight = each.value.weight
}
}

As you can see in the comments, I’ve tried to use a data lookup to determine the mapping from name to zone_id - but it errors.

Can anyone suggest how to do this?

Thanks