Microsoft Azure Cost Management Client Library for Python providing comprehensive access to cost analysis, billing data, budgets, alerts, exports, and recommendations.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Analyze and optimize Azure Reserved Instances, Savings Plans, and other cost-saving benefits. Get intelligent recommendations for optimal purchasing decisions, track utilization patterns, and maximize return on investment for Azure cost optimization benefits.
Get intelligent recommendations for Azure Reserved Instances, Savings Plans, and other cost optimization benefits based on usage patterns and cost analysis.
def list(
scope: str,
filter: str = None,
orderby: str = None,
expand: str = None
) -> BenefitRecommendationsListResult:
"""
List benefit recommendations for the specified scope.
Args:
scope (str): The scope to get recommendations for
filter (str): OData filter expression for filtering results
orderby (str): OData orderby expression for sorting results
expand (str): Expand options for additional properties
Returns:
BenefitRecommendationsListResult: Collection of benefit recommendations
"""Analyze utilization of existing benefits including Reserved Instances and Savings Plans to optimize usage and identify underutilized resources.
def list_by_billing_account_id(
billing_account_id: str,
filter: str = None,
grain_parameter: str = None
) -> BenefitUtilizationSummariesListResult:
"""
List benefit utilization summaries by billing account.
Args:
billing_account_id (str): The billing account ID
filter (str): OData filter expression
grain_parameter (str): Grain of the summary (Daily, Monthly)
Returns:
BenefitUtilizationSummariesListResult: Utilization summaries for billing account
"""
def list_by_billing_profile_id(
billing_account_id: str,
billing_profile_id: str,
filter: str = None,
grain_parameter: str = None
) -> BenefitUtilizationSummariesListResult:
"""
List benefit utilization summaries by billing profile.
Args:
billing_account_id (str): The billing account ID
billing_profile_id (str): The billing profile ID
filter (str): OData filter expression
grain_parameter (str): Grain of the summary
Returns:
BenefitUtilizationSummariesListResult: Utilization summaries for billing profile
"""
def list_by_savings_plan_order(
savings_plan_order_id: str,
filter: str = None,
grain_parameter: str = None
) -> BenefitUtilizationSummariesListResult:
"""
List benefit utilization summaries by savings plan order.
Args:
savings_plan_order_id (str): The savings plan order ID
filter (str): OData filter expression
grain_parameter (str): Grain of the summary
Returns:
BenefitUtilizationSummariesListResult: Utilization summaries for savings plan order
"""
def list_by_savings_plan_id(
savings_plan_order_id: str,
savings_plan_id: str,
filter: str = None,
grain_parameter: str = None
) -> BenefitUtilizationSummariesListResult:
"""
List benefit utilization summaries by specific savings plan.
Args:
savings_plan_order_id (str): The savings plan order ID
savings_plan_id (str): The specific savings plan ID
filter (str): OData filter expression
grain_parameter (str): Grain of the summary
Returns:
BenefitUtilizationSummariesListResult: Utilization summaries for specific savings plan
"""# Get RI recommendations for subscription
scope = "/subscriptions/{subscription-id}"
ri_recommendations = client.benefit_recommendations.list(
scope=scope,
filter="properties/recommendationDetails/recommendedQuantity gt 0",
orderby="properties/recommendationDetails/estimatedSavings desc"
)
print(f"Found {len(ri_recommendations.value)} RI recommendations:")
for recommendation in ri_recommendations.value:
props = recommendation.properties
details = props.recommendation_details
print(f" Resource Type: {props.resource_type}")
print(f" Location: {props.location}")
print(f" Recommended Quantity: {details.recommended_quantity}")
print(f" Estimated Savings: ${details.estimated_savings:.2f}")
print(f" Term: {details.term}")
print(f" Scope: {details.scope}")
print(f" Look Back Period: {props.look_back_period}")
print(" ---")# Get Savings Plan recommendations with expanded details
savings_recommendations = client.benefit_recommendations.list(
scope=scope,
filter="kind eq 'SavingsPlan'",
expand="properties/allRecommendationDetails"
)
print("Savings Plan Recommendations:")
for recommendation in savings_recommendations.value:
if recommendation.kind == "SavingsPlan":
props = recommendation.properties
print(f" Commitment Amount: ${props.commitment_amount}")
print(f" Annual Savings: ${props.annual_savings_amount:.2f}")
print(f" Total Cost: ${props.total_cost:.2f}")
print(f" Coverage Percentage: {props.coverage_percentage:.1f}%")
# Check all savings details if expanded
if hasattr(props, 'all_recommendation_details'):
for detail in props.all_recommendation_details:
print(f" Scope: {detail.scope}")
print(f" Savings: ${detail.estimated_savings:.2f}")# Get RI utilization for billing account
billing_account_id = "12345678"
ri_utilization = client.benefit_utilization_summaries.list_by_billing_account_id(
billing_account_id=billing_account_id,
filter="properties/kind eq 'Reservation'",
grain_parameter="Monthly"
)
print("Reserved Instance Utilization Summary:")
for summary in ri_utilization.value:
if summary.kind == "Reservation":
props = summary.properties
print(f" Reservation ID: {props.arm_sku_name}")
print(f" Usage Date: {props.usage_date}")
print(f" Utilized Percentage: {props.utilized_percentage:.1f}%")
print(f" Purchased Quantity: {props.purchased_quantity}")
print(f" Remaining Quantity: {props.remaining_quantity}")
print(f" Total Reserved Hours: {props.total_reserved_hours}")
print(f" Used Hours: {props.used_hours}")
print(" ---")# Get Savings Plan utilization
savings_plan_order_id = "sp-order-123"
sp_utilization = client.benefit_utilization_summaries.list_by_savings_plan_order(
savings_plan_order_id=savings_plan_order_id,
grain_parameter="Daily"
)
print("Savings Plan Utilization:")
total_benefit = 0
total_commitment = 0
for summary in sp_utilization.value:
props = summary.properties
print(f" Date: {props.usage_date}")
print(f" Benefit Amount: ${props.benefit_amount:.2f}")
print(f" Commitment Amount: ${props.commitment_amount:.2f}")
print(f" Utilization Percentage: {props.utilization_percentage:.1f}%")
total_benefit += props.benefit_amount or 0
total_commitment += props.commitment_amount or 0
if total_commitment > 0:
overall_utilization = (total_benefit / total_commitment) * 100
print(f"\nOverall Utilization: {overall_utilization:.1f}%")# Get recommendations for different benefit types
all_recommendations = client.benefit_recommendations.list(scope=scope)
# Group by benefit type
reservation_recs = []
savings_plan_recs = []
included_quantity_recs = []
for rec in all_recommendations.value:
if rec.kind == "Reservation":
reservation_recs.append(rec)
elif rec.kind == "SavingsPlan":
savings_plan_recs.append(rec)
elif rec.kind == "IncludedQuantity":
included_quantity_recs.append(rec)
print(f"Recommendation Summary:")
print(f" Reserved Instances: {len(reservation_recs)} recommendations")
print(f" Savings Plans: {len(savings_plan_recs)} recommendations")
print(f" Included Quantities: {len(included_quantity_recs)} recommendations")
# Compare potential savings
ri_savings = sum(r.properties.recommendation_details.estimated_savings
for r in reservation_recs
if hasattr(r.properties, 'recommendation_details'))
sp_savings = sum(r.properties.annual_savings_amount
for r in savings_plan_recs
if hasattr(r.properties, 'annual_savings_amount'))
print(f"\nPotential Annual Savings:")
print(f" Reserved Instances: ${ri_savings:.2f}")
print(f" Savings Plans: ${sp_savings:.2f}")
print(f" Total: ${ri_savings + sp_savings:.2f}")# Find underutilized reservations
underutilized_filter = "properties/utilizedPercentage lt 80 and properties/kind eq 'Reservation'"
underutilized = client.benefit_utilization_summaries.list_by_billing_account_id(
billing_account_id=billing_account_id,
filter=underutilized_filter,
grain_parameter="Monthly"
)
print("Underutilized Reservations (< 80% utilization):")
for summary in underutilized.value:
props = summary.properties
print(f" Resource: {props.arm_sku_name}")
print(f" Utilization: {props.utilized_percentage:.1f}%")
print(f" Unused Hours: {props.total_reserved_hours - props.used_hours}")
print(f" Potential Waste: ${(props.total_reserved_hours - props.used_hours) * props.cost_per_hour:.2f}")
print(" ---")class BenefitRecommendationModel:
id: str
name: str
type: str
kind: BenefitKind
properties: BenefitRecommendationProperties
class BenefitRecommendationProperties:
first_consumption_date: str
last_consumption_date: str
look_back_period: LookBackPeriod
total_hours: int
device: str
service: str
resource_type: str
location: str
resource_group: str
subscription_id: str
commitment_amount: float
annual_savings_amount: float
total_cost: float
coverage_percentage: float
all_recommendation_details: List[AllSavingsBenefitDetails]
recommendation_details: RecommendationUsageDetails
class RecommendationUsageDetails:
usage_details: List[dict]
recommended_quantity: float
estimated_savings: float
term: Term
scope: Scope
class BenefitRecommendationsListResult:
value: List[BenefitRecommendationModel]
next_link: str
class AllSavingsBenefitDetails:
overshot: float
savings_amount: float
scope: str
estimated_savings: floatclass BenefitUtilizationSummary:
id: str
name: str
type: str
kind: BenefitKind
properties: BenefitUtilizationSummaryProperties
class BenefitUtilizationSummaryProperties:
usage_date: str
arm_sku_name: str
benefit_id: str
benefit_order_id: str
benefit_type: str
utilized_percentage: float
purchased_quantity: float
remaining_quantity: float
total_reserved_hours: float
used_hours: float
unused_hours: float
cost_per_hour: float
commitment_amount: float
benefit_amount: float
utilization_percentage: float
class SavingsPlanUtilizationSummary:
id: str
name: str
type: str
kind: BenefitKind
properties: SavingsPlanUtilizationSummaryProperties
class SavingsPlanUtilizationSummaryProperties:
usage_date: str
benefit_id: str
benefit_order_id: str
commitment_amount: float
benefit_amount: float
utilization_percentage: float
class IncludedQuantityUtilizationSummary:
id: str
name: str
type: str
kind: BenefitKind
properties: IncludedQuantityUtilizationSummaryProperties
class IncludedQuantityUtilizationSummaryProperties:
usage_date: str
service_name: str
included_quantity: float
utilized_quantity: float
utilization_percentage: float
class BenefitUtilizationSummariesListResult:
value: List[BenefitUtilizationSummary]
next_link: strclass BenefitKind(str, Enum):
INCLUDED_QUANTITY = "IncludedQuantity"
RESERVATION = "Reservation"
SAVINGS_PLAN = "SavingsPlan"
class LookBackPeriod(str, Enum):
LAST7DAYS = "Last7Days"
LAST30DAYS = "Last30Days"
LAST60DAYS = "Last60Days"
class Scope(str, Enum):
SINGLE = "Single"
SHARED = "Shared"
MANAGEMENT_GROUP = "ManagementGroup"
class Term(str, Enum):
P1Y = "P1Y" # 1 year
P3Y = "P3Y" # 3 years
P5Y = "P5Y" # 5 years
class GrainParameter(str, Enum):
DAILY = "Daily"
MONTHLY = "Monthly"This module provides comprehensive benefit optimization capabilities for maximizing cost savings through intelligent recommendations and detailed utilization tracking of Azure Reserved Instances, Savings Plans, and included quantity benefits.
Install with Tessl CLI
npx tessl i tessl/pypi-azure-mgmt-costmanagement