From today’s blog post I’ll walk through how to deploy an azure web application in different azure data center using an ARM (Azure Resource Manager) template.Azure ARM is the new deployment model introduce by Microsoft for resource deployment. It uses JSON to store the resource data and configuration.
How we do it?
- Create ARM template.
For create an ARM template we can use any text editor as your prefer here I used VS 2015 because it has more features than other text editors. - Create a Web Service Plan.
- Create a Web App in each Web Service Plan.
- Create Azure Traffic Manager to maintain high availability of web apps.
- Use a PowerShell Script to deploy it.
Create a new JSON file in VS.
Open the Visual Studio or any prefered code editor, in this I’ll show how to create JSON file using VS.
File–> New–> File
Then it opens another window then search for JSON click open.
Next you have to add the ARM schema that requires to create ARM template.
Create a Web Service Plan
In VS we can use JSON Outline to add resources by doing this VS do the hard part by adding relevant code snippets. We just want to change the relevant code parts as per our environment.
Following is the code for creating Web Service Plan.
{ "name": "[concat(parameters('webAppNamePrefix'),'-farm',copyIndex())]", "type": "Microsoft.Web/serverfarms", "location": "[parameters('webAppLocations')[copyIndex()]]", "apiVersion": "2014-06-01", "dependsOn": [ ], "copy": { "count": "[length(parameters('webAppLocations'))]", "name": "farmCopy" }, "tags": { "displayName": "[concat(parameters('webAppNamePrefix'),'-farm',copyIndex())]" }, "properties": { "name": "[concat(parameters('webAppNamePrefix'),'-farm',copyIndex())]", "sku": "[parameters('farmSKU')]", "workerSize": "[parameters('farmWorkerSize')]", "numberOfWorkers": 1 } },
Create a Web Apps
To create the web site I added following code snippets to end of the web app plan code. IF you use VS JSON Outline to add web site resource it will add a variable to variables section in the template.
{ "name": "[concat(parameters('webAppNamePrefix'),'-webapp',copyIndex())]", "type": "Microsoft.Web/sites", "location": "[parameters('webAppLocations')[copyIndex()]]", "apiVersion": "2015-08-01", "copy": { "count": "[length(parameters('webAppLocations'))]", "name": "siteCopy" }, "dependsOn": [ "farmCopy" ], "tags": { "[concat('hidden-related:', resourceGroup().id, '/providers/Microsoft.Web/serverfarms/', concat(parameters('webAppNamePrefix'),'-farm',0))]": "Resource", "displayName": "[concat(parameters('webAppNamePrefix'),'-farm',copyIndex())]" }, "properties": { "name": "[concat(parameters('webAppNamePrefix'),'-webapp',copyIndex())]", "serverFarmId": "[resourceId('Microsoft.Web/serverfarms/', concat(parameters('webAppNamePrefix'),'-farm',copyIndex()))]" }
Create Azure Traffic Manager
Added following code snippets to create traffic manager. We can’t add the traffic manger using JSON Outline so because of that we had to code it.
{ "type": "Microsoft.Network/trafficManagerProfiles", "name": "[concat(parameters('webAppNamePrefix'),'-tm')]", "apiVersion": "2015-11-01", "location": "global", "dependsOn": [ "siteCopy" ], "properties": { "profileStatus": "Enabled", "trafficRoutingMethod": "Performance", "dnsConfig": { "relativeName": "[concat(parameters('webAppNamePrefix'),'-tm')]", "ttl": 30 }, "monitorConfig": { "protocol": "HTTP", "port": 80, "path": "/" }, "endpoints": [ { "name": "[concat(parameters('webAppNamePrefix'),'-endpoint','0')]", "type": "Microsoft.Network/trafficManagerProfiles/azureEndpoints", "properties": { "targetResourceId": "[resourceId('Microsoft.Web/sites/',concat(parameters('webAppNamePrefix'),'-webapp','0'))]", "endpointStatus": "Enabled" } }, { "name": "[concat(parameters('webAppNamePrefix'),'-endpoint','1')]", "type": "Microsoft.Network/trafficManagerProfiles/azureEndpoints", "properties": { "targetResourceId": "[resourceId('Microsoft.Web/sites/',concat(parameters('webAppNamePrefix'),'-webapp','1'))]", "endpointStatus": "Enabled" } }, { "name": "[concat(parameters('webAppNamePrefix'),'-endpoint','2')]", "type": "Microsoft.Network/trafficManagerProfiles/azureEndpoints", "properties": { "targetResourceId": "[resourceId('Microsoft.Web/sites/',concat(parameters('webAppNamePrefix'),'-webapp','2'))]", "endpointStatus": "Enabled" } }, { "name": "[concat(parameters('webAppNamePrefix'),'-endpoint','3')]", "type": "Microsoft.Network/trafficManagerProfiles/azureEndpoints", "properties": { "targetResourceId": "[resourceId('Microsoft.Web/sites/',concat(parameters('webAppNamePrefix'),'-webapp','3'))]", "endpointStatus": "Enabled" } } ] } }
So we added all the components to the ARM template now lets deploy it using a PowerShell Script.
You can get the full code from here.
Deploy ARM template using PowerShell
To do this I created a PowerShell script. Following is the script.
# Define variables $location = 'southeastasia' $resourceGroupName = 'webapps-arm' $resourceDeploymentName = 'webapps-arm-paas' $templatePath = 'F:EducationBITGitHubARM-TemplatesPaaSWeb App' $templateFile = 'pass-arm.json' $template = $templatePath + '' + $templateFile # Create Resource Group New-AzureRmResourceGroup ` -Name $resourceGroupName ` -Location $Location ` -Verbose -Force # Deploy Resources New-AzureRmResourceGroupDeployment ` -Name $resourceDeploymentName ` -ResourceGroupName $resourceGroupName ` -TemplateFile $template ` -Verbose -Force
after you run this PS script you will prompt a verbose as follows.
When you look in to azure portal you will see the resources are deployed successfully.
Additional Resources
Building Azure Resource Manager Templates – The Basics