How to Use Deployment Slots in Azure for Smooth App Updates
Use
deployment slots in Azure App Service to create separate live environments for your app, like staging and production. Deploy your updates to a slot, test them, then swap the slot with production to update your app without downtime.Syntax
Deployment slots are named environments under your Azure App Service. You create slots like staging or testing alongside production. You deploy your app code to a slot URL, test it, then swap slots to make the new version live.
Key parts:
App Service: Your web app hosting service.Slot name: The environment name, e.g.,staging.Swap: The action to exchange content and settings between slots.
bash
az webapp deployment slot create --resource-group <resource-group> --name <app-name> --slot <slot-name> az webapp deployment slot swap --resource-group <resource-group> --name <app-name> --slot <slot-name>
Example
This example shows how to create a staging slot, deploy code to it, test, and then swap it with production.
bash
az webapp deployment slot create --resource-group MyResourceGroup --name MyWebApp --slot staging
az webapp deployment source config-zip --resource-group MyResourceGroup --name MyWebApp --slot staging --src myapp.zip
# Test your app at https://MyWebApp-staging.azurewebsites.net
az webapp deployment slot swap --resource-group MyResourceGroup --name MyWebApp --slot stagingOutput
Deployment slot 'staging' created.
Deployment package uploaded to 'staging' slot.
Slots 'staging' and 'production' swapped successfully.
Common Pitfalls
- Forgetting to configure slot-specific settings can cause issues; use
slot-settingsto keep secrets or connection strings unique per slot. - Swapping slots without testing can cause downtime or errors.
- Deploying directly to production slot skips testing benefits.
bash
az webapp config appsettings set --resource-group MyResourceGroup --name MyWebApp --slot staging --settings "DB_CONNECTION=staging-db-connection" --slot-settings
# Wrong: Deploying directly to production
az webapp deployment source config-zip --resource-group MyResourceGroup --name MyWebApp --src myapp.zip
# Right: Deploy to staging slot first
az webapp deployment source config-zip --resource-group MyResourceGroup --name MyWebApp --slot staging --src myapp.zipQuick Reference
| Command | Purpose |
|---|---|
| az webapp deployment slot create | Create a new deployment slot |
| az webapp deployment source config-zip | Deploy app code to a slot |
| az webapp deployment slot swap | Swap two slots to make new code live |
| az webapp config appsettings set --slot-settings | Set slot-specific app settings |
Key Takeaways
Create deployment slots to test app updates safely before going live.
Deploy your app to a non-production slot, test it, then swap with production.
Use slot-specific settings to keep secrets and configs separate per slot.
Avoid deploying directly to production to prevent downtime.
Swapping slots is a zero-downtime way to update your app.