Post

How to Setup and Use App Service Deployment Slots

Azure App Service offers a feature called Deployment Slots for which allows you to have multiple environments for your Web App each with their own separate host names.

This is useful when you want to first deploy changes to a staging environment, then once you are comfortable the Web App is performing as expected you can swap the staging environment with the production environment.

The benefits from this is that:

  • It enables you to verify the application is working as expected in a production like environment
  • It eliminates down time in production when deploying changes
  • It enables you to rollback if there is a problem with the production environment following the deployment

This content is available in video form on the Cloud Engineer Skills YouTube channel.

Setting up and using App Service Deployment Slots

In this tutorial, we will cover:

  1. Creation of the Resource Group, App Service Plan and Web App using the Azure CLI
  2. Creation of a Web App Deployment Slot for the staging environment using the Azure Portal
  3. Creating Deploy Slot Application Settings using the Azure Portal
  4. Deploying a Web App into the staging Deployment Slot using the Azure CLI
  5. Deploying your Web App to the production Deployment Slot by manually swapping the staging and production Deployment Slots
  6. Rolling back a deployment to production by manually swapping the staging and production Deployment Slots
  7. Redeploying your changes to your Web App by updating the staging slot, and then swapping Deployment Slots

Create the Resources via the Azure CLI

We will first need to create the following Azure resources.

  1. Azure Resource Group
  2. Azure App Service Plan
  3. Azure Web App

Create the Resource Group

1
az group create --name cloudengineerskills-as-rg --location eastus

Create the App Service Plan

Your App Service Plan needs to be on any of the Standard, Premium, and Isolated App Service Plan tiers to enable to use of Deployment Slots.

Different Tiers have different maximum Deployment Slots available, see App Service Limits for more information.

I will use the P0V3 SKU which is a Premium App Service Plan Tier for the App Service Plan.

1
az appservice plan create --resource-group cloudengineerskills-as-rg --name NodeAppPlan --number-of-workers 1 --sku P0V3 --is-linux --location eastus

Create the App Service Web App

Next we will create the App Service Web App, the application will run as code on the Node version 20 LTS run time.

1
az webapp create -g cloudengineerskills-as-rg -p NodeAppPlan -n CloudEngineerSkillsSlots --runtime "NODE:20-lts"

Creating Deployment Slots via the Azure Portal

Azure Portal Steps to create a Deployment Slot:

  1. In the Azure Portal, select your App Service Web App
  2. In the left hand sidebar, under Deployment select Deployment slots
  3. Select Add
  4. Enter the Name as staging, leave Clone settings from as Do not clone settings and select Add
  5. Select the newly added Deployment slot for the staging environment and in the Overview screen note the Default domain for your staging environment

Alternatively, you can also create the deployment slot using the the Azure CLI.

1
az webapp deployment slot create --name CloudEngineerSkillsSlots --resource-group cloudengineerskills-as-rg --slot staging

Creating Deployment Slot Settings under Environment Variables via the Azure Portal

In our Web App, we will use an Environment Variable called ENVIRONMENT that we will display in the Hello World message outputted by the Express.js Node Server. When we are on the staging deployment slot ENVIRONMENT should be set to Staging, in the production deployment slot it should be set to Production.

Environment variables can be marked as deployment slot settings meaning that the values are tied to the particular deployment slot, so that if the deployment slot is swapped, it keeps the value of the environment variable bound to slot it was created with.

Azure Portal Steps to create a Deployment Slot Settings:

  1. In the Azure Portal, on the App Service Web App Overview screen, in the left hand sidebar, under Settings select Environment variables
  2. Under App Settings, select Add
  3. Enter the Name as ENVIRONMENT and if you are still on the staging slot enter Staging, or if you are on the production slot enter Production
  4. Check the Deployment slot setting checkbox and select Apply

Switch beteen the slots, and repeat this process so that both the production slot and staging slots have the environment variable ENVIRONMENT set as a Deployment slot setting.

Deploy the Web App to Staging Slot via the Azure CLI

We will now deploy a Node.js application with an index.js file and package.json file along with it’s dependencies using the Azure CLI to the staging deployment slot.

See the code below for index.js

1
2
3
4
5
6
7
8
9
10
11
12
const express = require("express");
const app = express();

const port = 8080;

app.get("/", (req, res) => {
  res.send(`Hello World! from ${process.env.ENVIRONMENT}`);
});

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`);
});

See the code below for package.json.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
  "name": "deploy-code",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node index.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.19.2"
  }
}

Install the dependencies using npm, using npm install.

1
npm install

Now we are ready to deploy the Web App.

Create a ZIP file of the deployment, then use the az webapp deploy command to deploy the ZIP file into the staging Deployment Slot.

1
2
Compress-Archive -Path \* -DestinationPath deployment.zip
az webapp deploy --resource-group cloudengineerskills-as-rg --name CloudEngineerSkillsSlots --slot staging --src-path deployment.zip --type zip

Once the deployment has completed successfully, navigate to the URL of the staging deployment slot in the web browser and you should see the text Hello World! from Staging.

Deploy to Production by Swapping the Staging and Production Slot

If you check the production deployment slot you should see a page generated by Azure containing the text Your web app is running and waiting for your content and NodeJS logo.

Next we will deploy our changes to the production deployment slot by swapping the staging slot with the production slot.

Azure Portal Steps to Manually Swap Deployment Slots:

  1. In the Azure Portal, select your App Service Web App
  2. In the left hand sidebar, under Deployment select Deployment slots
  3. Select Swap
  4. Select the staging deployment slot as the source, and the production deployment slot as the target
  5. Select the Start Swap button and wait for the swap to complete with a green tick

Once the deployment has completed successfully, navigate to the URL of the production deployment slot in the web browser and you should see the text Hello World! from Production.

If you check the staging deployment slot you should see the starter NodeJS screen from Azure.

Rolling Back

Imagine something has gone wrong with the deployment to the production environment, and you need to fix it quickly, we can rollback by swapping the deployment slots back. Let’s try this out.

Repeat the steps in the previous step to roll back these changes.

Once complete the staging environment should see the text Hello World! from Staging and the production environment should see the starter NodeJS screen from Azure.

Subsequent Deployment

Imagine you need to deploy a code fix that needs to end up in the production environment.

We will do this by coding the fix, deploying it to the staging environment, then performing the swap again.

See the updated code below for index.js

1
2
3
4
5
6
7
8
9
10
11
12
const express = require("express");
const app = express();

const port = 8080;

app.get("/", (req, res) => {
  res.send(`Hello World! from ${process.env.ENVIRONMENT} (All Fixed!)`);
});

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`);
});

Create a ZIP file of the deployment, then use the az webapp deploy command to redeploy the ZIP file into the staging Deployment Slot.

1
2
Compress-Archive -Path \* -DestinationPath deployment.zip
az webapp deploy --resource-group cloudengineerskills-as-rg --name CloudEngineerSkillsSlots --slot staging --src-path deployment.zip --type zip

Check the staging slot has the text Hello World! from Staging (All Fixed!), then repeat the steps in the Deploy to Production by Swapping the Staging and Production Slot section to swap the staging environment with the production environment.

From there on the production deployment slot you should see the text Hello World! from Production (All Fixed!).

Clean up the resources

Clean up all of the Azure resources you have created in this tutorial by deleting the Azure Resource Group.

1
az group delete --name cloudengineerskills-as-rg

Further Reading

This post is licensed under CC BY 4.0 by the author.