Azure App Services: Create an App Service and associated Slots using PowerShell
In this blog I will share one way to create an App Service Plan, App Service and associated Slots using PowerShell. I will also show you how to delete all created resources with one command or delete one resource at a time.
My blogs have relatively simple, and sometimes complex, examples and I’m hoping that you will be able to tailor them to your need or use them in your own scripts.
Prerequisites
- Install Azure PowerShell if you haven’t already. You can use Cloud Shell if you prefer to stay within Azure Portal.
- Install Azure Active Directory PowerShell module if you haven’t already. You can use Cloud Shell if you prefer to stay within Azure Portal.
- An existing Resource Group to host the new App Service.
- Optionaly, an App Service Plan to hosty new App Service.
Introduction
The goal of this blog is to show one way to accomplish a task. It is not to show how to write a perfect script, the perfect solution to a challenge or the perfect process to accomplish a task.
PowerShell Cmdlets Used
- Connect-AzAccount Connect to Azure with an authenticated account for use with cmdlets from the Az PowerShell modules.
- Set-AzContext Sets the tenant, subscription, and environment for cmdlets to use in the current session.
- Get-AzAppServicePlan Gets an Azure App Service plan in the specified resource group.
- New-AzAppServicePlan Creates an Azure App Service plan in a given Geo location.
- New-AzWebApp Creates an Azure Web App.
- New-AzWebAppSlot Creates an Azure Web App slot.
- Remove-AzWebAppSlot Removes an Azure Web App Slot provided the resource group and Web App name. This cmdlet, by default, also removes all slots and metrics.
- Remove-AzWebApp Removes an Azure Web App.
- Remove-AzAppServicePlan Removes an Azure App Service plan.
Sign in to Azure
- Sign in to Azure. If you have multiple subscriptions or tenants, make sure to sign in to the correct subscription.
- You can use Set-AzContext to set the tenant, subscription, and environment for cmdlets to use in the current session.
1Connect-AzAccount -Subscription "aa1111a1-1111-1a1a-11a1-1111a1a1a1a1"
App Service and App Service Plan Parameters
We will create an App Service with the following parameters. If the App Service Plan exists, use it. Otherwise, create a new App Service Plan.
App Service | app1-saad-dev | AppServicePlan | asp-app1-saad-dev |
Slots | Dev and QA | Tier | Basic |
Resource Group | rg-app1-saad-dev | NumberofWorkers | 1 |
Location | South Central US | WorkerSize | Small |
Create an App Service Plan
We will check if the App Service Plan exists or not. If it exists, we will use it, otherwise we will create a new one.
- Define variables with App Service Plan parameters.
- Get App Service Plan.
- (a) If the App Service Plan does not exist, create a new App Service Plan.
A Standard App Service Plan tier or better is required to support slot creation.
123456789101112131415# 1. Define variables with App Service Plan parameters$AppServicePlanName = "asp-app1-saad-dev"$Tier = "Standard"$NumberofWorkers = 1$WorkerSize = “Small“$ResourceGroup = "rg-app1-saad-dev"$Location = "SouthCentralUS"# 2. Get App Service Plan$AppServicePlan = Get-AzAppServicePlan | Where {$_.Name -eq $AppServicePlanName}if ($AppServicePlan -eq $null) { # 3. (a) If the App Service Plan does not exist# 3. (b) create a new App Service Plan$AppServicePlan = New-AzAppServicePlan -Name $AppServicePlanName -ResourceGroupName $ResourceGroup -Location $Location `-Tier $Tier -NumberofWorkers $NumberofWorkers -WorkerSize $WorkerSize}
Create an App Service and Associated Slots
We will create an App Service called “app1-saad-dev” with two slots Dev and QA. We have two options to create the App Service Slots (a) use one command for each slot, (b) use an array. I will show you how to use an array.
- Define variables with App Service parameters.
- Create App Service slots.
123456789# 1. Define variables with App Service parameters$WebAppName = "app1-saad-dev"$Slots = @("Dev","QA")# 2. Create App Service slots$WebApp = New-AzWebApp -Name $WebAppName -Location $Location -AppServicePlan $AppServicePlan.Name -ResourceGroupName $ResourceGroupForEach ($Slot in $Slots) {New-AzWebAppSlot -Name $WebAppName -Slot $Slot -ResourceGroupName $ResourceGroup}
Create an App Service Slots
In this example I’ll show you how to add slots to the App Service we just created, or to another App Service.
- You can use variables defined in the previous example for parameters values and add the new slot name, “Stage”; or
- You can enter parameters values in the command.
- You can also multiple slots using and an array with new slots names.
12345678910111213# 1 You can use variables defined in the previous example for parameters values and add the new slot name, "Stage"New-AzWebAppSlot -Name $WebAppName -Slot "Stage" -ResourceGroupName $ResourceGroup# Or# 2. You can enter parameters values in the commandNew-AzWebAppSlot -Name "app1-saad-dev" -Slot "Stage" -ResourceGroupName "rg-app1-saad-dev"# 3. You can also multiple slots using and an array with new slots names$NewSlots = @("Stage", "Master")foreach ($NewSlot in $NewSlots) {New-AzWebAppSlot -Name "app1-saad-dev" -Slot $NewSlot -ResourceGroupName "rg-app1-saad-dev"}
Clean up resources with one cmdlet command
With Remove-AzWebApp cmdlet, you can delete the newly created App Service, associated slots and also the App Service Plan, to prevent unexpected charges. The App Service Plan will not be delete if it hosts other App Services. You must delete all other App Services hosted by the App Service Plan before deleting it.
You can use -Force to suppress the confirmation dialog. Be extra careful when using the -Force parameters.
- Use Remove-AzWebApp to remove “app1-saad-dev” and associated slots. Use -DeleteAppServicePlan parameter to delete the App Service Plan.
- You can use -Force to suppress the confirmation dialog. Be extra careful when using the -Force parameters.
12# 1. Use Remove-AzWebApp to remove "app1-saad-dev" and associated slots. Use -DeleteAppServicePlan parameter to delete the App Service PlanRemove-AzWebApp -Name "app1-saad-dev" -ResourceGroupName "rg-app1-saad-dev"
Review the next sections to clean up resources one by one.
Remove an App Service Slots
- Use Remove-AzWebAppSlot to remove an App Service Slot.
- You can also remove multiple slots using and an array with slots names to be removed.
- Use -Force to suppress the confirmation dialog. Be extra careful when using the -Force parameters.
12345678# 1. Use Remove-AzWebAppSlot ro remove an App Service SlotRemove-AzWebAppSlot -Name "app1-saad-dev" -Slot "Master" -ResourceGroupName "rg-app1-saad-dev" -Force# 2. You can also remove multiple slots using and an array with slots names to be removed$DelSlots = @("Stage", "Master")foreach ($DelSlot in $DelSlots) {Remove-AzWebAppSlot -Name "app1-saad-dev" -Slot $DelSlot -ResourceGroupName "rg-app1-saad-dev" -Force}
Remove an App Service
- Use Remove-AzWebApp to remove “app1-saad-dev” App Service.
- Use -Force to suppress the confirmation dialog. Be extra careful when using the -Force parameters.
12# 1. Use Remove-AzWebApp to remove "app1-saad-dev" App ServiceRemove-AzWebApp -Name "app1-saad-dev" -ResourceGroupName "rg-app1-saad-dev" -Force
Remove an App Service Plan
You must delete all other App Services hosted by the App Service Plan before deleting it.
- Use Remove-AzAppServicePlan to remove “asp-app1-saad-dev” App Service Plan.
- Use -Force to suppress the confirmation dialog. Be extra careful when using the -Force parameters.
1Remove-AzAppServicePlan -Name "asp-app1-saad-dev" -ResourceGroupName "rg-app1-saad-dev" -Force
Conclusion
In this blog we explored how to create an App Service Plan, an App Service and associated Slots using PowerShell. We also explored deleting all resources we created with one command or one by one.
Did you find this blog easy to follow and helpful to you? Let me know in the comments below.
Disclaimer
Purpose of the code contained in blog is solely for learning and demo purposes. Author will not be held responsible for any failure or damages caused due to any other usage.
There's no comments