azure-resource-managercnameazure-rm-templateazure-dns

How to replace Azure DNS A record with CNAME record using ARM template


I am trying to replace an existing DNS A record with CNAME record having same name using ARM template. I have A record in DNS zone that looks like the following

I want to have CNAME record in the same DNS zone that should look like following

Currently my ARM template looks like following that creates A record

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {},
  "resources": [
    {
      "type": "Microsoft.Network/dnszones/A",
      "apiVersion": "2018-05-01",
      "name": "mylink",
      "properties": {
        "TTL": 60,
        "ARecords": [
          {
            "ipv4Address": "10.22.31.3"
          }
        ]
      }
    },
  ]
}

I tried to add another CNAME record to this are template which gave error on deployment.

"code": "Conflict",

"message": "The CNAME record could not be created because another record with the same name already exists in this zone."


Solution

  • As the error displayed, we can not create a CNAME record name that is the same as existing records sets in the same zone because CNAME record sets cannot coexist with other record sets with the same name. Refer here.

    So we need to delete the old A records or create CNAME records with a different record name in this case.

    When deploying your resources with the ARM template, you specify that the deployment is either an incremental update or a complete update. By default, the deployment is using incremental mode. You may consider using complete mode to delete resources that exist in the resource group but aren't specified in the template.

    However, the child resources type like dnszones / * does not support complete mode deletion. enter image description here

    In conclusion, we can not directly replace Azure DNS A record with the CNAME record using the ARM template. We need to delete the A records first with the Azure portal or other cmdlets then create CNAME records with the ARM template as usual.