Azure Cost Management Client Library
The Microsoft Azure Cost Management Client Library for Python allows developers to manage Azure cost and usage data programmatically. It provides access to features like querying cost data, managing budgets, and working with dimensions and exports. Currently at version 4.0.1, it follows the Azure SDK release cadence, with updates often tied to new API versions and general SDK guidelines.
Warnings
- breaking Version 3.0.0 introduced a complete rewrite of the SDK following new Azure SDK guidelines. The main client class was renamed to `CostManagementClient`, and all models and operations moved under `azure.mgmt.costmanagement.models` and `azure.mgmt.costmanagement.operations`. Asynchronous operations are now available via `azure.mgmt.costmanagement.aio`.
- breaking In version 4.0.0, the `QueryDefinition` model had two new required parameters added: `time_period` and `type`. Calls using `QueryDefinition` without these parameters will fail.
- gotcha Azure SDK management libraries like `azure-mgmt-costmanagement` require separate installation of an authentication library (e.g., `azure-identity`) for convenient credential management. While `azure-mgmt-costmanagement` does not directly depend on `azure-identity` via `pip` requirements, it is essential for practical use.
- gotcha The Cost Management API often uses 'scopes' to define the context for operations (e.g., subscription, resource group, management group). Incorrectly formatted scopes or scopes without appropriate permissions will result in authorization errors.
Install
-
pip install azure-mgmt-costmanagement azure-identity
Imports
- CostManagementClient
from azure.mgmt.costmanagement import CostManagementClient
- DefaultAzureCredential
from azure.identity import DefaultAzureCredential
Quickstart
import os
from azure.identity import DefaultAzureCredential
from azure.mgmt.costmanagement import CostManagementClient
# Set your Azure Subscription ID from environment variables
subscription_id = os.environ.get("AZURE_SUBSCRIPTION_ID", "YOUR_SUBSCRIPTION_ID")
if subscription_id == "YOUR_SUBSCRIPTION_ID":
print("Please set the AZURE_SUBSCRIPTION_ID environment variable.")
else:
try:
# Authenticate using DefaultAzureCredential (looks for env vars, managed identity, etc.)
credential = DefaultAzureCredential()
# Create a CostManagementClient
client = CostManagementClient(credential, subscription_id)
# Example: List budgets for the subscription
print(f"Listing budgets for subscription: {subscription_id}")
scope = f"subscriptions/{subscription_id}"
budgets = client.budgets.list(scope=scope)
found_budgets = False
for budget in budgets:
print(f" Budget Name: {budget.name}, Amount: {budget.amount} {budget.currency}")
found_budgets = True
if not found_budgets:
print(" No budgets found for this subscription.")
except Exception as e:
print(f"An error occurred: {e}")
print("Ensure you have set up Azure credentials (e.g., AZURE_CLIENT_ID, AZURE_CLIENT_SECRET, AZURE_TENANT_ID) or are logged in via `az login`.")