Create Azure App Service via PowerShell 7

Hello everyone, in this article we are about to learn how to create the Azure App Service using Powershell.

To start with the article there are some prerequisites for the understanding.

  1. Basics of Azure Knowledge (What are the App Service, Resource Group, App Service plans, etc.)
  2. Installation of PowerShell
  3. Follow this link for the PowerShell installation if you haven't done (PowerShell installation)
  4. Require at least one of the subscription access on Azure. (If not then please check with your IT team)
  5. Basics of Powershell language.

What is Powershell?

PowerShell is a task automation and configuration management framework developed by Microsoft that features a command-line shell and scripting language. It’s designed to help IT professionals manage systems, automate repetitive tasks, and control configurations on Windows, macOS, and Linux. PowerShell scripts and cmdlets simplify complex workflows by integrating commands and allowing seamless interaction with different system components.

Why do we need to use PowerShell to create the Azure resource?

Using PowerShell to create Azure resources allows for efficient automation, consistent deployments, and script-based management. It simplifies repetitive tasks, enables version control for infrastructure configurations, and integrates easily with Azure CLI and other Azure services. PowerShell also provides access to Azure-specific cmdlets, making it easier to manage and scale resources across different environments in a secure, scriptable way.

A practical example of how PowerShell can benefit an organization

Imagine a scenario where a client requests the creation of an Azure App Service. To fulfill this request, the team needs to create or reuse a resource group, set up app service plans, and apply any necessary virtual networks (VNets).

If the DevOps team has prepared a PowerShell script or template to handle these tasks, they can accomplish this quickly and with minimal effort. This approach significantly reduces the likelihood of errors, such as selecting the wrong app service plans or VNets, ensuring a smoother and more efficient deployment process.

Now, As per the blog title, Let's start with how can we create the app service via PowerShell.

Note. I'm using Powershell version 7 and the app service plan will be a Free tier.

Step 1. Run the Connect-AzAccount command on PowerShell and check the access of your subscription, it will open one popup, and then select your account from it.

Retrieving subscriptions for the selection.

Subscription name Tenant

-----------------------

MXXXXXXXX Default Directory

Here it will show the list of subscriptions with the respective tenant IDs, in my case, I've only one subscription and I'm the owner of it so it will show the Default Directory, but if you have one or more access then it will show the tenant Id

Step 2. Create the ResourceGroup, App Service Plan, and other basic requirement.

if we are not creating it then we can also create it via script will show it in the next step.

Step 3. Script for the App Service Creation.

# Variables
$ResourceGroupName = "ps-learn"
$Location = "Central India" # e.g., "EastUS"
$AppServicePlanName = "ps-asp"
$SkuName = "Free" # Basic tier, can be B1, S1, etc. depending on your needs.
$AppServiceName = "ps-as"

# Create Resource Group (if it doesn't exist)
if (-not (Get-AzResourceGroup -Name $ResourceGroupName -ErrorAction SilentlyContinue)) {
    New-AzResourceGroup -Name $ResourceGroupName -Location $Location
}

# Create an App Service Plan (if it doesn't exist)
if (-not (Get-AzAppServicePlan -Name $AppServicePlanName -ResourceGroupName $ResourceGroupName -ErrorAction SilentlyContinue)) {
    New-AzAppServicePlan -Name $AppServicePlanName -Location $Location -ResourceGroupName $ResourceGroupName -Sku $SkuName -NumberofWorkers 1
}

# Create the App Service
New-AzWebApp -Name $AppServiceName `
             -Location $Location `
             -AppServicePlan $AppServicePlanName `
             -ResourceGroupName $ResourceGroupName

Write-Output "Created App Service: $AppServiceName"

# Retrieve the App Service details
$webApp = Get-AzWebApp -Name $AppServiceName -ResourceGroupName $ResourceGroupName

# Output the created App Service URL
Write-Output "Created App Service: $AppServiceName"
Write-Output "URL: https://$($webApp.DefaultHostName)"

Now let's understand the script.

Explanation of Parameters

  1. $ResourceGroupName: The name of the resource group where all App Services will be created.
  2. $Location: The region where resources are created.
  3. $AppServicePlanName: The App Service Plan to host your App Services
  4. $SkuName: Defines the pricing tier, where B1 is Basic. Change this if needed.

The New-AzWebApp cmdlet in Azure PowerShell is used to create a new Azure App Service Web App. This cmdlet allows you to deploy a web application on Azure's App Service platform, which is a fully managed platform for building, deploying, and scaling web apps.

Key Features of New-AzWebApp

  1. Create Web Apps: You can create a web application in Azure App Service using this cmdlet.
  2. Flexible Configuration: You can specify various settings such as the app name, resource group, service plan, runtime stack, and more.
  3. Deployment Options: After creating the web app, you can deploy your application code to it using various methods, such as FTP, Git, or Azure DevOps.

Basic Syntax

New-AzWebApp `
    -Name <String> `
    -ResourceGroupName <String> `
    -AppServicePlan <String> `
    -Location <String> `
    [-OtherParameters]

Common Parameters

  • Name: The name of the web app. This must be unique across Azure.
  • ResourceGroupName: The name of the resource group in which the web app will be created.
  • AppServicePlan: The name of the app service plan associated with the web app.
  • Location: The Azure region where the web app will be hosted.
  • AppSettings: A hashtable of application settings for the web app (key-value pairs).
  • RuntimeStack: The runtime stack for the web app, such as .NET, Node, Java, etc.

Step 4. How to Run the script.

  1. Save the file name with an extension .ps1 (e.g. CreateAppService.ps1)
  2. Open the PowerShell from your machine go to the path where you are storing this file enter your file name and press enter.

Once you execute the script it will take some time to create the App Service on Azure.

Step 5. Site URL.

With the Get-AzWebApp cmdlet, we are extracting the site URL as per below that you can find at last of your PowerShell terminal.

URL: https://ps-as.azurewebsites.net

Azure

Thanks for reading the article.

Happy Coding!


Similar Articles