3 minute read

In some situations you may want to start using Deployment Slots in combination with your Azure App Service. This means you will have separate deployment slots instead of only the default production slot when running the App Service in Standard or Premium plan mode.

Deployment Slots

Deployment slots are actually live web apps with their own hostnames. Web app content and configurations elements can be swapped between deployment slots, including the production slot. Deploying your application to a deployment slot has the following benefits:

  • Validate web app changes in a staging deployment slot before swapping it with the production slot.
  • Eliminates downtime when you deploy your web app. The traffic redirection is seamless, and no requests are dropped as a result of swap operations.
  • Swap back, If the changes swapped into the production slot are not as you expected, you can perform the same swap immediately to get your "last known good site" back.

Resource Templates

This raises the question of how you are able to deploy deployment slots by using Azure Resource Templates.

A default App Service (web application) looks like the below snip-it.

{
  "apiVersion": "2015-08-01",
  "name": "[parameters('siteName')]",
  "type": "Microsoft.Web/sites",
  "location": "[variables('location')]",
  "tags": {
    "[concat('hidden-related:', resourceGroup().id, '/providers/Microsoft.Web/serverfarms/', parameters('hostingPlanName'))]": "Resource",
    "displayName": "Website"
  },
  "dependsOn": [
    "[concat('Microsoft.Web/serverfarms/', parameters('hostingPlanName'))]"
  ],
  "properties": {
    "name": "[parameters('siteName')]",
    "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', parameters('hostingPlanName'))]"
  },
  "resources": []
},

Deployment slots are in fact resources of the App Service it self and can be deployed as such. This means you can specify them within the resources array of your web application / App Service.

{
  "apiVersion": "2015-08-01",
  "name": "[parameters('tstSlot')]",
  "type": "slots",
  "tags": {
    "displayName": "TST Slot"
  },
  "location": "[variables('location')]",
  "dependsOn": [
    "[resourceId('Microsoft.Web/Sites', parameters('siteName'))]"
  ],
  "properties": {
  },
  "resources": []
}

The above snip-it adds a Deployment Slot to the App Service called “tst”. This name is defined within the parameters file of the template. When deployed the host name for the Deployment Slot will be:

  • https://[App Service Host Name]-[Slot Name].azurewebsites.net

Defining application settings on a Deployment Slot works the same as defining them on the application it self, and that is by adding a resource of the name “appsettings” to the resources array of the Deployment Slot.

{
  "name": "appsettings",
  "type": "config",
  "apiVersion": "2015-08-01",
  "dependsOn": [
    "[concat('Microsoft.Web/sites/', parameters('siteName'))]",
    "[concat('Microsoft.Insights/components/', parameters('appInsightsName-tst'))]"
  ],
  "tags": {
    "displayName": "AppSettings"
  },
  "properties": {
    "insights:InstrumentationKey": "[reference(concat('Microsoft.Insights/components/', parameters('appInsightsName-tst'))).InstrumentationKey]"
  }
}

This snip-it configures a application setting that is called “insights:InstrumentationKey” to the Application Insights component for Test that is created within the same Resource Template.

Slot Setting

As you may know a Deployment Slot setting has a marking option called “Slot Setting”. This means that the setting is sticky to the slot that gives you the option to keep settings specific for a specific environment. Take for example your production connection strings.

Within the “AppSettings” section in the Azure Resource Template you do not have an option to specify it.

To enable those markings another needs to be added to the App Service it self. This resource needs to be called “slotConfigNames” and must be of the type “config”. Within this resource you have to specify a property called “appSettingNames” that is an array of string items representing the application setting names.

{
  "name": "slotConfigNames",
  "type": "config",
  "apiVersion": "2015-08-01",
  "dependsOn": [
    "[concat('Microsoft.Web/sites/', parameters('siteName'))]"
  ],
  "tags": {
    "displayName": "slotConfigNames"
  },
  "properties": {
    "appSettingNames": [
      "insights:InstrumentationKey"
    ]
  }
}

In the above snip-it the application setting “insight:InstrumentationKey” is specified as a “Slot Setting” in order to keep it sticky with the slots.

If you are interested in the complete resource template you can take a look at my GitHub repository: