Skip to main content

Swapping Azure App Service Deployment Slots

Learn to use App Service Deployment Slots to test a new release on a live URL before swapping it into production with zero downtime.

Overview and What You Will Learn

A deployment slot is a separate, live copy of an App Service app with its own URL, and a swap exchanges the staging slot with production without taking the app offline. In this lab you will deploy version 1 to production, deploy version 2 to a staging slot, test it on the staging URL, swap it into production, and swap back to roll it back.

  • Creating an App Service Plan that supports slots
  • Creating a staging slot that copies production's configuration
  • Marking app settings as slot-specific so they stay put during a swap
  • Swapping staging into production and reversing the swap
  • Warming up a slot before it takes traffic

Why This Matters in Production

Slots separate deploying a release from releasing it, so customers never see a version you have not tested on a real URL. Imagine a team at a company like Swiggy that deploys straight to a single production app and finds a bug affecting live orders minutes later, then must redeploy the old build under pressure. With a staging slot, the new version is tested first, and rolling back is a second swap instead of a redeploy.

Core Principles

A swap applies the target slot's settings to the staging slot, restarts and warms it up, and only then redirects production traffic to it. The previous production version lands in the staging slot, so a second swap restores it within moments. Slots share the App Service Plan's compute, so they add no separate hosting charge.

flowchart TD DEP[Deploy v2]:::tool --> STG[Staging slot
own live URL]:::soft STG -->|test and warm up| SWP{Swap}:::warn SWP --> PRD[Production slot
now serves v2]:::ok SWP --> OLD[Staging slot
now holds v1]:::soft OLD -.->|swap back| PRD classDef tool fill:#326CE5,color:#FFFFFF classDef soft fill:#DBEAFE,color:#1E293B classDef warn fill:#FFEDD5,color:#1E293B classDef ok fill:#10B981,color:#FFFFFF
Remember

Deployment slots need the Standard (S1) tier or higher. Free, Shared and Basic plans do not support them, and S1 allows up to five slots including production.

Detailed Step-by-Step Practical Lab

  1. Create a resource group, an App Service Plan, and a web app:
Bash
az group create --name rg-slots-lab-central --location centralindia
az appservice plan create \
--name asp-slots-lab \
--resource-group rg-slots-lab-central \
--sku S1 \
--is-linux
az webapp create \
--resource-group rg-slots-lab-central \
--plan asp-slots-lab \
--name inventory-app-rahul \
--runtime "NODE:20-lts"
  1. Build two tiny Node.js versions of the app. Each one reports its version and the value of an APP_ENV setting:
Bash
mkdir inventory-v1 inventory-v2
cat > inventory-v1/server.js << 'EOF'
require('http').createServer((req, res) => res.end('inventory v1 env=' + process.env.APP_ENV + '\n')).listen(process.env.PORT || 8080);
EOF
cat > inventory-v2/server.js << 'EOF'
require('http').createServer((req, res) => res.end('inventory v2 env=' + process.env.APP_ENV + '\n')).listen(process.env.PORT || 8080);
EOF
for v in v1 v2; do
(cd inventory-$v && npm init -y > /dev/null && npm pkg set scripts.start="node server.js" && zip -qr ../inventory-app-$v.zip .)
done
  1. Deploy version 1 to production and mark its APP_ENV as a slot-specific setting:
Bash
az webapp deployment source config-zip \
--resource-group rg-slots-lab-central \
--name inventory-app-rahul \
--src ./inventory-app-v1.zip
az webapp config appsettings set \
--resource-group rg-slots-lab-central \
--name inventory-app-rahul \
--slot-settings APP_ENV=production
  1. Create the staging slot as a copy of production's configuration, and give it its own slot-specific setting:
Bash
az webapp deployment slot create \
--resource-group rg-slots-lab-central \
--name inventory-app-rahul \
--slot staging \
--configuration-source inventory-app-rahul
az webapp config appsettings set \
--resource-group rg-slots-lab-central \
--name inventory-app-rahul \
--slot staging \
--slot-settings APP_ENV=staging
  1. Deploy version 2 to the staging slot only. Production keeps serving version 1:
Bash
az webapp deployment source config-zip \
--resource-group rg-slots-lab-central \
--name inventory-app-rahul \
--slot staging \
--src ./inventory-app-v2.zip
  1. Test both URLs. Look up the host names rather than guessing them, since newer apps can have a generated suffix:
Bash
PROD_HOST=$(az webapp show --resource-group rg-slots-lab-central \
--name inventory-app-rahul --query defaultHostName --output tsv)
STG_HOST=$(az webapp deployment slot list --resource-group rg-slots-lab-central \
--name inventory-app-rahul --query "[0].defaultHostName" --output tsv)
curl "https://$PROD_HOST"
curl "https://$STG_HOST"
# Expected: inventory v2 env=staging
  1. Send a few requests to the staging slot so the app has finished starting before it takes real traffic, then swap staging into production:
Bash
for i in 1 2 3 4 5; do curl -s "https://$STG_HOST" > /dev/null; done
az webapp deployment slot swap \
--resource-group rg-slots-lab-central \
--name inventory-app-rahul \
--slot staging \
--target-slot production
  1. Confirm production now runs version 2 and kept its own APP_ENV value, because that setting is slot-specific and did not move with the code:
Bash
curl "https://$PROD_HOST"
# Expected: inventory v2 env=production
  1. Roll back by swapping again. Production returns to version 1 within moments:
Bash
az webapp deployment slot swap \
--resource-group rg-slots-lab-central \
--name inventory-app-rahul \
--slot staging \
--target-slot production
curl "https://$PROD_HOST"
# Expected: inventory v1 env=production
  1. Clean up. The S1 plan bills by the hour, so delete it promptly:
Bash
az group delete --name rg-slots-lab-central --yes --no-wait

Production Best Practices & Common Pitfalls

Common Mistake

Deploying new code straight to the production slot with no staging step, then finding a critical bug only after customers are affected. A staging slot adds no extra hosting cost and gives you a real live environment before anyone sees the release.

Tip

A swap exchanges slots in moments and can be reversed by swapping again, which makes it one of the lowest-risk ways to release. For extra caution, use --action preview to apply production settings to staging and test with them before completing the swap.

  • Mark environment-specific settings as slot-specific. Connection strings and API keys that differ per environment must be flagged as slot settings, or they travel with the code and point production at staging resources.
  • Warm up before swapping. Hit the slot's key pages first, or set WEBSITE_SWAP_WARMUP_PING_PATH so App Service pings a path before routing traffic, otherwise customers hit cold starts.
  • Remember that slots share one plan. A heavy test on staging consumes the same CPU and memory as production, so avoid load testing in a slot on a small plan.
  • Try canary traffic when you want less risk. az webapp traffic-routing set --distribution staging=10 sends 10 percent of production traffic to staging, so a bad release reaches only a few users.
  • Keep the previous version in the slot after a swap. It is your rollback, so do not redeploy over it until the new release has proven stable.

Quick Reference & Troubleshooting Commands

Command Description
az webapp deployment slot create Create a new deployment slot
az webapp deployment slot swap Swap two slots, promoting one to production
az webapp deployment slot list List all slots for a web app
az webapp deployment source config-zip Deploy a zip package to a slot
az webapp config appsettings set --slot-settings Set a slot-specific app setting
az webapp traffic-routing set Send a share of production traffic to a slot

Explore More in Azure Compute Services

All 6 Topics

Frequently Asked Questions

Is Swapping Azure App Service Deployment Slots free to learn on DevOps Network?

Yes - this topic, like everything on DevOps Network, is 100% free with no paywall or sign-up gate.

What does the Swapping Azure App Service Deployment Slots topic cover?

Learn to use App Service Deployment Slots to test a new release on a live URL before swapping it into production with zero downtime.

Found a mistake, an outdated step or a broken link?