With Azure App Service it's easy to add a custom domain via the portal. Everything is well described in the Azure documentation on the Map a custom domain name to an Azure app page.
Today we will focus on creating an Azure Resource Manager template to easily add a custom domain to an app. For our ARM template to work properly, it is first required to create the DNS record and the website.
Creation
Let's declare the parameters of the ARM template:
{ "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#", "contentVersion": "1.0.0.0", "parameters": { "websiteName": { "type": "string" }, "websiteCustomDomainName": { "type": "string" } } ... }
Now we will declare the resources of the ARM template:
{ ... "resources": [ { "apiVersion": "2015-08-01", "name": "[concat(parameters('websiteName'),'/',parameters('websiteCustomDomainName'))]", "type": "Microsoft.Web/sites/hostNameBindings", "location": "[resourceGroup().location]", "properties": { "domainId": null, "hostNameType": "Verified", "siteName": "parameters('websiteName')" } } ] ... }
We can pay attention to two things here:
Example of use
The ARM template is now ready, let's open a Windows PowerShell and try it:
.\Deploy-AzureResourceGroup.ps1 -ResourceGroupName 'MyResourceGroupName' -ResourceGroupLocation 'canadaeast' -TemplateFile '.\azuredeploy.json'
...
Resource Microsoft.Web/sites/hostNameBindings 'myappname/mycustomdomain.com' provisioning status is succeeded
If everything goes well, you should see the same kind of message as above.
To go further
If the DNS record is not created when deploying the template you'll get the following error message:
A CNAME record pointing from mycustomdomain.com to myappname.azurewebsites.net was not found. Alternative record awverify.mycustomdomain.com to awverify.myappname.azurewebsites.net was not found either.
Summary
We have seen how to create an ARM template that will add a custom domain in Azure App Service.
You can download the example solution here:
Or
Browse the GitHub repository
Please feel free to comment or contact me if you have any question about this article.