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.")
相关推荐
xywww16813 小时前
大模型 API 选型实战:GPT、Gemini、Claude 接入时该看哪些指标?
运维·服务器·人工智能·python·gpt·langchain
小白考证进阶中17 小时前
阿里云认证重要通知(Clouder+ACX职业岗位)
阿里云·云计算·阿里云acp报名·阿里云acp备考·阿里云aca证书·阿里云aca大模型·阿里云aca报名
夜雪一千18 小时前
Python enumerate() 函数完整详解:遍历同时获取索引,告别手动计数
服务器·windows·python
能有时光19 小时前
PyTorch KernelAgent 源码解读 ---(4)--- ExtractorAgent
人工智能·pytorch·python
spider_xcxc19 小时前
告别单点故障:阿里云CLB负载均衡从入门到实战
阿里云·云计算·负载均衡
_Jimmy_19 小时前
Python 协程库如何使用以及有哪些使用场景
python
aqi0020 小时前
15天学会AI应用开发(十七)使用LangGraph实现会话记忆功能
人工智能·python·大模型·ai编程·ai应用
第一程序员20 小时前
Rust Agent 子进程执行:Command 之前,先定义输入和超时
python·rust·github
skywalk816320 小时前
设计并实现段言的 C FFI 绑定机制 @Trae
c语言·开发语言·python·编程
weixin_BYSJ198720 小时前
SpringBoot + MySQL 乒乓球运动员信息管理系统项目实战--附源码04954
java·javascript·spring boot·python·django·flask·php