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.")
相关推荐
EntyIU16 分钟前
mineru从安装部署到测试使用完整指南
python·ocr
安替-AnTi1 小时前
厚朴 APK 搜索接口分析
python·apk·解析·taobao
山川湖海1 小时前
AI时代快速学编程语言的陷阱(以Python为例)
大数据·人工智能·python
H Journey1 小时前
Supervisor 进程管理工具介绍
python·supervisor·linux 运维
春日见2 小时前
5分钟入门强化学习之动态规划算法与实现
大数据·人工智能·python·算法·机器学习·计算机视觉
DeniuHe2 小时前
sklearn 中所有交叉验证数据集划分方式完整总结
人工智能·python·sklearn
wanhengidc2 小时前
云手机搬砖 像僵尸开炮
运维·网络·智能手机·云计算
DeniuHe2 小时前
sklearn中不同交叉验证方法的场景适配
人工智能·python·sklearn
隐于花海,等待花开3 小时前
16.Python 常用第三方库概览 深度解析
python
我材不敲代码3 小时前
Python 函数核心:位置参数与关键字参数详解
java·前端·python