Using Powershell to manage Azure Web App Deployment Slots

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-AzureRmWebAppSlot -ResourceGroupName $rg -name $site -slot staging

List deployment slots for a web app

Get-AzureRmWebAppSlot -ResourceGroupName $rg -name $site

Delete a deployment slot

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

Specify slot app settings and connection strings

Set-AzureRmWebAppSlotConfigName -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-AzureRmWebApp -ResourceGroupName $rg -Name $site -AppSettings @{"SlotName" = "Production"; "Environment" = "Production"}
Set-AzureRmWebAppSlot -ResourceGroupName $rg -Name $site -Slot staging -AppSettings @{"SlotName" = "Staging"; "Environment" = "Staging"}

Perform a regular slot swap

Switch-AzureRmWebAppSlot -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-AzureRmWebAppSlot -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-AzureRmWebAppSlot -ResourceGroupName $rg -Name $site -SourceSlotName staging -DestinationSlotName production -SwapWithPreviewAction ApplySlotConfig

Now confirm that the app setting values have changed:

$stagingSite = Get-AzureRmWebAppSlot -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-AzureRmWebAppSlot -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-AzureRmWebAppSlot -ResourceGroupName $rg -Name $site -SourceSlotName staging -DestinationSlotName production -SwapWithPreviewAction ResetSlotSwap

13,856 views

ruslany on October 27th 2016 in Other, WAWS

PoorFairAverageGoodExcellent (6 votes, average: 4.67 out of 5)

5 Responses to “Using Powershell to manage Azure Web App Deployment Slots”

  1. Gravatar ImageAndris responded on 02 Nov 2016 at 3:12 am #

    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. Gravatar Imageruslany responded on 02 Nov 2016 at 10:07 am #

    Hi Andris, it is Switch-AzureRmWebAppSlot. It is a new cmdlet that supports swap with preview functionality. It was recently added. You can see the latest commands and their implementation on GitHub Azure Powershell repo.

  3. Gravatar ImageNiclas Rothman responded on 12 Nov 2016 at 9:59 pm #

    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

  4. Gravatar ImageMaurice responded on 13 Feb 2017 at 9:30 pm #

    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?

  5. Gravatar ImageRaghav responded on 06 Nov 2017 at 1:39 am #

    @Niclas Rothman. You can see that in the azure portal -> your web app -> activity log

Trackback URI | Comments RSS

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

XML Markup: If You want to add XML code to the comment please XML encode it first, otherwise the code will not show up.

Recently Published Articles