在AWS上创建一个月度预算(10美元)并设置邮件告警。具体包括:
-
创建月度预算 - 设置每月预算上限为10美元
-
配置两个级别的告警:
-
实际花费达到预算的80%(8美元)时发送邮件通知
-
实际花费达到预算的100%(10美元)时发送邮件通知
-
-
邮件通知 - 将告警发送到指定的邮箱

Python脚本
python
import boto3
# ==============================
# 配置
# ==============================
BUDGET_AMOUNT = 10.0 # 美金
EMAIL = "yourname@youremailprovider.com"
# ==============================
# AWS 客户端
# ==============================
budgets_client = boto3.client("budgets")
sts_client = boto3.client("sts")
account_id = sts_client.get_caller_identity()["Account"]
# ==============================
# 创建 Budget 并设置邮件通知
# ==============================
budgets_client.create_budget(
AccountId=account_id,
Budget={
'BudgetName': 'Monthly10USD',
'BudgetLimit': {'Amount': str(BUDGET_AMOUNT), 'Unit': 'USD'},
'TimeUnit': 'MONTHLY',
'BudgetType': 'COST',
},
NotificationsWithSubscribers=[
# 80% 警告邮件
{
'Notification': {
'NotificationType': 'ACTUAL', # 实际花费
'ComparisonOperator': 'GREATER_THAN',
'Threshold': 80, # 80%
'ThresholdType': 'PERCENTAGE',
'NotificationState': 'ALARM'
},
'Subscribers': [
{'SubscriptionType': 'EMAIL', 'Address': EMAIL}
]
},
# 100% 警告邮件
{
'Notification': {
'NotificationType': 'ACTUAL',
'ComparisonOperator': 'GREATER_THAN',
'Threshold': 100, # 100%
'ThresholdType': 'PERCENTAGE',
'NotificationState': 'ALARM'
},
'Subscribers': [
{'SubscriptionType': 'EMAIL', 'Address': EMAIL}
]
}
]
)
print("Budget created: 80% and 100% email alerts configured.")