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
Automate cost management tasks with scheduled actions including email notifications, insights alerts, and custom workflows triggered by cost events. Configure automated responses to budget thresholds, cost anomalies, and billing events for proactive cost management.
Create, update, and manage automated actions that respond to cost events and thresholds with customizable schedules and notification settings.
def list(filter: str = None) -> ScheduledActionListResult:
"""
List all scheduled actions.
Args:
filter (str): Optional filter expression for results
Returns:
ScheduledActionListResult: Collection of scheduled actions
"""
def create_or_update(
name: str,
scheduled_action: ScheduledAction,
if_match: str = None
) -> ScheduledAction:
"""
Create or update a scheduled action.
Args:
name (str): Name of the scheduled action
scheduled_action (ScheduledAction): Action configuration details
if_match (str): Optional ETag for concurrency control
Returns:
ScheduledAction: Created or updated scheduled action
"""
def get(name: str) -> ScheduledAction:
"""
Get a specific scheduled action by name.
Args:
name (str): Name of the scheduled action
Returns:
ScheduledAction: Scheduled action details and configuration
"""
def delete(name: str) -> None:
"""
Delete a scheduled action.
Args:
name (str): Name of the scheduled action to delete
"""
def run(name: str) -> None:
"""
Execute a scheduled action immediately.
Args:
name (str): Name of the scheduled action to execute
"""Manage scheduled actions within specific scopes for targeted automation and notifications.
def list_by_scope(scope: str, filter: str = None) -> ScheduledActionListResult:
"""
List scheduled actions for a specific scope.
Args:
scope (str): The scope to list actions for
filter (str): Optional filter expression
Returns:
ScheduledActionListResult: Collection of scoped scheduled actions
"""
def create_or_update_by_scope(
scope: str,
name: str,
scheduled_action: ScheduledAction,
if_match: str = None
) -> ScheduledAction:
"""
Create or update a scheduled action within a specific scope.
Args:
scope (str): The scope for the action
name (str): Name of the scheduled action
scheduled_action (ScheduledAction): Action configuration
if_match (str): Optional ETag for concurrency control
Returns:
ScheduledAction: Created or updated scoped action
"""
def get_by_scope(scope: str, name: str) -> ScheduledAction:
"""
Get a scheduled action within a specific scope.
Args:
scope (str): The scope containing the action
name (str): Name of the scheduled action
Returns:
ScheduledAction: Scoped scheduled action details
"""
def delete_by_scope(scope: str, name: str) -> None:
"""
Delete a scheduled action within a specific scope.
Args:
scope (str): The scope containing the action
name (str): Name of the scheduled action to delete
"""
def run_by_scope(scope: str, name: str) -> None:
"""
Execute a scheduled action within a specific scope immediately.
Args:
scope (str): The scope containing the action
name (str): Name of the scheduled action to execute
"""Check name availability for scheduled actions to ensure unique naming.
def check_name_availability(body: CheckNameAvailabilityRequest) -> CheckNameAvailabilityResponse:
"""
Check if a scheduled action name is available.
Args:
body (CheckNameAvailabilityRequest): Name availability check request
Returns:
CheckNameAvailabilityResponse: Availability status and reason
"""
def check_name_availability_by_scope(
scope: str,
body: CheckNameAvailabilityRequest
) -> CheckNameAvailabilityResponse:
"""
Check name availability within a specific scope.
Args:
scope (str): The scope to check within
body (CheckNameAvailabilityRequest): Name availability check request
Returns:
CheckNameAvailabilityResponse: Scoped availability status
"""from azure.mgmt.costmanagement.models import (
ScheduledAction,
ScheduleProperties,
NotificationProperties,
ScheduledActionKind,
ScheduleFrequency,
ScheduledActionStatus
)
# Configure email notification
notification = NotificationProperties(
to=["finance-team@company.com", "manager@company.com"],
subject="Budget Alert: Monthly costs exceeding 80%",
message="Monthly spending has exceeded 80% of the allocated budget. Please review costs immediately."
)
# Configure schedule (weekly check)
schedule = ScheduleProperties(
frequency=ScheduleFrequency.WEEKLY,
hour_of_day=9,
days_of_week=["Monday"],
start_date="2024-01-01T00:00:00Z",
end_date="2024-12-31T23:59:59Z"
)
# Create scheduled action
budget_alert = ScheduledAction(
kind=ScheduledActionKind.EMAIL,
display_name="Weekly Budget Alert",
file_destination=None,
notification=notification,
schedule=schedule,
scope="/subscriptions/{subscription-id}",
status=ScheduledActionStatus.ENABLED,
view_id="/subscriptions/{sub-id}/providers/Microsoft.CostManagement/views/budget-view"
)
created_action = client.scheduled_actions.create_or_update(
name="weekly-budget-alert",
scheduled_action=budget_alert
)
print(f"Created scheduled action: {created_action.display_name}")# List all scheduled actions
actions = client.scheduled_actions.list()
print(f"Found {len(actions.value)} scheduled actions:")
for action in actions.value:
print(f" Name: {action.name}")
print(f" Display Name: {action.display_name}")
print(f" Kind: {action.kind}")
print(f" Status: {action.status}")
print(f" Frequency: {action.schedule.frequency}")
print(f" Next Run: {action.schedule.start_date}")
print(" ---")
# List actions for specific subscription
scope = "/subscriptions/{subscription-id}"
scoped_actions = client.scheduled_actions.list_by_scope(scope)
print(f"\nSubscription actions ({len(scoped_actions.value)}):")
for action in scoped_actions.value:
print(f" {action.name}: {action.status}")# Daily insight alert for cost anomalies
daily_notification = NotificationProperties(
to=["cost-alerts@company.com"],
subject="Daily Cost Insights - Anomaly Detection",
message="Daily cost analysis has detected potential anomalies in your Azure spending."
)
daily_schedule = ScheduleProperties(
frequency=ScheduleFrequency.DAILY,
hour_of_day=8,
start_date="2024-01-01T00:00:00Z"
)
insight_alert = ScheduledAction(
kind=ScheduledActionKind.INSIGHT_ALERT,
display_name="Daily Cost Insights",
notification=daily_notification,
schedule=daily_schedule,
scope=scope,
status=ScheduledActionStatus.ENABLED
)
daily_action = client.scheduled_actions.create_or_update(
name="daily-cost-insights",
scheduled_action=insight_alert
)# Run a scheduled action on-demand
print("Executing budget alert immediately...")
client.scheduled_actions.run("weekly-budget-alert")
print("Action executed successfully")
# Run scoped action
client.scheduled_actions.run_by_scope(scope, "daily-cost-insights")
print("Scoped action executed successfully")from azure.mgmt.costmanagement.models import CheckNameAvailabilityRequest
# Check if action name is available
availability_request = CheckNameAvailabilityRequest(
name="new-budget-alert",
type="Microsoft.CostManagement/scheduledActions"
)
availability = client.scheduled_actions.check_name_availability(availability_request)
if availability.name_available:
print(f"Name '{availability_request.name}' is available")
else:
print(f"Name not available. Reason: {availability.reason}")
print(f"Message: {availability.message}")# Get existing action
existing_action = client.scheduled_actions.get("weekly-budget-alert")
# Update notification recipients
existing_action.notification.to.append("ceo@company.com")
# Update schedule frequency to daily
existing_action.schedule.frequency = ScheduleFrequency.DAILY
existing_action.schedule.hour_of_day = 7
# Update the action
updated_action = client.scheduled_actions.create_or_update(
name="weekly-budget-alert",
scheduled_action=existing_action
)
print(f"Updated action: {updated_action.display_name}")class ScheduledAction:
id: str
name: str
type: str
kind: ScheduledActionKind
display_name: str
file_destination: FileDestination
notification: NotificationProperties
schedule: ScheduleProperties
scope: str
status: ScheduledActionStatus
view_id: str
class ScheduleProperties:
frequency: ScheduleFrequency
hour_of_day: int
days_of_week: List[DaysOfWeek]
weeks_of_month: List[WeeksOfMonth]
day_of_month: int
start_date: str
end_date: str
class NotificationProperties:
to: List[str]
language: str
message: str
regional_format: str
subject: str
class ScheduledActionListResult:
value: List[ScheduledAction]
next_link: str
class CheckNameAvailabilityRequest:
def __init__(self, name: str, type: str): ...
class CheckNameAvailabilityResponse:
name_available: bool
reason: str
message: str
class FileDestination:
file_formats: List[str]class ScheduledActionKind(str, Enum):
EMAIL = "Email"
INSIGHT_ALERT = "InsightAlert"
class ScheduledActionStatus(str, Enum):
ENABLED = "Enabled"
DISABLED = "Disabled"
EXPIRED = "Expired"
class ScheduleFrequency(str, Enum):
DAILY = "Daily"
WEEKLY = "Weekly"
MONTHLY = "Monthly"
class DaysOfWeek(str, Enum):
MONDAY = "Monday"
TUESDAY = "Tuesday"
WEDNESDAY = "Wednesday"
THURSDAY = "Thursday"
FRIDAY = "Friday"
SATURDAY = "Saturday"
SUNDAY = "Sunday"
class WeeksOfMonth(str, Enum):
FIRST = "First"
SECOND = "Second"
THIRD = "Third"
FOURTH = "Fourth"
LAST = "Last"
class CheckNameAvailabilityReason(str, Enum):
INVALID = "Invalid"
ALREADY_EXISTS = "AlreadyExists"This module provides comprehensive scheduled action capabilities for automating cost management workflows, notifications, and proactive cost monitoring across Azure resources and scopes.
Install with Tessl CLI
npx tessl i tessl/pypi-azure-mgmt-costmanagement