Objective: Trying to create DNZ Zones and DNS records via terraform on Azure
Code that I tried:
My Variable.tf:
variable "subnets" {
description = "subnet address prefixes to respective environments"
type = any
default = {
dev = {
gw_snet = {
attach_nsg = false
virtual_network_name = "vnet-hub"
name = "GatewaySubnet"
address_prefixes = ["xx.xxx.x.x/xx"]
}
dns-snet = {
attach_nsg = false
virtual_network_name = "vnet-hub"
name = "DNSSubnet"
address_prefixes = ["xx.xxx.x.x/xx"]
}
common_snet = {
attach_nsg = false
virtual_network_name = "vnet-hub"
name = "CommonSubnet"
address_prefixes = ["xx.xxx.x.x/xx"]
}
clientdata_snet = {
attach_nsg = true
virtual_network_name = "vnet-hub"
name = "ClientDataSubnet"
address_prefixes = ["xx.xxx.x.x/xx"]
}
}
}
}
My main.tf:
resource "azurerm_private_dns_a_record" "monitor_snapshot" {
for_each = { for k, v in var.subnets[var.env] : k => v }
name = "snapshot"
zone_name = azurerm_private_dns_zone.monitor.name
resource_group_name = azurerm_resource_group.rg[0].name
ttl = 3600
records = [cidrhost(azurerm_subnet.mysubnet["gw_snet"].address_prefixes[0], 11)]
}
Issue that I am facing:
Since I used creating multiple subnets using for_each in loop, so when I try to create dns record using particular subnet , here in this case "gw_snet" but, terraform is erroring out as it tried to add dnsrecord for each subnet which I dont want.. only using gw_snet it has to create.
can you please suggest what mistake I am doing here ?