What is the best way to retrieve AKS costs using a REST API?

Roman Skolin 20 Reputation points
2025-03-12T12:23:10.6433333+00:00

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

Azure Kubernetes Service
Azure Kubernetes Service
An Azure service that provides serverless Kubernetes, an integrated continuous integration and continuous delivery experience, and enterprise-grade security and governance.
2,456 questions
{count} votes

Accepted answer
  1. Anusree Nashetty 4,555 Reputation points Microsoft External Staff Moderator
    2025-03-17T06:54:29.8466667+00:00

    Hi Roman Skolin,

    You can target the specific Resource Group containing your AKS resources and can retrieve cost data using the Azure Cost Management API. This method allows you to filter costs associated with the AKS cluster.
    For your reference: Query Resource Group

    See that you are using the correct API endpoint and version, update to the latest version.

    To automate the process of retrieving cost data, you can use PowerShell scripts that interact with Azure APIs. Please check this which might help you.

    If you have any further queries, let me know. If the information is helpful, please click on Upvote.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.