I am using terraform to deploy MX records in cloudflare. Created locals and flattened them filtering out different values
locals {
MXRecordSets = [
{
Name = "example.com.",
Type = "MX",
TTL = 3600,
MXRecords = [
{
Value = "asdsd.l.google.com",
Priority = 1
},
{
Value = "alt1.asdsd.l.google.com",
Priority = 5
},
{
Value = "alt2.asdsd.l.google.com",
Priority = 5
}
]
}
]
}
locals {
FlatMXRecordSets = merge([
for idx, MXRecordSet in local.MXRecordSets:
{
for MXRecord in MXRecordSet.MXRecords:
"${idx}-${MXRecord.Value}" => {
MXRecordSet = MXRecordSet
MXRecord = MXRecord["Value"]
Priority = MXRecord["Priority"]
}
}
]...)
}
resource "cloudflare_record" "com_records_mx" {
for_each = local.FlatMXRecordSets
zone_id = cloudflare_zone.example_com.id
name = each.value["MXRecordSet"].Name
value = each.value["MXRecord"].Value
type = each.value["MXRecordSet"].Type
proxied = false
ttl = 1
priority = each.value["MXRecord"].Priority
}
While running terraform apply, I am getting an error which says Can't access attributes on a primitive-typed value (string).
╷
│ Error: Unsupported attribute
│
│ on main.tf line 476, in resource "cloudflare_record" "com_records_mx":
│ 476: priority = each.value["MXRecord"].Priority
│ ├────────────────
│ │ each.value["MXRecord"] is "alt2.asdsd.l.google.com"
│
│ Can't access attributes on a primitive-typed value (string).