Terraform多个AWS路由53 MX记录



我在tf中有locals来将MX记录推送到cloudflare,它的工作方式正是我想要的。我想将相同的locals用于Route53,但对于Route53,records应该组合为一。如何使用blowlocals使用地形推送到Route53。

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"]
}
}
]...)
}

最后,我使用下面的aws route53模块向aws推送更改,但它不起作用:

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
}

错误为[Tried to create resource record set [name='example.com.', type='MX'] but it already exists],因为我正在使用for_each,不应该使用它。还有别的办法吗?

您的MXRecordSets已经是扁平的,因此不需要扁平化。但它也是一个地图列表,这可能会导致更多的问题,因为列表取决于项目的顺序。因此,如果可能的话,最好只使用一张地图:

locals {
MXRecordSets = {
"mail.mwotest.xyz." = {
Type =  "MX",
TTL =  3600,
MXRecords =  [
{
"Value" =  "1 aspmx.l.google.com"
},
{
"Value" =  "5 alt1.aspmx.l.google.com"
}
]
}
}            
}

然后

resource "aws_route53_record" "mx_records" {
for_each = local.MXRecordSets
zone_id  = aws_route53_zone.carfeine_com.id
name     = each.key
type     = each.value.Type
records  = [ for key,record in each.value["MXRecords"]: record["Value"] ]
ttl      = 1
}

在上面,

[ for key,record in each.value["MXRecords"]: record["Value"] ]

将为MX生成正确的records作为:

records = ["1 aspmx.l.google.com", "5 alt1.aspmx.l.google.com"]

相关内容

  • 没有找到相关文章

最新更新