Open
Description
Description
I experience that Invoke-AzCostManagementQuery
often does not return results on first try. Seems the Rest API endpoint it uses is unstable. If I retry once or sometimes even twice, the success rate becomes better.
Can Az do something about this? Add some retry logic or something?
Issue script & Debug output
$null = Add-Member -InputObject $Subscription -Force -NotePropertyName 'ConsumptionTwoWeeksAgo' -NotePropertyValue (
Invoke-AzCostManagementQuery -WhatIf:$false -DatasetAggregation @{
'totalCost' = @{
'name' = [string] 'Cost'
'function' = [string] 'SUM'
}
} -Type 'ActualCost' -Timeframe 'Custom' `
-Scope ('/subscriptions/{0}' -f $Subscription.'Id') `
-TimePeriodFrom $StartOfTwoWeeksAgo.ToString('o') `
-TimePeriodTo $EndOfTwoWeeksAgo.ToString('o') 6>$null
)
Environment data
PowerShell v7.5.1 x64
Windows 11 24H2 x64
Module versions
Az.Accounts 4.2.0
Az.CostManagement 0.4.0
Error output
No error
Metadata
Metadata
Assignees
Labels
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
o-l-a-v commentedon May 12, 2025
Seems I get throttled. Az should handle that, right? Seems it does not in this case.
[-]`Invoke-AzCostManagementQuery` often does not return results on first try[/-][+]`Invoke-AzCostManagementQuery` often does not return results on first try. Throttling?[/+]o-l-a-v commentedon May 12, 2025
Made a workaround using the API directly with
Invoke-AzRestMethod
, which returns HTTP status code too.If header
x-ms-ratelimit-microsoft.costmanagement-entity-retry-after
is present I wait that amount. Else I just wait 5 seconds to try again.Invoke-AzRestMethod
handle throttling 429 #27752isra-fel commentedon May 16, 2025
cc @dolauli
dolauli commentedon May 16, 2025
We will retry only when
Retry-After
instead ofx-ms-ratelimit-microsoft.costmanagement-entity-retry-after
is set in the 429 response header.o-l-a-v commentedon May 16, 2025
So the resource provider does not follow Azure RM API guidelines?
Maybe throw an error or warning on 429 without the expected header? Instead of just returning nothing.