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

Roman Skolin 0 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 (AKS)
Azure Kubernetes Service (AKS)
An Azure service that provides serverless Kubernetes, an integrated continuous integration and continuous delivery experience, and enterprise-grade security and governance.
2,307 questions
0 comments No comments
{count} votes

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.