DEV Community

Jennifer Davis for Microsoft Azure

Posted on

Managing CNAMEs with Azure Resource Manager Templates

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.

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

Pre-Requisites:

Rather than hand crafting DNS records, you can manage your CNAMEs 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 installed.

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

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

The $schema attribute is the top level schema template.

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:

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, you can get feedback about the validity of your JSON as you write your template.

jsonlint azure_deploy.json
Enter fullscreen mode Exit fullscreen mode

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!

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

az group deployment validate --resource-group RESOURCEGROUP --template-file azure_deploy.json
Enter fullscreen mode Exit fullscreen mode

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


az group deployment create --resource-group RESOURCEGROUP --template-file azure_deploy.json
Enter fullscreen mode Exit fullscreen mode

Verify the deployment using the show subcommand.

az group deployment show --resource-group RESOURCEGROUP --name azure_deploy
Enter fullscreen mode Exit fullscreen mode

List the deployed resources using the list subcommand and the --output flag for clearer descriptions.

az group deployment list --resource-group RESOURCEGROUP --output table
Enter fullscreen mode Exit fullscreen mode

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

Oldest comments (0)