Managing CNAMEs with Azure Resource Manager Templates
Tags: azure, devops, sre, beginners
Published: 2019-08-13 00:00:00

---
title: Managing CNAMEs with Azure Resource Manager Templates
description: Learn how to use ARM Templates to manage a CNAME in less than 5 minutes.
date: "2019-08-13"
published: true
tags: 
- azure
- devops
- sre
- beginners 

---

So you purchased your own domain name and you want to host your blog on GitHub Pages. You discover that you need to configure a [CNAME record with your DNS provider](https://help.github.com/en/articles/setting-up-a-custom-subdomain).  

> 💡 You can manage other resources within a Resource Manager template. This post just focuses on one specific resource type intentionally! 

Pre-Requisites:

* Your [GitHub repository is enabled with GitHub Pages](https://help.github.com/en/categories/github-pages-basics) and associated with your custom domain to your GitHub repository. 
* [Azure is hosting your domain](https://azure.microsoft.com/en-us/services/dns/?WT.mc_id=devto-blog-jedavis). 

Rather than hand crafting DNS records, you can manage your [CNAMEs](https://docs.microsoft.com/en-us/azure/templates/microsoft.network/2018-05-01/dnszones/cname/?WT.mc_id=devto-blog-jedavis) via an Azure Resource Manager Template in Azure. This will let you version, review, and share your template in version control. The following assumes that you have the [Azure CLI](https://docs.microsoft.com/en-us/cli/azure/?WT.mc_id=devto-blog-jedavis=&view=azure-cli-latest) installed. 

Resource Manager templates are essentially JSON files that follow a specific format: 

```json
{
    "$schema": "http://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "",
    "parameters": {  },
    "variables": {  },
    "functions": [  ],
    "resources": [  ],
    "outputs": {  }
}
```

The $schema attribute is the [top level schema template](https://github.com/Azure/azure-resource-manager-schemas). 

The _contentVersion_ attribute is a way to version the template. This is how you signal significant changes in your template across versions.  

The _resources_ attribute is where you declare the resources that you want to deploy or update. 

These 3 attributes are the only required attributes of a Resource Manager template. I don't use the other JSON attributes in my example CNAME template. Here is an example template to manage a single CNAME resource:

{% gist https://gist.github.com/iennae/068557f98a60f444a0e714eb606c4daf %}

In this example template, there is one CNAME record defined _www_ for the domain _MYCUSTOMDOMAIN.org_ with the _TTL_ set to 300 seconds. I've configured it to point to _GITHUBUSER.github.io_. As long as my repo has been configured with the custom domain, GitHub will route requests to the right location.

If you want to manage multiple CNAME records, just describe a resource per CNAME record in the template. For example, I might want to have blog.example.org point to the same place as _www_. You can have one or multiple templates depending on how you want to organize your definitions!

Once I've got my template set up, I run through a set of common steps.  

 A good first test is to validate that the template is valid with _jsonlint_. If you're using an editor like [VS Code](https://code.visualstudio.com/docs/languages/json), you can get feedback about the validity of your JSON as you write your template. 

```shell
jsonlint azure_deploy.json
```

Any errors with the JSON can be corrected at this point. 

> 💡 All the following steps can be run from your local system with the Azure CLI installed or directly in [Cloud Shell](https://azure.microsoft.com/en-us/features/cloud-shell/?WT.mc_id=devto-blog-jedavis)!

Next, validate the RM template with the _validate_ subcommand. This will make sure that your template is syntactically correct. 

```shell
az group deployment validate --resource-group RESOURCEGROUP --template-file azure_deploy.json
```

Deploy the RM template with the _create_ subcommand. This starts the deployment. It will be a pretty quick operation.

```shell

az group deployment create --resource-group RESOURCEGROUP --template-file azure_deploy.json
```

Verify the deployment using the _show_ subcommand.

```shell
az group deployment show --resource-group RESOURCEGROUP --name azure_deploy
```

List the deployed resources using the _list_ subcommand and the _--output_ flag for clearer descriptions.

```shell
az group deployment list --resource-group RESOURCEGROUP --output table
```

If you want to learn more about building Resource Manager templates, or the underlying Resource Manager technology here are a few helpful resources:

* [Build Azure Virtual Machines with Resource Manager templates](https://docs.microsoft.com/en-us/learn/modules/build-azure-vm-templates/?WT.mc_id=devto-blog-jedavis)
* [Resource Manager Documentation](https://docs.microsoft.com/en-us/azure/azure-resource-manager/?WT.mc_id=devto-blog-jedavis)
* [Structure and Syntax of Resource Manager templates](https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-authoring-templates/?WT.mc_id=devto-blog-jedavis)