Hello there,
I’m planning to manage all our Azure DNS records in one json-file per DNS zone.
One json file would look like this
{
"a": [
{
"name": "@",
"ip": "111.222.333.444"
},
{
"name": "www",
"ip": "111.222.333.444"
}
],
"aaaa": [],
"cname": [
{
"name": "autodiscover",
"alias": "autodiscover.outlook.com"
},
{
"name": "lyncdiscover",
"alias": "webdir.online.lync.com"
},
{
"name": "sip",
"alias": "sipdir.online.lync.com"
}
],
"mx": [
{
"preference": 10,
"host": "mail01.domain.com"
},
{
"preference": 10,
"host": "mail02.domain.com"
}
]
}
I’m using local variable block to read the json files and use jsondecode to get the content
locals {
# # get dns data from json file
dns_records_files = fileset(path.module, "/files/dns_zones/*.json")
dns_records_data = [for file in local.dns_records_files : jsondecode(file("${path.module}/${file}"))]
}
I now want to loop through each file and get all A, CNAME, MX, … records and create the appropriate DNS resource with the AzureRM provider.
I’ve tried this approach (alhtough I’m not sure if this is even possible)
resource "azurerm_dns_a_record" "dns_a_records" {
for_each = { for record in local.dns_records_data.a : record.name => record }
name = each.value.name
zone_name = azurerm_dns_zone.dns_zone[trimsuffix(basename(each.key), ".json")].name
resource_group_name = azurerm_resource_group.resource_group_core_dns.name
ttl = 3600
records = each.value.ip
}
Terraform validate outputs the following error
│ Error: Unsupported attribute
│
│ on dns_records.tf line 31, in resource "azurerm_dns_a_record" "dns_a_records":
│ 31: for_each = { for record in local.dns_records_data.a : record.name => record }
│ ├────────────────
│ │ local.dns_records_data is tuple with 2 elements
│
│ This value does not have any attributes.
Question:
- Is this even possible to loop through multiple json files, filter on A records and use only one resource block?
- What’s the correct approach to filter/select the A records?
Thank you so much for any hints and a happy new year in advance!
Denny