Menu

Virtual Geek

Tales from real IT system administrators world and non-production environment

Terraform deploy create A Private DNS Record in Microsoft Azure from list of objects

This is an example of creating multiple private DNS records in Microsoft Azure from list of objects. Here I am starting with variables.tf, I am using below list of objects as type in variable.

#variables.tf
variable "dns_records" {
  type = list(object({
    name                = string
    resource_group_name = string
    zone_name           = string
    ttl                 = string
    records             = set(string)
  }))
}

This is my simple configuration and I am using here azurerm_private_dns_a_record resource. Since my variable type for dns_records is list of objects I am using, for_each loop to loop through create multiple private dns records.

The for_each meta-argument is utilized to make numerous instances of a resource or module based on the elements of a map, set, or list. The expression inside for_each creates a map where the keys are the unique identifiers for the resource instances, and the values are the elements of the original map, set, or list.

  1. for record in var.dns_records: This is a for expression that iterates over each element in the var.dns_records variable. var.dns_records is assumed to be a map, set, or list defined in the Terraform configuration.

  2. record.name => record: This is the key-value pair that creates the map for the for_each meta-argument. In this case, record.name is used as the key, and record itself is used as the value for each element in var.dns_records. This means that each unique record.name will become a key in the map, and its corresponding value will be the entire record element.

#main.tf
provider "azurerm" {
  features {}
}

# data "azurerm_private_dns_zone" "name" {
#   name = "test.vcloud-lab.com"
#   resource_group_name = "vcloud-lab.com"
# }

# output "name" {
#   value = data.azurerm_private_dns_zone.name.name
# }

resource "azurerm_private_dns_a_record" "zone" {
  for_each            = { for record in var.dns_records : record.name => record }
  name                = each.value.name
  resource_group_name = each.value.resource_group_name
  zone_name           = each.value.zone_name
  ttl                 = each.value.ttl
  records             = each.value.records
}

Below is the data I provided in tfvars file.

#dev.tfvars
dns_records = [{
  name                = "record1"
  resource_group_name = "vcloud-lab.com"
  zone_name           = "test.vcloud-lab.com"
  ttl                 = "3600"
  records             = ["192.168.34.100", "192.168.34.101"]
  },
  {
    name                = "record2"
    resource_group_name = "vcloud-lab.com"
    zone_name           = "test.vcloud-lab.com"
    ttl                 = "3600"
    records             = ["192.168.34.101"]
}]

Below is the screenshot from the deployment apply of azurerm private dns a record zone instance resource.

VMware terraform azure apply -var-file dev.tfvars --auto-approve azurerm_private_dns_a_record resource variable fqdn id records zone_name.png

Download this terraform configuration here or it is also available on github.com.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
PS D:\Projects\Terraform\Create_of_DNS_A_Record_from_List_Object> terraform apply -var-file dev.tfvars --auto-approve

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # azurerm_private_dns_a_record.zone["record1"] will be created
  + resource "azurerm_private_dns_a_record" "zone" {
      + fqdn                = (known after apply)
      + id                  = (known after apply)
      + name                = "record1"
      + records             = [
          + "192.168.34.100",
          + "192.168.34.101",
        ]
      + resource_group_name = "vcloud-lab.com"
      + ttl                 = 3600
      + zone_name           = "test.vcloud-lab.com"
    }

  # azurerm_private_dns_a_record.zone["record2"] will be created
  + resource "azurerm_private_dns_a_record" "zone" {
      + fqdn                = (known after apply)
      + id                  = (known after apply)
      + name                = "record2"
      + records             = [
          + "192.168.34.101",
        ]
      + resource_group_name = "vcloud-lab.com"
      + ttl                 = 3600
      + zone_name           = "test.vcloud-lab.com"
    }

Plan: 2 to add, 0 to change, 0 to destroy.

Changes to Outputs:
  - name = "test.vcloud-lab.com" -> null
azurerm_private_dns_a_record.zone["record2"]: Creating...
azurerm_private_dns_a_record.zone["record1"]: Creating...
azurerm_private_dns_a_record.zone["record1"]: Creation complete after 3s [id=/subscriptions/9e22xxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/vcloud-lab.com/providers/Microsoft.Network/privateDnsZones/test.vcloud-lab.com/A/record1]
azurerm_private_dns_a_record.zone["record2"]: Creation complete after 3s [id=/subscriptions/9e22xxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/vcloud-lab.com/providers/Microsoft.Network/privateDnsZones/test.vcloud-lab.com/A/record2]

Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
PS D:\Projects\Terraform\Create_of_DNS_A_Record_from_List_Object> 

If in case if there is an error out while creating dns record that, DNS zone is not found found on Azure. 

 Error: creating/updating DNS A Record "record2" (Zone "test.vcloud-lab.com" / Resource Group "vcloud-lab.com"): unexpected status 404 with error: ParentResourceNotFound: Failed to perform 'write' on resource(s) of type 'dnszones/A', because the parent resource '/subscriptions/9e22xxxx-xxxx-xxxx-xxxx-a26fec38e029/resourceGroups/vcloud-lab.com/providers/Microsoft.Network/dnszones/test.vcloud-lab.com' could not be found.

   with azurerm_dns_a_record.zone["record2"],
│   on main.tf line 14, in resource "azurerm_dns_a_record" "zone":
   14: resource "azurerm_dns_a_record" "zone" {

Make sure you are using correct resource to deploy Azure resource, There are two different Azure terraform resources azurerm_dns_a_record and azurerm_private_dns_a_record, make sure you use the correct resource in your terraform configurations to resolve this issue.

Microsoft Private dns zone azure vmware terraform hcl hashicorp zone not found error no private dns zones to display tf tfvars.png

Useful Articles
Terraform testing variable map object values without resource configuration part 2
Terraform error retrieving storage account failure responding to request StatusCode 404 StorageAccountNotFound The storage account was not found
Terraform clone virtual machine template in VMware vSphere vCenter from CSV file
Terraform A reference to a resource type must be followed by at least one attribute access, specifying the resource name
Terraform one module deploy null or multiple resources based on input
Hashicorp Terraform map and object inside module and variable example
Terraform module clone VMware vSphere Linux and Windows virtual machine
Terraform VMware vSphere Virtual Machine customization clone failed on Windows
Terraform VMware vSphere Virtual Machine cloning Operating system not found
How to Install Minikube on Ubuntu - Step by Step
MINIKUBE Unable to start VM - This computer doesn't have VT-X AMD-v enabled
Install and Setup your own Kubernetes Cluster with K3s
Rancher k3s.yaml permission denied when using kubectl - Kubernetes
Hashicorp Terraform map and object inside module and variable example
Terraform testing local variables and output csv file without resource Part 1

Go Back

Comment

Blog Search

Page Views

11356472

Follow me on Blogarama