AKS Cost Analysis - API access for users
Today it is possible to obtain data from the AKS Cost Analysis add-on only through the Azure Portal or downloaded CSV files.
Using az CLI credentials to create an API call throws an error: {"error":{"code":"401","message":"Request blocked. Your request to this resource has been denied. Please access this resource from the Cost Analysis blade on the Azure Portal."}}
To automate add-on usage and obtain data from tables, it should be possible to provide users with access to use API.
We also need access to the API.
I asked this as well to MSFT Support, here's the response we got:
I understand that you are encountering an error when attempting to query the Azure AKS Cost API, specifically the following message:
{'error':{'code':'401','message':'Request blocked. Your request to this resource has been denied. Please access this resource from the Cost Analysis blade on the Azure Portal.'}}
This issue arises because direct API access to cost data for AKS is currently restricted, and Azure recommends viewing cost data via the Cost Analysis blade in the Azure Portal. Unfortunately, querying cost data at the namespace level via API is not natively supported at this time, which I understand is important for your tracking and reporting needs.
As per if there's an ETA for release:
At this time, there is no confirmed ETA from Microsoft regarding when native support for querying AKS costs at the namespace level via API will be available. However, the Azure Product Group is actively working on improving cost management capabilities for AKS, including more granular reporting.
I have tested with api-version=2023-04-01-preview , it works good with my access token but i get below error while using service principal which has same rights as me.
{
"error": {
"code": "401",
"message": "Request blocked. Your request to this resource has been denied. Please access this resource from the Cost Analysis blade on the Azure Portal."
}
}
I have openend a ticket in ms community : https://learn.microsoft.com/en-us/answers/questions/2099742/not-able-to-get-cluster-and-namespace-cost-using-m?comment=question
+1 that this would be a very welcome feature!
why is this not available? 🤦
can you please prioritize this ticket
@aritraghosh Could you consider this feature to add on AKS roadmap?
@aritraghosh Could you consider this feature to add on AKS roadmap?
I see related task regarding automated exporting and api access on roadmap: https://github.com/Azure/AKS/issues/4336
This is still needed ;)
The portal's Kubernetes namespaces view is backed by a preview Cost Management endpoint that does return per-namespace data — it's just not the stable /query API everyone tries first. Captured from the portal traffic and verified live with az rest:
POST https://management.azure.com/subscriptions/{subId}/providers/Microsoft.CostManagement/query?api-version=2023-04-01-preview clienttype: CostAnalysisV3 x-ms-command-name: ACM.CostAnalysisV3.KubernetesNamespaces.QueryPrimaryEntities Content-Type: application/json { "type": "AmortizedCost", "timeframe": "Custom", "timePeriod": { "from": "2026-03-01T00:00:00.000Z", "to": "2026-03-31T23:59:59.000Z" }, "provider": "Microsoft.ContainerService", "dataSet": { "granularity": "None", "aggregation": { "totalCost": { "name": "Cost", "function": "Sum" }, "totalCostUSD": { "name": "CostUSD", "function": "Sum" } }, "filter": { "dimensions": { "name": "Cluster", "operator": "In", "values": ["/subscriptions/{subId}/resourceGroups/{rg}/providers/Microsoft.ContainerService/managedClusters/{clusterName}"] } }, "grouping": [{ "type": "Dimension", "name": "Namespace" }], "sorting": [{ "direction": "descending", "name": "Cost" }] } } Returns rows of [Cost, CostUSD, Namespace, Currency] synchronously (sub-second). Both AmortizedCost and ActualCost work. Add ServiceCategory to grouping for a Compute/Networking/Storage drilldown (the portal calls that one …QueryPrimaryDetails). Idle/system/unallocated charges appear as namespaces named #idle charges#, #system charges#, #service charges#, #unallocated charges#.
Prereqs: AKS cost analysis add-on enabled on the cluster (Standard/Premium tier) plus Cost Management Reader on the subscription.
Caveat: the endpoint is undocumented and could change without notice. Save raw responses if you build tooling on top.