Azure Function App adventures: setting the ‘scale out limit’ with PowerShell
I use Azure Function Apps on a daily basis and was recently working on some scripts to handle configuration and security.
I’ll talk about the security part in another post, but wanted to share this little tip on how to use PowerShell to set the scale out limit for an Azure Function App that is on a consumption plan.
- An Azure Function App that is on a consumption plan. If you’re not familiar with how to set this up, look here.
- PowerShell, with the Az module installed. See here.
What is an Azure Function App?
Azure Functions is a serverless compute service that enables you to run a piece of code on-demand, in response to an HTTP request, a timer, or an event from another (Azure) service.
One Function App can contain multiple functions and can spin-up more resources or instances when needed, and output can be directed to different kinds of data stores and other Azure services, like Cosmos DB and Event Hub.
I use them a lot because they are extremely scalable, and very easy to set up.
I talk more about Azure Functions (with Visual Studio) in this post.
What is the Consumption Plan?
You can choose to run a Function App on a Consumption or an App Service plan (and even on a powerful Premium plan).
While the App Service plan is good for long running operations and predictive performance and pricing, with the Consumption plan, your functions will automatically scale out to multiple instances when needed, and you only pay for what you use. You can set the bounds that the app can scale within, and that’s what we’re looking at in this article.
Setting the scale out limit in the Azure Portal
To set the scale out limit manually, just go to your Function App in the Azure portal, and in the left panel find Scale out under Settings:
Initially your scale out settings will probably look like this, set to the maximum limit of 200 instances:
This means that under heavy load, Azure will spin up to 200 instances of your Azure Function app.
To prevent unexpected costs, you might want to set a lower limit. Of course, you can do this in the Azure portal, but when you’re managing a large number of Function Apps that’s not very practical.
Setting the scale out limit with PowerShell
Let’s see how we can adjust the limit by using PowerShell.
Before we can run the script, we need some settings:
- The Azure subscription ID that the Function App is in
- The name of the resource group that the Function App is in
- The name of the Function App
You can find these on the main page for your Function App in the Azure portal, as shown here:
Below is the PowerShell script (which you can download here). Notice the settings at the top, under ### INPUT ###. They include the values shown above, and a variable called $functionAppScaleLimit, which holds the desired scale limit instance count:
# This script shows you how to set the 'App Scale Out' limit on an Azure Function App, # as described in this blog post: https://jurgenonazure.com/2021/11/azure-function-app-adventures-setting-the-scale-out-limit-with-powershell # NOTE: if you haven't installed the Az PowerShell module yet, # look here: https://docs.microsoft.com/ko-kr/powershell/azure/install-az-ps ### INPUT ### $subscriptionId="<obfuscated>" $resourceGroupName = "app-blog-rg" $functionAppName = "app-blog-func-app" $functionAppScaleLimit=10 ### LOGIC ### # Login to Azure Connect-AzAccount -SubscriptionId $subscriptionId Write-Output "" Write-Output "SETTING THE SCALE LIMIT on $($functionAppName)" # Get function app with properties $resource = Get-AzResource ` -ResourceType Microsoft.Web/sites ` -ResourceGroupName $resourceGroupName ` -ResourceName "$($functionAppName)/config/web" # Set scale limit on properties and apply $resource.Properties.functionAppScaleLimit = $functionAppScaleLimit $resource | Set-AzResource -Force Write-Output "" Write-Output "DONE!"
The script is fairly straightforward. It’s just a matter of getting the Function App resource, with its properties, and then adjusting the property for the scale out limit, called functionAppScaleLimit.
In the example above we’re setting the limit to 10.
Next, we push the changes to Azure with Set-AzResource.
And that’s it. After running this, and refreshing the settings in the Azure portal, it should look like this:
As a final tip, let me note that it’s also possible to disable the scale out limit. Just set the $functionAppScaleLimit variable to 0 (zero) and run the script again. The Azure portal should then indicate that the limit is disabled:
Thanks for reading!