Search code examples
terraformterraform-provider-azureazure-rm

Creating a private dns zone a record in terraform


So I have a terraform script where I am creating a private DNS zone (azurerm_private_dns_zone.flexpsql) with a network link. I need to create an A record in a separate private DNS zone (data.azurerm_private_dns_zone.main) that is connected to my organizations network. How do I automatically get the IP address from the A record that is generated by the private DNS I just created?

This would be my terraform:

resource "azurerm_private_dns_zone" "flexpsql" {
  name                = "${local.flexpsql_name}.private.postgres.database.azure.com"
  resource_group_name = var.vnet_resource_group_name

}

resource "azurerm_private_dns_zone_virtual_network_link" "flexpsql" {
  name                  = local.flexpsql_name
  private_dns_zone_name = azurerm_private_dns_zone.flexpsql.name
  virtual_network_id    = var.virtual_network_id
  resource_group_name   = var.vnet_resource_group_name
}

I then will need to create an Private DNS A Record in another existing Private DNS

resource "azurerm_private_dns_a_record" "example" {
  name                = "${local.flexpsql_name}"
  zone_name           = data.azurerm_private_dns_zone.other.name
  resource_group_name = "rgtest111"
  ttl                 = 3600
  records             = split(",", join(",", azurerm_private_dns_a_record.flexpsql.records))
}

I will test the above.. But ultimately this is what I want to achieve


Solution

  • I tried to create a private DNS zone record using the network link created for another DNS zone and I was able to provision the requirement successfully.

    To automatically retrieve the IP address from the record generated by the private DNS zone you just created, you can use the data block in Terraform. The data block allows you to fetch information from existing resources or data sources.

    In the first step, I created a private_dns_zone.main with a network link.

    My terraform configuration:

    data "azurerm_resource_group" "example"{
        name = "v-bolliv"
    }
    
    resource "azurerm_private_dns_zone" "main" {
      name                = "mydomainvk.com"
      resource_group_name = data.azurerm_resource_group.example.name
    }
    
    resource "azurerm_private_dns_a_record" "main" {
      name                = "testvk"
      zone_name           = azurerm_private_dns_zone.example.name
      resource_group_name = data.azurerm_resource_group.example.name
      ttl                 = 3600
      records             = ["10.0.180.17"]
    }
    

    Output:

    enter image description here

    enter image description here

    enter image description here

    Now inorder to use this network link azurerm_private_dns_zone.main for the new azurerm_private_dns_zone.example

    My terraform configuration:

    data "azurerm_resource_group" "main"{
        name = "v-bolliv"
    }
    
    data "azurerm_private_dns_zone" "main" {
      name                = "mydomainvk.com"
      resource_group_name = data.azurerm_resource_group.main.name
    }
    
    
    data "azurerm_private_dns_a_record" "main"{
      name                = "testvk"
      zone_name           = data.azurerm_private_dns_zone.main.name
      resource_group_name = data.azurerm_resource_group.main.name
    }
    
    
    resource "azurerm_private_dns_zone" "example" {
      name                = "mydomainvksb.com"
      resource_group_name = data.azurerm_resource_group.main.name
    }
    
    resource "azurerm_private_dns_a_record" "example" {
      name                = "testsbvk"
      zone_name           = azurerm_private_dns_zone.example.name
      resource_group_name = data.azurerm_resource_group.main.name
      ttl                 = 3600
      records             = split(",", join(",", data.azurerm_private_dns_a_record.main.records))
    }
    

    Output:

    enter image description here

    enter image description here

    enter image description here

    Now as per requirement check the network IP provided by checking the output module.

    Output module:

    output  "private_dns_records" {
    
    value  =  [azurerm_private_dns_a_record.example.records]
    
    }
    

    Output:

    enter image description here