Is There a way to scaleout App Service plan using powershell

Vaishnav AV-FT 81 Reputation points
2023-06-07T06:55:56.84+00:00

I want to write a script which will switch instance scaling from Manual to custom rule based autoscale using Powershell. I am currently unable to find any document related to the same. Can anyone help me in scripting with powershell to toggle between manual scaling and custom rule based scaling in app service using powershell.

Thanks!

PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,707 questions
Azure ISV (Independent Software Vendors) and Startups
Azure ISV (Independent Software Vendors) and Startups
Azure: A cloud computing platform and infrastructure for building, deploying and managing applications and services through a worldwide network of Microsoft-managed datacenters.ISV (Independent Software Vendors) and Startups: A Microsoft program that helps customers adopt Microsoft Cloud solutions and drive user adoption.
97 questions
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
8,092 questions
{count} votes

4 answers

Sort by: Most helpful
  1. Konstantinos Passadis 19,276 Reputation points MVP
    2023-06-07T07:54:09.4466667+00:00

    Hello @Vaishnav AV-FT !

    I see you want to scale your App Service Plan for Web Apps

    Ther is this link

    https://zcusa.951200.xyz/en-us/azure/app-service/scripts/powershell-scale-manual

    And a sample Script :

    Install-Module -Name Az.Monitor -Force -AllowClobber

    Connect-AzAccount

    Set variables

    $resourceGroupName = "<your resource group name>"

    $appServicePlanName = "<your app service plan name>"

    $location = "<your location>"

    Get the App Service Plan resource

    $appServicePlan = Get-AzAppServicePlan -Name $appServicePlanName -ResourceGroupName $resourceGroupName

    Set up autoscale rules

    $scaleRule = New-AzAutoscaleRule -MetricName "CpuPercentage" -MetricResourceId $appServicePlan.Id -Operator "GreaterThanOrEqual" -MetricStatistic "Average" -Threshold 75 -TimeGrain 00:01:00 -ScaleActionCooldown 00:05:00 -ScaleActionDirection "Increase" -ScaleActionValue "1"

    Create an autoscale setting

    $autoscaleSetting = New-AzAutoscaleSetting -Location $location -Name ($appServicePlanName + "-AutoscaleSetting") -ResourceGroup $resourceGroupName -TargetResourceId $appServicePlan.Id -AutoscaleProfile (New-AzAutoscaleProfile -Name "AutoscaleProfile" -DefaultCapacity 1 -MaximumCapacity 3 -MinimumCapacity 1 -Rule $scaleRule)

    Apply the autoscale setting

    $autoscaleSetting | Set-AzAutoscaleSetting

    I hope this helps!

    Kindly mark the answer as Accepted and Upvote in case it helped!

    Regards


  2. brtrach-MSFT 16,926 Reputation points Microsoft Employee
    2023-06-08T18:36:01.04+00:00

    @Vaishnav AV-FT You can use PowerShell to toggle between manual scaling and custom rule-based scaling in App Service. Here are the steps to do so:

    1. First, you need to create an App Service Plan with per-app scaling by passing in the -PerSiteScaling $true parameter to the New-AzAppServicePlan cmdlet.
    New-AzAppServicePlan -ResourceGroupName <resource-group-name> -Name <app-service-plan-name
    -Location <location>
    -Tier Premium 
    -WorkerSize Small
    -NumberofWorkers 5 
    -PerSiteScaling $true
    
    1. Next, you can enable per-app scaling with an existing App Service Plan by passing in the -PerSiteScaling $true parameter to the Set-AzAppServicePlan cmdlet.
    Set-AzAppServicePlan -ResourceGroupName <resource-group-name>
       -Name <app-service-plan-name> -PerSiteScaling $true
    
    1. At the app level, you can configure the number of instances the app can use in the App Service plan1.
    newapp = Get-AzWebApp -ResourceGroupName <resource-group-name> -Name <app-name>
    
    $newapp.SiteConfig.NumberOfWorkers = 2
    
    Set-AzWebApp $newapp
    
    1. To switch from manual scaling to custom rule-based scaling, you can use the Set-AzWebApp cmdlet with the -AutoScaleMode parameter set to Scheduled or Metric.
    Set-AzWebApp -ResourceGroupName <resource-group-name> -Name <app-name>
       -AutoScaleMode Metric 
       -MinCapacity 2 
       -MaxCapacity 10 
       -DefaultCapacity 5
       -ScaleRules @()
    

    This will set the autoscale mode to metric-based scaling with a minimum capacity of 2 instances, a maximum capacity of 10 instances, and a default capacity of 5 instances. The -ScaleRules parameter is set to an empty array, which means there are no custom scaling rules defined.

    1. To switch back to manual scaling, you can use the Set-AzWebApp cmdlet with the -AutoScaleMode parameter set to None.
    Set-AzWebApp -ResourceGroupName <resource-group-name> -Name <app-name>
       -AutoScaleMode None
       -Capacity 2
    

    This will set the autoscale mode to manual scaling with a capacity of 2 instances.

    I hope this helps to answer your question. If so, we would appreciate it if you could provide a new positive survey as this will help to offset the previous survey rating, which greatly helps our team. If you have further concerns, please reply here so we can assist you further.


  3. Konstantinos Passadis 19,276 Reputation points MVP
    2023-06-08T21:02:43.16+00:00

    Hello @Vaishnav AV-FT !

    I have seen that the commands are not accepted .

    Theres is a whole new approach

    For the meantime have a look please :

    # Generates a Random Value

    $Random=(New-Guid).ToString().Substring(0,8)

    # Variables

    $ResourceGroupName="myResourceGroup$random"

    $AppName="AppServiceManualScale$random"

    $Location="WestUS"

    # Create a Resource Group

    New-AzResourceGroup -Name $ResourceGroupName -Location $Location

    # Create an App Service Plan

    New-AzAppservicePlan -Name AppServiceManualScalePlan -ResourceGroupName $ResourceGroupName -Location $Location -Tier Basic

    # Create a Web App in the App Service Plan

    New-AzWebApp -Name $AppName -ResourceGroupName $ResourceGroupName -Location $Location -AppServicePlan AppServiceManualScalePlan

    # Scale Web App to 2 Workers

    Set-AzAppServicePlan -NumberofWorkers 2 -Name AppServiceManualScalePlan -ResourceGroupName $ResourceGroupName

    https://zcusa.951200.xyz/en-us/azure/app-service/scripts/powershell-scale-manual

    I am also looking into different scenarios and got here :

    https://zcusa.951200.xyz/en-us/azure/azure-monitor/autoscale/autoscale-multiprofile?tabs=powershell

    owerShell can be used to create multiple profiles in your autoscale settings.
    
    See the PowerShell Az.Monitor Reference for the full set of autoscale PowerShell commands.
    
    The following steps show how to create an autoscale profile using PowerShell.
    
    Create rules using New-AzAutoscaleRule.
    Create profiles using New-AzAutoscaleProfile using the rules from the previous step.
    Use Add-AzAutoscaleSetting to apply the profiles to your autoscale setting.
    Add a recurring profile using PowerShell
    The example below shows how to create default profile and a recurring autoscale profile, recurring on Wednesdays and Fridays between 09:00 and 23:00. The default profile uses the CpuIn and CpuOut Rules. The recurring profile uses the BandwidthIn and BandwidthOut rules.
    
    Azure PowerShell
    
    Copy
    
    $ResourceGroupName="rg-vmss-001"
    $TargetResourceId="/subscriptions/abc123456-987-f6e5-d43c-9a8d8e7f6541/resourceGroups/rg-vmss-001/providers/Microsoft.Compute/virtualMachineScaleSets/vmss-001"
    $ScaleSettingName="vmss-autoscalesetting=001"
    
    $CpuOut=New-AzAutoscaleScaleRuleObject `
        -MetricTriggerMetricName "Percentage CPU" `
        -MetricTriggerMetricResourceUri "$TargetResourceId"  `
        -MetricTriggerTimeGrain ([System.TimeSpan]::New(0,1,0)) `
        -MetricTriggerStatistic "Average" `
        -MetricTriggerTimeWindow ([System.TimeSpan]::New(0,5,0)) `
        -MetricTriggerTimeAggregation "Average" `
        -MetricTriggerOperator "GreaterThan" `
        -MetricTriggerThreshold 50 `
        -MetricTriggerDividePerInstance $false `
        -ScaleActionDirection "Increase" `
        -ScaleActionType "ChangeCount" `
        -ScaleActionValue 1 `
        -ScaleActionCooldown ([System.TimeSpan]::New(0,5,0))
    
    
    $CpuIn=New-AzAutoscaleScaleRuleObject `
        -MetricTriggerMetricName "Percentage CPU" `
        -MetricTriggerMetricResourceUri "$TargetResourceId"  `
        -MetricTriggerTimeGrain ([System.TimeSpan]::New(0,1,0)) `
        -MetricTriggerStatistic "Average" `
        -MetricTriggerTimeWindow ([System.TimeSpan]::New(0,5,0)) `
        -MetricTriggerTimeAggregation "Average" `
        -MetricTriggerOperator "LessThan" `
        -MetricTriggerThreshold 30 `
        -MetricTriggerDividePerInstance $false `
        -ScaleActionDirection "Decrease" `
        -ScaleActionType "ChangeCount" `
        -ScaleActionValue 1 `
        -ScaleActionCooldown ([System.TimeSpan]::New(0,5,0))
    
    
    $defaultProfile=New-AzAutoscaleProfileObject `
        -Name "Default" `
        -CapacityDefault 1 `
        -CapacityMaximum 5 `
        -CapacityMinimum 1 `
        -Rule $CpuOut, $CpuIn
    
    
    $BandwidthIn=New-AzAutoscaleScaleRuleObject `
        -MetricTriggerMetricName "VM Cached Bandwidth Consumed Percentage" `
        -MetricTriggerMetricResourceUri "$TargetResourceId"  `
        -MetricTriggerTimeGrain ([System.TimeSpan]::New(0,1,0)) `
        -MetricTriggerStatistic "Average" `
        -MetricTriggerTimeWindow ([System.TimeSpan]::New(0,5,0)) `
        -MetricTriggerTimeAggregation "Average" `
        -MetricTriggerOperator "LessThan" `
        -MetricTriggerThreshold 30 `
        -MetricTriggerDividePerInstance $false `
        -ScaleActionDirection "Decrease" `
        -ScaleActionType "ChangeCount" `
        -ScaleActionValue 1 `
        -ScaleActionCooldown ([System.TimeSpan]::New(0,5,0))
    
    
    $BandwidthOut=New-AzAutoscaleScaleRuleObject `
        -MetricTriggerMetricName "VM Cached Bandwidth Consumed Percentage" `
        -MetricTriggerMetricResourceUri "$TargetResourceId"  `
        -MetricTriggerTimeGrain ([System.TimeSpan]::New(0,1,0)) `
        -MetricTriggerStatistic "Average" `
        -MetricTriggerTimeWindow ([System.TimeSpan]::New(0,5,0)) `
        -MetricTriggerTimeAggregation "Average" `
        -MetricTriggerOperator "GreaterThan" `
        -MetricTriggerThreshold 60 `
        -MetricTriggerDividePerInstance $false `
        -ScaleActionDirection "Increase" `
        -ScaleActionType "ChangeCount" `
        -ScaleActionValue 1 `
        -ScaleActionCooldown ([System.TimeSpan]::New(0,5,0))
    
    $RecurringProfile=New-AzAutoscaleProfileObject `
        -Name "Wednesdays and Fridays" `
        -CapacityDefault 1 `
        -CapacityMaximum 10 `
        -CapacityMinimum 1 `
        -RecurrenceFrequency week `
        -ScheduleDay "Wednesday","Friday" `
        -ScheduleHour 09 `
        -ScheduleMinute 00  `
        -ScheduleTimeZone "Pacific Standard Time" `
        -Rule $BandwidthIn, $BandwidthOut
    
    
    
    $DefaultProfile2=New-AzAutoscaleProfileObject `
        -Name "Back to default after Wednesday and Friday" `
        -CapacityDefault 1 `
        -CapacityMaximum 5 `
        -CapacityMinimum 1 `
        -RecurrenceFrequency week `
        -ScheduleDay "Wednesday","Friday" `
        -ScheduleHour 23 `
        -ScheduleMinute 00 `
        -ScheduleTimeZone "Pacific Standard Time" `
        -Rule $CpuOut, $CpuIn
    
    
    Update-AzAutoscaleSetting  `
    -name $ScaleSettingName `
    -ResourceGroup $ResourceGroupName `
    -Enabled $true `
    -TargetResourceUri $TargetResourceId `
    -Profile $DefaultProfile, $RecurringProfile, $DefaultProfile2
    
     Note
    
    You can't specify an end date for recurring profiles in PowerShell. To end a recurring profile, create a copy of default profile with the same recurrence parameters as the recurring profile. Set the start time to be the time you want the recurring profile to end. Each recurring profile requires its own copy of the default profile to specify an end time.
    
    Updating the default profile when you have recurring profiles
    If you have multiple recurring profiles and want to change your default profile, the change must be made to each default profile corresponding to a recurring profile.
    
    For example, if you have two recurring profiles called SundayProfile and ThursdayProfile, you need two New-AzAutoscaleProfile commands to change to the default profile.
    
    Azure PowerShell
    
    Copy
    
    
    $DefaultProfileSundayProfile = New-AzAutoscaleProfile -DefaultCapacity "1" -MaximumCapacity "10" -MinimumCapacity "1" -Rule $CpuOut,$CpuIn -Name "Defalut for Sunday" -RecurrenceFrequency week  -ScheduleDay "Sunday" -ScheduleHour 19 -ScheduleMinute 00   -ScheduleTimeZone "Pacific Standard Time"`
    
    
    $DefaultProfileThursdayProfile = New-AzAutoscaleProfile -DefaultCapacity "1" -MaximumCapacity "10" -MinimumCapacity "1" -Rule $CpuOut,$CpuIn -Name "Default for Thursday" -RecurrenceFrequency week -ScheduleDay "Thursday" -ScheduleHour 19 -ScheduleMinute 00 -ScheduleTimeZone "Pacific Standard Time"`
    
    
    
    I hope this helps!
    Kindly mark the answer as Accepted and Upvote in case it helped!
    Regards
    
    0 comments No comments

  4. Konstantinos Passadis 19,276 Reputation points MVP
    2023-06-08T21:18:47.88+00:00

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.