I have a setup of terraform cloudflare push with below configuration:
locals {
ResourceRecordSets= [
{
Name = "example.com.",
Type = "A",
TTL = 60,
ResourceRecords = [
{
Value = "1.2.3.4"
}
]
}
Using this locals
value I am pushing DNS records as:
resource "cloudflare_record" "com_records" {
count = length(local.ResourceRecordSets)
zone_id = cloudflare_zone.site.id
name = local.ResourceRecordSets[count.index].Name
value = local.ResourceRecordSets[count.index].ResourceRecords.value
type = local.ResourceRecordSets[count.index].Type
proxied = false
ttl = 1
}
The only thing I am struggling with is the value
section. sometime it can be a MX
records with multiple values under ResourceRecords
. What type of loop should I use?
You have to flatten your ResourceRecordSets, for example:
FlatResourceRecordSets = merge([
for idx, ResourceRecordSet in local.ResourceRecordSets:
{
for ResourceRecord in ResourceRecordSet.ResourceRecords:
"${idx}-${ResourceRecord.Value}" => {
ResourceRecordSet = ResourceRecordSet
ResourceRecord = ResourceRecord["Value"]
}
}
]...) # please do NOT remove the dots
then
resource "cloudflare_record" "com_records" {
for_each = local.FlatResourceRecordSets
zone_id = cloudflare_zone.site.id
name = each.value["ResourceRecordSet"].Name
value = each.value["ResourceRecord"]
type = each.value["ResourceRecordSet"].Type
proxied = false
ttl = 1
}