Creating Website Slots and SQL Elastic Pools using Azure Resource Templates

Posted by Rik Hepworth on Wednesday, January 18, 2017

Recently I have been helping a number of organisations automate the deployment of their applications to Azure and came across a couple of scenarios that were not documented: Deploying an App Services web site with slots and SQL connection string settings, and the creation of a SQL Elastic Pool. Of those, the SQL Elastic Pool I found to be written up already by Vincent-Philipe Lauzon and all credit to him - my template draws on his excellent article.

The Web slots and configuration, however, I didn’t find. There are templates that deploy a web site, and some that deploy configuration settings into that web site (indeed, creating a new Web+SQL template through Visual Studio does just that). However, I could find none that deployed slots and none that added the config to the slot.

You can find the full template in my GitHub Repo. The template code to deploy a slot and associated config is shown below. This sits in the nested resources bock within the website resource, for reference.

The trick with the config, as it turns out, is the resource type. If you examine the connectionStrings node within a slot through Resource Explorer you will see it reported as Microsoft.Web/sites/config. However, if you click the PowerShell tab for the same note you will see the type reported as Microsoft.Web/sites/slots/config. Make sure that the resource name matches the config section (i.e. connectionStrings - or appsettings, etc).

{
  "apiVersion": "2015-08-01",
  "name": "[concat(variables('website').websiteName, '/', variables('website').slotName)]",
  "type": "Microsoft.Web/Sites/slots",
  "location": "\[resourceGroup().location]",
  "dependsOn": [
    "[concat('Microsoft.Web/Sites/', variables('website').websiteName)]"
  ],
  "tags": {
    "displayName": "Slot"
  },
  "properties": {
  },
  "resources": [
    {
      "apiVersion": "2015-08-01",
      "name": "[concat(variables('website').websiteName, '/', variables('website').slotName, '/connectionStrings')]",
      "type": "Microsoft.Web/Sites/slots/config",
      "location": "[resourceGroup().location\]",
      "dependsOn": [
        "[concat('Microsoft.Web/Sites/', variables('website').websiteName, '/slots/', variables('website').slotName)\]"
      ],
      "tags": {
        "displayName": "SlotConnectionStrings"
      },
      "properties": {
        "DefaultConnection": {
          "value": "[concat('Data Source=tcp:', reference(concat('Microsoft.Sql/servers/', variables('sqlServer').name)).fullyQualifiedDomainName, ',1433;Initial Catalog=', variables('sqlServer').stagingDbname, ';User Id=', parameters('sqlAdminLogin'), '@', variables('sqlServer').name, ';Password=', parameters('sqlAdminPassword'), ';')\]",
          "type": "SQLServer"
        }
      }
    }
  ]
}