vrtareg
vrtareg

Reputation: 13

How to create DNS A Record usin Azure CLI or Terraform

I'm trying to automate DNS record creation in Azure Portal and wondering if there is any possibility to create Alias A DNS record which points to another Azure Resource like Public IP address instead of being IP record?

Looking to do this using Azure CLI or Terraform.

AWS Screenshot

Regards, Areg

Upvotes: 1

Views: 2058

Answers (3)

Jhonny Ramirez Zeballos
Jhonny Ramirez Zeballos

Reputation: 3196

resource "azurerm_dns_zone" "dns_zone" {
  name                = "bolivia.bo"
  resource_group_name = var.rg_name

  tags = {
    Environment = "QA"
    Team        = "Yes"
  }
}

resource "azurerm_dns_a_record" "dns_a_record" {
  name                = "@"
  zone_name           = azurerm_dns_zone.dns_zone.name
  resource_group_name = var.rg_name
  ttl                 = 3600
  target_resource_id  = var.public_ip_id
}

resource "azurerm_dns_a_record" "dns_www_a_record" {
  name                = "www"
  zone_name           = azurerm_dns_zone.dns_zone.name
  resource_group_name = var.rg_name
  ttl                 = 3600
  target_resource_id  = var.public_ip_id
}

resource "azurerm_dns_cname_record" "dns_api_cname_record" {
  name                = "api"
  zone_name           = azurerm_dns_zone.dns_zone.name
  resource_group_name = var.rg_name
  ttl                 = 3600
  record              = azurerm_dns_zone.dns_zone.name
}

Upvotes: 1

Rafael Aguilar
Rafael Aguilar

Reputation: 3279

You could create a Terraform module that has as a variable of the target resource for the A Record value, let's imagine the following scenario:

> "Record"          "TTL"      "RecordType"  "Target"
> www.contoso.com   3600   IN  A             1.1.1.1

Where "1.1.1.1" is the Address of the resource you want to link, for this example will be a azurerm_public_ip

You could use the following:

variable "target_ip" {
   type        = str
   description = "Target Resource address"
}

variable "record_a_name" {
   type        = str
   description = "Target Resource address"
}


resource "azurerm_resource_group" "contoso_rg" {
  name     = "acceptanceTestResourceGroup1"
  location = "West US"
}

resource "azurerm_dns_zone" "contoso_dns" {
  name                = "contoso.com"
  resource_group_name = azurerm_resource_group.example.name
}

resource "azurerm_dns_a_record" "contoso_dns_a_record" {
  name                = var.record_a_name
  zone_name           = azurerm_dns_zone.contoso_dns.name
  resource_group_name = azurerm_resource_group.contoso_rg.name
  ttl                 = 300

  records = [var.target_ip]

}

Whereas you can call the module this way:


data "azurerm_public_ip" "target_resource" {
  name                = "<name_of_resource_target>"
}


module "dns_example" {
  source = "<path_to_module>"

  record_a_name = "www"
  target_ip     = azurerm_public_ip.target_resource.ip_address  
}

As a side note, above code is far from being production quality, it is only to show how can be accomplished. I would also make TTL and Tags dynamic, I would decouple records from the zone and resource group declaration, among other good practices.

Reference: https://www.terraform.io/docs/providers/azurerm/r/dns_ns_record.html

Upvotes: 0

Nancy Xiong
Nancy Xiong

Reputation: 28304

You could create an empty A record with --target-resource which points to another Azure Resource ID in the Azure CLI command like this:

az network dns record-set a create -g myrg -n aaa -z example.com --target-resource "/subscriptions/xxx/resourceGroups/myrg/providers/Microsoft.Network/publicIPAddresses/vmb-ip"

enter image description here

Upvotes: 0

Related Questions