Get Azure Function AzureWebJobsDashboard Storage Usage Script

Last week on the Twitter while checking I noticed a thread related to the Azure web job storage size. The issue is that the Azure storage used has grown tremendously, close to 2 TB. By default, this storage account is supposed to be used for storing checkpoints, trigger and metadata related to the functions, so it shouldn’t grow to that extent.

The author did some digging around this and found a great article on why the size was grown huge. I’m not going to go through this again as the article explains it in depth, and it’s out of scope for this article.

In Azure Functions Runtime V4 the property AzureWebJobsDashboard is deprecated, however in the previous versions it was supported for backlog compatibility.

Later, I realised that we too have hundreds of Azure functions in our environment, and it will be very hard for us to go and check this across all the Azure functions manually. This is what taught me to write a script which will fetch all the Azure Functions and the storage size associated with it from a subscription. This will help to understand whether AzureWebJobsDashboard property is configured and all the size of the storage account so that the priority can be given to the one which took more space.

I’m sharing the script with all my readers so that you can make use of it.

$subscriptionName = "<SubscriptionName>" #Provide the subscription name

##Install and Import required Modules. Run in administrator mode to install it
##Uninstall AzureRM module , Az and AzureRm cannot be installed together. Run Uninstall-AzureRm command

If (!( Get-Module | Where-Object { $_.Name -eq "Az" })) {
    Install-Module "Az" -Force
}
Import-Module "Az" -Force

# #Connect to Azure account
Connect-AzAccount -Subscription $subscriptionName 

#Gather Data
$output = New-Object System.Collections.Generic.List[Object]
#Uncomment below line and comment the next to neqxt line if you want to filter by resoruce group.
#Get-AzFunctionapp -ResourceGroupName "<<ResourceGroupname>>" | ForEach-Object {
Get-AzFunctionapp | ForEach-Object {
    $awjd = (Get-AzFunctionAppSetting -Name $_.Name -ResourceGroupName $_.ResourceGroupName)["AzureWebJobsDashboard"]
    if ($awjd) {
        $webJobsDashboardConfigured = $true
    }
    else {
        $awjd = (Get-AzFunctionAppSetting -Name $_.Name -ResourceGroupName $_.ResourceGroupName)["AzureWebJobsStorage"]
        $webJobsDashboardConfigured = $false
    }
    $storageAccountName = (($awjd -split ";")[1] -split "=")[1]
    $getStorageAccountDetails = Get-AzStorageAccount -ResourceGroupName $_.ResourceGroupName -AccountName $storageAccountName
    $tableCount = ($getStorageAccountDetails | Get-AzStorageTable).count
    $saId = ($getStorageAccountDetails).id
    $storageAccountAvgSizeMB = (Get-AzMetric -ResourceId $saId -MetricName "UsedCapacity" -AggregationType Average).Data.Average / 1024 / 1024
    $tableServiceAvgSizeMB = (Get-AzMetric -ResourceId "$saId/tableServices/default" -MetricName "TableCapacity" -AggregationType Average).Data.Average / 1024 / 1024
    $outObject = [PSCustomObject]@{
        FunctionAppName                 = $_.Name
        ResourceGroupName               = $_.ResourceGroupName
        AzureWebJobsDashboardConfigured = $webJobsDashboardConfigured
        StorageAccountName              = $storageAccountName
        StorageAccountAvgSizeinMB       = $storageAccountAvgSizeMB.tostring("#.##") 
        TableServiceAvgSizeinMB         = $tableServiceAvgSizeMB.tostring("#.##") 
        TotalTables                     = $tableCount
    }    
    $output.Add($outObject)
} 
Write-Output $output
$output | Export-Csv -Path "./export.csv"

The above script will connect to a subscription and will go through all the functions in the subscription and will grab the below details in a csv file and also prints in the console. This csv file will be exported to the same path. In case if you need to collect the details for only one resource group, you can comment the line mentioned in the script. Of course, you can change the script as per your need.

Output

Az Functions & Storage Account Console Output
Attribute NameDescription
FunctionNameName of the Azure function
ResourceGroupNameResource group in which the Azure function and storage account were created
AzureWebJobsDashboardConfiguredWhen enabled returns true otherwise returnes false
StorageAccountNameStorage account name used for web jobs dashboard
StorageAccountAvgSizeinMBAverage storage account size including all the services. Value is in MB
TableServiceAvgSizeinMBAverage space used by table service only within the storage account. Value is in MB
TotalTablesTotal number of tables created in the storage account
Output data definition

Posted

in

, , , ,

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *