Using Powershell to manage Azure Web App Deployment Slots

This blog post has been updated from its original version in order to use the correct names of the PowerShell cmdlets.

This blog post explains how to perform common management tasks for Azure Web App deployment slots by using Powershell cmdlets. To learn more about deployment slots refer to the Azure documentation and my previous blog posts: Azure Web App Deployment Slot Swap with Preview and How to warm up Azure Web App during deployment slots swap.

Create a new deployment slot for an existing web app

$rg = "SomeResourceGroupName"
$site = "SomeSiteName"
New-AzWebAppSlot -ResourceGroupName $rg -name $site -slot staging

List deployment slots for a web app

Get-AzWebAppSlot -ResourceGroupName $rg -name $site

Delete a deployment slot

Remove-AzWebAppSlot -ResourceGroupName $rg -name $site -Slot staging

Specify slot app settings and connection strings

Set-AzWebAppSlotConfigName -ResourceGroupName $rg -name $site -AppSettingNames "SlotName","Environment" -ConnectionStringNames "PrimaryDB","SecondaryDB"

This cmdlet specifies the names of app settings and connection strings that will be treated as slot settings, meaning that they will remain with the slot during swap.

Set the app setting values on web app and on its slots

Set-AzWebApp -ResourceGroupName $rg -Name $site -AppSettings @{"SlotName" = "Production"; "Environment" = "Production"}
Set-AzWebAppSlot -ResourceGroupName $rg -Name $site -Slot staging -AppSettings @{"SlotName" = "Staging"; "Environment" = "Staging"}

Perform a regular slot swap

Switch-AzWebAppSlot -ResourceGroupName $rg -Name $site -SourceSlotName staging -DestinationSlotName production

Perform swap with preview

First check that the staging slot’s app setting have staging values:

$stagingSite = Get-AzWebAppSlot -ResourceGroupName $rg -Name $site -Slot staging
$stagingSite.SiteConfig.AppSettings

Name Value
---- -----
SlotName Staging
Environment Staging

Next perform the first step of the swap, which is to apply target slot’s setting values to the current staging slot:

Switch-AzWebAppSlot -ResourceGroupName $rg -Name $site -SourceSlotName staging -DestinationSlotName production -SwapWithPreviewAction ApplySlotConfig

Now confirm that the app setting values have changed:

$stagingSite = Get-AzWebAppSlot -ResourceGroupName $rg -Name $site -Slot staging
$stagingSite.SiteConfig.AppSettings

Name Value
---- -----
SlotName Production
Environment Production

At this point you would want to verify that the web app in the staging slot works as expected. You can make multiple requests to it to warm it up and fill up its caches, etc.

Once you are sure the app works as expected call this cmdlet to finish the swap:

Switch-AzWebAppSlot -ResourceGroupName $rg -Name $site -SourceSlotName staging -DestinationSlotName production -SwapWithPreviewAction CompleteSlotSwap

If for any reason you do not want to proceed with the swap you can reset the configuration on the staging slot back to its original state:

Switch-AzWebAppSlot -ResourceGroupName $rg -Name $site -SourceSlotName staging -DestinationSlotName production -SwapWithPreviewAction ResetSlotSwap

6 thoughts on “Using Powershell to manage Azure Web App Deployment Slots”

  1. Hi Ruslan!

    Thanks for the blog post!
    Just one thing: is the Switch-AzureRmWebAppSlot proper name of ARM cmdlet or should it actually be Swap-AzureRmWebAppSlot? Couldn’t find Switch-AzureRmWebAppSlot on msdn…

  2. Hi there,
    When working with slots, do you know any easy way to see the history of your deployments / Swapping. I Really would like being able to answear what version of my code is currently running in the production an staging slot.
    We are using continous deployments with git by the way!

    Thanks
    Nic

  3. How can you manage custom domain on a slot – the Set-AzureRmWebApp doesn’t seem to work and the Set-AzureRmWebAppSlot doesn’t have a hostnames parameter?

Leave a Reply

Your email address will not be published. Required fields are marked *