What is the best way to retrieve AKS costs using a REST API?
Is it somehow possible to read out AKS costs via REST API?
I made a POST request to the Cost Management API endpoint.
management.azure.com/subscriptions/xxxxx/providers/Microsoft.CostManagement/query?api-version=2024-08-01
with payload
"type": "ActualCost",
"dataSet": {
"granularity": "None",
"aggregation": {
"totalCost": {
"name": "Cost",
"function": "Sum"
},
"totalCostUSD": {
"name": "CostUSD",
"function": "Sum"
}
},
"sorting": [
{
"direction": "descending",
"name": "Cost"
}
],
"filter": {
"and": [
{
"dimensions": {
"name": "Cluster",
"operator": "In",
"values": [
"/subscriptions/xxxxxxxxxxx/resourceGroups/ma-smuod-appcity-test/providers/Microsoft.ContainerService/managedClusters/smuod-appcity-test-aks-green"
]
}
},
{
"dimensions": {
"name": "Cluster",
"operator": "In",
"values": [
"/subscriptions/xxxxxxxxxxxx/resourcegroups/ma-smuod-appcity-test/providers/microsoft.containerservice/managedclusters/smuod-appcity-test-aks-green"
]
}
},
{
"dimensions": {
"name": "Namespace",
"operator": "In",
"values": [
"#idle charges#",
"kube-system",
"#service charges#",
"nginx-ingress",
"#system charges#",
"#unallocated charges#",
"appcity"
]
}
}
]
},
"grouping": [
{
"type": "Dimension",
"name": "Namespace"
},
{
"type": "Dimension",
"name": "Cluster"
},
{
"type": "Dimension",
"name": "ServiceCategory"
}
]
},
"timeframe": "Custom",
"timePeriod": {
"from": "2025-03-01T00:00:00.000Z",
"to": "2025-03-31T23:59:59.000Z"
},
"provider": "Microsoft.ContainerService"
}
with the access token obtained using an entra application that has the Owner role in my subscription. However, it seems that queries for "provider": "Microsoft.ContainerService" are only possible from within the Azure portal.
I would like to automate cost retrieval without using the Azure Portal or CubeCost/OpenCost solutions. What are the best options for achieving this?
Br