Azure App Services: Create an App Service and associated Slots using PowerShell

By Saad Khamis

October 19, 2021

16916 views

In this blog I will share one way to create an App Service PlanApp 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

  1. Install Azure PowerShell if you haven’t already. You can use Cloud Shell if you prefer to stay within Azure Portal.
  2. Install Azure Active Directory PowerShell module if you haven’t already. You can use Cloud Shell if you prefer to stay within Azure Portal.
  3. An existing Resource Group to host the new App Service.
  4. 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

  1. Connect-AzAccount Connect to Azure with an authenticated account for use with cmdlets from the Az PowerShell modules.
  2. Set-AzContext Sets the tenant, subscription, and environment for cmdlets to use in the current session.
  3. Get-AzAppServicePlan Gets an Azure App Service plan in the specified resource group.
  4. New-AzAppServicePlan Creates an Azure App Service plan in a given Geo location.
  5. New-AzWebApp Creates an Azure Web App.
  6. New-AzWebAppSlot Creates an Azure Web App slot.
  7. 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.
  8. Remove-AzWebApp Removes an Azure Web App.
  9. Remove-AzAppServicePlan Removes an Azure App Service plan.

Sign in to Azure

  1. Sign in to Azure. If you have multiple subscriptions or tenants, make sure to sign in to the correct subscription.
  2. You can use Set-AzContext to set the tenant, subscription, and environment for cmdlets to use in the current session.

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.

  1. Define variables with App Service Plan parameters.
  2. Get App Service Plan.
  3. (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.

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.

  1. Define variables with App Service parameters.
  2. Create App Service slots.

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.

  1. You can use variables defined in the previous example for parameters values and add the new slot name, “Stage”; or
  2. You can enter parameters values in the command.
  3. You can also multiple slots using and an array with new slots names.

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.

  1. Use Remove-AzWebApp to remove “app1-saad-dev” and associated slots. Use -DeleteAppServicePlan parameter to delete the App Service Plan.
  2. You can use -Force to suppress the confirmation dialog. Be extra careful when using the -Force parameters.

    Review the next sections to clean up resources one by one.

Remove an App Service Slots

  1. Use Remove-AzWebAppSlot to remove an App Service Slot.
  2. You can also remove multiple slots using and an array with slots names to be removed.
  3. Use -Force to suppress the confirmation dialog. Be extra careful when using the -Force parameters.

Remove an App Service

  1. Use Remove-AzWebApp to remove “app1-saad-dev” App Service.
  2. Use -Force to suppress the confirmation dialog. Be extra careful when using the -Force parameters.

Remove an App Service Plan

You must delete all other App Services hosted by the App Service Plan before deleting it.

  1. Use Remove-AzAppServicePlan to remove “asp-app1-saad-dev” App Service Plan.
  2. Use -Force to suppress the confirmation dialog. Be extra careful when using the -Force parameters.

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.

Comments

There's no comments

Leave a Reply

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

Related Posts

List all AWS Certificate Manager certificates

By Saad Khamis 14126 views July 31, 2024

Get all AWS EBS snapshots across all profiles and regions

By Saad Khamis 18484 views June 26, 2024