Azure Resource Templates and Deployment Slots

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:

Related Posts

Azure Managed Service Identity and Local Development Instead of storing user credentials of an external system in a configuration file, you should store them in the Azure Key Vault. Before MSI (Managed S...
The securitydata Azure resource group Most of the times companies have rules in place for managing their Azure environment. The main rules that should be in place are “Azure Policies” and ...
Point to Site VPN Client won’t install To connect an Azure App Service to a on-premise database you can make use of different solutions. Two of those solutions are: Hybrid Connection ...
Invoke Azure Function in your Visual Studio Team Services CI/CD pipeline A utility task is available for Visual Studio Team Services (VSTS) to invoke an http triggered Azure function. The ability to invoke a Function from y...
VSTS Extension for Azure Role Based Access Control Today I published an extension for Visual Studio Team Services (VSTS) that gives you the ability to add and remove role based access assignments in Az...
Listing Azure Services within a CSV file In some situations you will look into a current Azure Environment and the setup/governance of it and need to migrate or move resources around. The ...

4 comments

  • I have followed your tutorial, but how do we auto swap through the template?

    • You can not auto swap through the template. The only thing you can do is deploy to the production slot by using a template.

      If you automatically want to swap your slots you can do this in the following ways:
      – By hand trough the Azure portal
      – VSTS Release Management
      – PowerShell
      – Azure CLI

      • I am using slots for functions. I haven’t found any Azure CLI commands for slot functionality. I want to deploy to a staging slot and then swap to production in an automated fashion. Do you know if this is possible? Thanks.

        • Hi Cat,

          You could use the following PowerShell script to Swap Deployment slots

          Switch-AzureRmWebAppSlot -ResourceGroupName [Resource Group Name] -Name [Web App] -SourceSlotName [Source Slot] -DestinationSlotName [Destination Slot]  
          

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.