How to configure auto scaling for an Azure App Service with PowerShell

In some cases, there are features which exist on the backend REST APIs which are not yet implemented in the portal.  For example, I support the Azure German Cloud as well, and at the moment you cannot configure Auto Scaling from the portal, but the PowerShell cmdlets which call the REST APIs to configure it are exposed.  Perhaps there are some parameters for other Azure SDK cmdlets which are not configurable from the portal.  I am not aware of them all, but hey, if you are interested, it is a good learning opportunity.

What I see by default when I want to configure Auto Scale or scaling in general is the following, Figure 1.

image

Figure 1, configuring auto scaling or scaling manually Azure App Service Web App

To create the Auto Scale rule I first logged in and then set the focus to a specific subscription id, using the following cmdlets as shown in Figure 2.

Login-AzureRmAccount
Set-AzureRmContext -SubscriptionId “25******”

image

Figure 2, login to Azure using PowerShell cmdlets

Then I used these 3 PowerShell cmdlets:

Then I scaled using these cmdlets, parameters and values.

$AutoScaleRule = New-AzureRmAutoscaleRule -MetricName "CpuTime" -MetricResourceId
    "/subscriptions/<subscriptionId>/resourceGroups/<resourceGroup>/providers/Microsoft.Web/sites/
    <siteName>" -MetricStatistic Average -Operator GreaterThan -Threshold 70 -TimeGrain 00:05:00
    -ScaleActionDirection Increase -ScaleActionValue "1" -ScaleActionCooldown 00:10:00


$AutoScaleProfile = New-AzureRmAutoscaleProfile -DefaultCapacity "1" -MaximumCapacity "10"
    -MinimumCapacity "1" -StartTimeWindow 2017-07-26T00:00:00
    -EndTimeWindow 2017-07-27T23:59:00 -TimeWindowTimeZone UTC
    -Rules $AutoScaleRule -Name "Scale when CPU is HIGH"
  
Add-AzureRmAutoscaleSetting -Location "<ex: South Central US>" -Name "Auto Scale Setting"
    -ResourceGroup "<resourceGroup>" -TargetResourceId "/subscriptions/<subscriptionId>/
    resourceGroups/<resourceGroup>/providers/microsoft.web/serverFarms/<AppServicePlanName>"
    -AutoscaleProfiles $AutoScaleProfile

And when I looked at the Rule/Profile/Setting in the portal for the given App Service, I see it as per Figure 3.

image

Figure 3, Auto scaling with PowerShell cmdlets

Parameter descriptions for New-AzureRmAutoscaleRule

-MetricName “CpuTime” –> The metric to create the rule for, ex: “CpuTime”, “MemoryPercentage”, “DiskQueueLength”, “HttpQueueLength”, “BytesReceived”, “BytesOut”

-MetricResourceId “/subscriptions/ <subscriptionId> /resourceGroups/ <resourceGroup> /providers/Microsoft.Web/sites/ <siteName> –> the resource onto which you want to apply the rule.

-MetricStatistic Average –> This is the aggregation method used to aggregate the sampled metrics.  TimeAggregation = “Average” will aggregate the sampled metrics by taking the average.  ex: Average, Min, Max, Sum

-Operator GreaterThan –> Compare to the value set in the Threshold.  ex: Equals, NotEquals, GreaterThan, GreaterThanOrEqual, LessThan, LessThanOrEqual

-Threshold 70 –> The value of the metric which triggers the auto scale.  ex: Average, CPU Percentage, GreaterThan.

-TimeGrain 00:10:00 –> This is amount of time to look back for metrics.  ex: 10 minutes means that every time auto scale runs, it will query metrics for the last 10 minutes.  This allows your metrics to stabilize and avoids reacting to transient spikes.

-ScaleActionDirection Increase –> Specifies the scale action direction.  For example, None, Increase or Decrease

-ScaleActionValue “1” –> Specifies the number of instances to change based on the value set for ScaleActionDirection

-ScaleActionCooldown 00:10:00 –> The amount of time to wait after a scaling operation before scaling again.  For example, if cool down is 10 minutes and a scale operation just occurred, Auto scale will not attempt to scale again until after 10 minutes. This is to allow the metrics to stabilize first.

image

$AutoScaleRule = New-AzureRmAutoscaleRule -MetricName "CpuTime" -MetricResourceId "/subscriptions/ad3a536b-eb52-41aa-938a-68b944861b0e/resourceGroups/GERMANCLOUD-FR1/providers/Microsoft.Web/sites/blackforest" -MetricStatistic Average -Operator GreaterThan -Threshold 70 -TimeGrain 00:05:00 -ScaleActionDirection Increase -ScaleActionValue "1" -ScaleActionCooldown 00:10:00


$AutoScaleProfile = New-AzureRmAutoscaleProfile -DefaultCapacity "1" -MaximumCapacity "10" -MinimumCapacity "1" -StartTimeWindow 2017-07-26T00:00:00 -EndTimeWindow 2017-07-27T23:59:00 -TimeWindowTimeZone UTC -Rules $AutoScaleRule -Name "Scale when CPU is HIGH"
Add-AzureRmAutoscaleSetting -Location "Germany Central" -Name "Auto Scale Setting" -ResourceGroup "GERMANCLOUD-FR1" -TargetResourceId "/subscriptions/ad3a536b-eb52-41aa-938a-68b944861b0e/resourceGroups/GERMANCLOUD-FR1/providers/microsoft.web/serverFarms/FR1-ASP-001" -AutoscaleProfiles $AutoScaleProfile