Python脚本 - 创建AWS月度预算

在AWS上创建一个月度预算(10美元)并设置邮件告警。具体包括:

  1. 创建月度预算 - 设置每月预算上限为10美元

  2. 配置两个级别的告警

    • 实际花费达到预算的80%(8美元)时发送邮件通知

    • 实际花费达到预算的100%(10美元)时发送邮件通知

  3. 邮件通知 - 将告警发送到指定的邮箱

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.")
相关推荐
金銀銅鐵6 小时前
[Python] 从《千字文》中随机挑选汉字
后端·python
cup1111 小时前
[技术复盘] Windows Python 打包实战:Nuitka 环境踩坑总结与 CI 自动化构建全指南
python·ai·环境变量·ci·nuitka·skill
aqi0013 小时前
15天学会AI应用开发(七)有了大模型为什么还要引入RAG
人工智能·python·大模型·ai编程·ai应用
金銀銅鐵15 小时前
用 Python 实现 Take-Away 游戏
python·游戏
copyer_xyf16 小时前
Agent 流程编排
后端·python·agent
copyer_xyf16 小时前
Agent RAG
后端·python·agent
copyer_xyf16 小时前
【RAG】向量数据库:milvus
后端·python·agent
copyer_xyf17 小时前
Agent 记忆管理
后端·python·agent
A小辣椒1 天前
AWS Clould Support Engineer就职面试题
aws