In the series of articles about ARM template, we will learn to create a template to secure a custom domain with SSL.
Via the Azure portal you can create an SSL binding with Azure App Service. When selecting SSL certificates in an App Service then Add binding, you can bind a custom domain with a certificate.
Today we will discover how to manage this operation via an Azure Resource Manager template. Our ARM template will be created in a new Azure Resource Group deployment project in Visual Studio.
Creation
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"websiteName": {
"type": "string"
},
"websiteCustomDomainName": {
"type": "string"
},
"certificateThumbprint": {
"type": "string"
}
}
...
}
- websiteName: the name of the azure app service such as myappname.azurewebsites.net.
- websiteCustomDomainName: the name of the custom domain such as mycustomdomain.com.
- certificateThumbprint: the thumbprint of the certificate to bind such as 22XXBE10XXE5D2DBAD29DXXXX7.
Now we will declare the resources of the ARM template:
{
...
"resources": [
{
"apiVersion": "2015-08-01",
"name": "[parameters('websiteName')]",
"type": "Microsoft.Web/sites",
"location": "[resourceGroup().location]",
"properties": {
"hostNameSslStates": [
{
"name": "[parameters('websiteCustomDomainName')]",
"sslState": "SniEnabled",
"thumbprint": "[parameters('certificateThumbprint')]",
"toUpdate": true
}
]
}
}
]
...
}
We can pay attention to several things here:
- The SSL binding is made via a property of the website named: hostNameSslStates.
- One or multiple hostNameSslStates can be declared.
- Each hostNameSslState have name property corresponding to the custom domain name and thumbprint property corresponding to the certificate thumbprint that will be binded.
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 'myappname' provisioning status is succeeded
If everything goes well, you should see the same kind of message as above.
To go further
To be able to perform the binding operation you first need to add the custom domain name to the app as seen in a previous article. If the custom domain is not added first you’ll get the following error message:
Resource Microsoft.Web/sites ‘myappname’ failed with message ‘Hostname ‘mycustomdomain.com’ does not exist’.
Summary
We have seen how to create an ARM template that will create an SSL binding to an app 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.