Setting Azure Web App Slot Auto Swap Using ARM Templates and Terraform

Written by Ken Dale
3
This post is days old.

We’re in the process of scaffolding out our Microsoft Azure environments using Terraform. We’re looking at deploying consistently from master going forward, but our new infrastructure design requires explicit swapping of staging slots to production. We want this to happen automatically though in our development environment, ensuring we’re always using the latest code as we try things out in the cloud.

Many items in Terraform are supported in a first-class manner. However, sometimes you find an item not currently supported. In the case of Azure, we can specify absent Terraform provider functionality we need using ARM templates. It’s a bit more verbose to setup than a simple built-in property, but it works.

Here’s the code:

Terraform

Assuming azurerm_app_service.app_service and azurerm_app_service_slot.app_service_slot are available (update this template as needed):

resource "azurerm_template_deployment" "app_service_auto_swap" {
  name                = "my-app-service-auto-swap" 
  resource_group_name = "YOUR_RESOURCE_GROUP_NAME"
  deployment_mode     = "Incremental"

  template_body = "${file("./path/to/app-service-slot-auto-swap.json")}"

  parameters {
    "existingWebAppLocation" = "eastus"
    "webAppName"             = "${azurerm_app_service.app_service.name}"
    "slotName"               = "staging"
    "autoSwapSlotName"       = "production"
  }

  depends_on = [
    "azurerm_app_service.app_service",
    "azurerm_app_service_slot.app_service_slot"
  ]
}

ARM Template

app-service-slot-auto-swap.json

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "existingWebAppLocation": {
      "type": "string",
      "metadata": {
        "description": "App location"
      }
    },
    "webAppName": {
      "type": "string",
      "metadata": {
        "description": "Example: app-name"
      }
    },
    "slotName": {
      "type": "string",
      "metadata": {
        "description": "Example: staging"
      }
    },
    "autoSwapSlotName": {
      "type": "string",
      "metadata": {
        "description": "Example: production"
      }
    }
  },
  "resources": [
    {
      "type": "Microsoft.Web/sites/slots",
      "name": "[concat(parameters('webAppName'), '/', parameters('slotName'))]",
      "apiVersion": "2015-08-01",
      "location": "[parameters('existingWebAppLocation')]",
      "properties": {
        "siteConfig": {
          "autoSwapSlotName": "[parameters('autoSwapSlotName')]"
        }
      }
    }
  ]
}
Suggested reading

Comments