Auto-scale for Azure Files Storage Premium

Automatically increase your Azure Files Storage share quota using Function App. This script and similar instructions are provided by Microsoft but I have included some addition information to make it easier for anyone to configure this in their environment. I typically use this script and similar function app for FSLogix Profile storage to automatically increase share quota when we add more users to a AVD environment.

There is also a script in the GitHub repo that will allow you to define how many requests the workload can tolerate being throttled before auto-scaling if that fits your needs better. Same steps apply just use the throttled script instead.

First you need to create a Function App. Create a function in Azure that runs on a schedule | Microsoft Learn

Login to Azure Portal and go to Function App.

Click Create.

Click on Create new or use existing Resource group selector.

Specify a unique name for your Function App. Then select PowerShell Core for Runtime stack, version 7.2, and a Region. Operating System is Windows, and Consumption Plan. Click Next to continue.

Create a new Storage Account or you can select an existing one from the drop down.

Networking: Enable Public Access Off.

Monitoring: Optional. Yes, if you want to be able to see insights.

Add Tags if needed. Then Review and Create.

After the Function App is created you still need to create the Function, add a schedule and the script.

Open your new Function App and click create.

Select Timer trigger, then specify a name for the New Function and you can set the schedule now or leave it default and adjust later. Schedule information can be found here. https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-timer?tabs=python-v2%2Cin-process%2Cnodejs-v4&pivots=programming-language-csharp#ncrontab-expressions Default is every 5 minutes.

Click Code + Test.

Copy and paste the script, then adjust the variables for your Azure subscription, Resource Group, Storage Account Name and File Share or multiple shares. Shares are separated by comma. Click save when finished.

The script checks if less than 20% of provisioned capacity is remaining and increase provisioned capacity. You can adjust the thresholds as needed.

Adjust Timer/Schedule. Click on Integration.

Specify a different schedule since the default is every 5 minutes. Typically, once a day should be good enough for AVD FSLogix storage. Other use cases you might need a different schedule.

Next you need to grant permissions for the Function App to increase quota automatically.

Click on Identity.

Under Status, System Assigned, click on and then click Save. Then click Azure role assignments.

Click Add Role Assignment (Preview).

Change scope to Storage then choose the storage account and select contributor role. Click save. You might have to wait a couple minutes for the roles to show up or try refreshing.

Repeat steps if you are going to use this script for other storage accounts.

After your permissions are set you are all finished. You can test it or just wait for the schedule to run the function.

#Script created by someone at Microsoft. https://github.com/Azure-Samples/azure-files-samples/tree/master/autogrow-PFS-quota 
# Input bindings are passed in via param block.
param($Timer)

# Get the current universal time in the default string format.
$currentUTCtime = (Get-Date).ToUniversalTime()

# The 'IsPastDue' property is 'true' when the current function invocation is later than scheduled.
if ($Timer.IsPastDue) {
    Write-Host "PowerShell timer is running late!"
}

#Variable Definitions
$subscription_id = subscription ID
$resource_group = resource group name
$storage_account_name = storage account name
$file_share_names = @("profiles01", "profiles02", "profiles03", "profiles04")

#Connect to Azure and Import Az Module
Connect-AzAccount -Identity
Import-Module -Name Az.Accounts
Import-Module -Name Az.Storage
Set-AzContext -SubscriptionId $subscription_id

foreach ($file_share_name in $file_share_names) {
    # Get file share
    $StorageContext = New-AzStorageContext -StorageAccountName $storage_account_name -Anonymous
    $PFS = Get-AzRmStorageShare -ResourceGroupName $resource_group -StorageAccountName $storage_account_name -Name $file_share_name -GetShareUsage

    # Get provisioned capacity and used capacity
    $ProvisionedCapacity = $PFS.QuotaGiB
    $UsedCapacity = $PFS.ShareUsageBytes
    Write-Host "Provisioned Capacity for ${file_share_name}:" $ProvisionedCapacity
    Write-Host "Share Usage Bytes for ${file_share_name}:" $UsedCapacity

    # get storage account
    $StorageAccount = Get-AzStorageAccount -ResourceGroupName $resource_group -AccountName $storage_account_name

    # if less than 20% of provisioned capacity is remaining, increase provisioned capacity by 20%
    if (($ProvisionedCapacity - ($UsedCapacity / ([Math]::Pow(2,30)))) -lt ($ProvisionedCapacity*0.2)) {
         $Quota = $ProvisionedCapacity*1.2
         Update-AzRmStorageShare -StorageAccount $StorageAccount -Name $file_share_name -QuotaGiB $Quota
         $ProvisionedCapacity = $Quota
    }
    Write-Host "Provisioned Capacity for ${file_share_name}:" $ProvisionedCapacity

}

# Write an information log with the current time.
Write-Host "PowerShell timer trigger function ran! TIME:" $currentUTCtime

azure-files-samples/autogrow-PFS-quota at master · Azure-Samples/azure-files-samples (github.com)

Share or Save this:
Share