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.")
相关推荐
helloweilei1 天前
python 抽象基类
python
用户8356290780511 天前
Python 实现 PPT 转 HTML
后端·python
Johny_Zhao1 天前
centos7安装部署openclaw
linux·人工智能·信息安全·云计算·yum源·系统运维·openclaw
zone77391 天前
004:RAG 入门-LangChain读取PDF
后端·python·面试
zone77391 天前
005:RAG 入门-LangChain读取表格数据
后端·python·agent
树獭非懒2 天前
AI大模型小白手册|Embedding 与向量数据库
后端·python·llm
唐叔在学习2 天前
就算没有服务器,我照样能够同步数据
后端·python·程序员
曲幽2 天前
FastAPI流式输出实战与避坑指南:让AI像人一样“边想边说”
python·ai·fastapi·web·stream·chat·async·generator·ollama
Flittly2 天前
【从零手写 AI Agent:learn-claude-code 项目实战笔记】(1)The Agent Loop (智能体循环)
python·agent