//reference: https://lucian.blog/terraform-csvs-netflix/
locals {
dns_records = csvdecode(file("mydomaincom_dns_records.csv"))
// csv needs the columns: name, proxied, type, record_data, mx_priority
}
resource "cloudflare_record" "dns_records" {
for_each = { for records in local.dns_records : records.name => records }
name = each.value.name
proxied = each.value.proxied
ttl = 1
type = each.value.type
value = each.value.record_data
zone_id = var.zone_id
// If record type is MX then add the MX priority
priority = (each.value.type == "MX") == true ? each.value.mx_priority : null
}
name: the name of the record i.e. www proxied: (true or false) whether to proxy the record via cloudflare or if its a DNS only record type: cname, A, AAAA, MX, TXT etc the type of record record data: the IP address, or hostname, or TXT data priority: the record priority, for MX records only
That last line in the above snippet will check if the record type being added is an MX record, if it is a MX record, it will then add the priority to the record.