Skip to content

Automation of App Service Plan

Azure blog - Automation of App Service Plan 2022

Again about how to save money on Azure. Developers used their applications for testing on a higher plan, and after the tests were finished, they manually switched the App Service Plan to a smaller size “Instance Size = Small” and the free layer “Pricing Tier = Free”. That’s cool, but sometimes they forgot about it. It consumed money needlessly until they remembered it.

App Service Plan- configuration

So a script was created that was run every day, automatically after finishing work. He checked if the apps were on the right plan, if they weren’t switched. It is important to note that the App Service Plan can be changed to a lower plan only if you turn off the “alwaysOn” option, which starts automatically with a higher plan.

$ResourceGroupName = "Resource_group_Name"
$AppServicePlan = "AppServicePlan_Name"
$Tier = "Free"
$Size = "Small"


$PropertiesObject = @{alwaysOn = $false;}
$asp=Get-AzureRmAppServicePlan -ResourceGroupName $ResourceGroupName -Name $AppServicePlan

$apps=Get-AzureRmWebApp -AppServicePlan $asp

Foreach($app in $apps)
{
 $websiteName = $app.Name
 Set-AzureRmResource -PropertyObject $PropertiesObject -ResourceGroupName $app.ResourceGroup -ResourceType Microsoft.Web/sites/config -ResourceName $websiteName/web -Force
}

Set-AzureRmAppServicePlan -ResourceGroupName $ResourceGroupName -Name $AppServicePlan  -Tier "$Tier" -WorkerSize "$Size"
The script can be run in any way, I prefer via 'Automation Account'.

If you don’t know how to create an ‘automation account’, see this article, everything is explained step by step.

If anyone would like to learn more about App Service plan, as usual, please visit the Microsoft technical documentation: https://azure.microsoft.com/pl-pl/pricing/details/app-service/plans/

You can find more articles about Microsoft Azure on my blog in the Azure category.

If you would like to know more ways to save money on Azure, I invite you to my other posts, for example: