Deploy Azure Web App with slots using Azure ARM

Azure ARM uses simple JSON files for deploying infrastructure in Azure. While creating an azure web app or app service is not that tricky, usually you would require additional settings like deployment slots, application settings, connection strings, custom time zone etc. as well. It would be certainly nice if we can incorporate some of that as part of ARM templates itself so that we need not worry about it later. Since this topic is going to be lengthy, we’ll break into 3-4 smaller posts and also learn few azure resource manager tricks as well along the way. In this blog post, we’ll see how to create an Azure Web app and a slot associated with it using Azure ARM template.

Create an App Service Plan

To deploy a web app, first we need to create an app service plan. We can use below code to create an app service plan. It looks like below code:

ARM Code for creating app service plan
ARM Code for creating app service plan

As it is clear from the above code that we’ll also require to going the two properties named appServiceName and appServiceSkuName. So we need to define those two also in our ARM template.

Create an Azure Web App

We can create the web app using below code snippet:

ARM Code for creating web app / app service
ARM Code for creating web app / app service

Here, our web app name is defined inside a property named webAppName. There are two important properties to be noted about above code:
1. We are defining that app service plan is created first and then web app. This is done by mentioning app service plan as part of dependsOn in web app code.
2. We are associating the app service plan with web app using the properties section of above code.

Create an deployment slot

Deployment slots are in fact resources of the App Service itself and can be deployed as such. This means you can specify them within the resources array of your web application / App Service. ARM code for deployment slot looks like below:

ARM Code for Azure Web App Slot
ARM Code for Azure Web App Slot

Here, slot name is defined inside a property named stagingSlotName. Again, we have made sure that web service exists first before the slot is created by using the dependsOn in the above code. Since it is defined within the resources array of the web app itself, there is no need to link the two.

Here’s a full copy of the ARM template:


{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"appServiceName": {
"type": "string",
"minLength": 1
},
"appServiceSkuName": {
"type": "string",
"defaultValue": "F1",
"allowedValues": [
"F1",
"D1",
"B1",
"B2",
"B3",
"S1",
"S2",
"S3",
"P1",
"P2",
"P3",
"P4"
],
"metadata": {
"description": "Describes plan's pricing tier and capacity. Check details at https://azure.microsoft.com/en-us/pricing/details/app-service/"
}
},
"webAppName": {
"type": "string",
"minLength": 1
},
"stagingSlotName": {
"type": "string",
"minLength": 1
}
},
"variables": {},
"resources": [
{
"name": "[parameters('appServiceName')]",
"type": "Microsoft.Web/serverfarms",
"location": "[resourceGroup().location]",
"apiVersion": "2015-08-01",
"sku": {
"name": "[parameters('appServiceSkuName')]"
},
"dependsOn": [ ],
"tags": {
"displayName": "appService"
},
"properties": {
"name": "[parameters('appServiceName')]",
"numberOfWorkers": 1
}
},
{
"name": "[parameters('webAppName')]",
"type": "Microsoft.Web/sites",
"location": "[resourceGroup().location]",
"apiVersion": "2015-08-01",
"dependsOn": [
"[resourceId('Microsoft.Web/serverfarms', parameters('appServiceName'))]"
],
"tags": {
"[concat('hidden-related:', resourceId('Microsoft.Web/serverfarms', parameters('appServiceName')))]": "Resource",
"displayName": "webApp"
},
"properties": {
"name": "[parameters('webAppName')]",
"serverFarmId": "[resourceId('Microsoft.Web/serverfarms', parameters('appServiceName'))]"
},
"resources": [
{
"apiVersion": "2015-08-01",
"name": "[parameters('stagingSlotName')]",
"type": "slots",
"tags": {
"displayName": "webAppSlots"
},
"location": "[resourceGroup().location]",
"dependsOn": [
"[resourceId('Microsoft.Web/Sites', parameters('webAppName'))]"
],
"properties": {
},
"resources": []
}
]
}
],
"outputs": {}
}

We’ll see in upcoming posts how we can deploy additional settings as part of the template and also improve on above template created.

2 thoughts on “Deploy Azure Web App with slots using Azure ARM

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s