我在 terraform 模块中有以下代码:
locals {
txt_records = flatten(
[
for i in var.DnsZones :
{
zone_name = i.ZoneName
resource_group_name = i.ResourceGroup
name = "@"
ttl = i.Ttl
records = i.TxtRecords
}
]
)
}
resource "azurerm_dns_zone" "zone" {
count = length(var.DnsZones)
name = var.DnsZones[count.index].ZoneName
resource_group_name = var.DnsZones[count.index].ResourceGroup
}
resource "azurerm_dns_txt_record" "record-txt" {
count = length(local.txt_records)
resource_group_name = local.txt_records[count.index].resource_group_name
zone_name = local.txt_records[count.index].zone_name
ttl = local.txt_records[count.index].ttl
name = local.txt_records[count.index].name
record {
value = local.txt_records[count.index].records[0]
}
record {
value = local.txt_records[count.index].records[1]
}
record {
value = local.txt_records[count.index].records[2]
}
record {
value = local.txt_records[count.index].records[3]
}
record {
value = local.txt_records[count.index].records[4]
}
record {
value = local.txt_records[count.index].records[5]
}
record {
value = local.txt_records[count.index].records[6]
}
depends_on = [azurerm_dns_zone.zone]
}
添加记录块似乎不是一种非常干净的方法,但我找不到更好的方法。
我试过用这种方式重构它:
resource "azurerm_dns_txt_record" "record-txt" {
count = length(local.txt_records)
resource_group_name = local.txt_records[count.index].resource_group_name
zone_name = local.txt_records[count.index].zone_name
ttl = local.txt_records[count.index].ttl
name = local.txt_records[count.index].name
dynamic "record" {
for_each = local.txt_records[count.index].records
iterator = i
content {
value = i.value
}
}
depends_on = [azurerm_dns_zone.zone]
}
但不幸的是,这会导致我们的 DNS 中只有一条 MX 记录,我们应该在其中插入 7 个项目。似乎每个项目都插入到前一个项目之上。
如您所见,每个记录块都需要在资源中分开:MX记录资源
谁能想到一种更好的方式来构建这个 terraform?