I have locals in tf to push MX records to cloudflare and it works exactly I want it to. The same locals I want to use for Route53 but for route53 the records should be combined to one. How I can use blow locals to push to Route53 using terraform.
locals {
MXRecordSets = [
{
Name = "example.com.",
Type = "MX",
TTL = 3600,
MXRecords = [
{
"Value" = "1 aspmx.l.google.com"
},
{
"Value" = "5 alt1.aspmx.l.google.com"
}
]
}
]
}
locals {
FlatMXRecordSets = merge([
for idx, MXRecordSet in local.MXRecordSets:
{
for MXRecord in MXRecordSet.MXRecords:
"${idx}-${MXRecord.Value}" => {
MXRecordSet = MXRecordSet
MXRecord = MXRecord["Value"]
}
}
]...)
}
and finally I am using below aws route53 module to push changes to AWS, but its not working:
resource "aws_route53_record" "mx_records" {
for_each =local.FlatMXRecordSets
zone_id = aws_route53_zone.carfeine_com.id
name = each.value["MXRecordSet"].Name
type = each.value["MXRecordSet"].Type
records = [ each.value["MXRecord"] ]
ttl = 1
}
The error comes as [Tried to create resource record set [name='example.com.', type='MX'] but it already exists] because I am using for_each and I should not use that. Any other way ?