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.")
相关推荐
大鹏说大话7 小时前
从爬虫到决策引擎:大数据下自媒体如何用Python挖掘用户痛点
开发语言·爬虫·python
Niuguangshuo7 小时前
silero-vad:超轻量级开源 VAD 实践指南
开发语言·python
2501_944676168 小时前
揭秘!WORDTIP公司靠不靠谱?小白必看
大数据·人工智能·python
Minner-Scrapy8 小时前
Scrapy 2.17 源码解析:Scheduler 调度器与磁盘/内存双队列
java·爬虫·python·scrapy·网络爬虫·twisted
CODER03049 小时前
Anaconda和Mamba创建环境管理包常用命令合集
python·深度学习·conda·虚拟环境·mamba·常用命令大全
闲猫9 小时前
LangGraph / Capabilities / Fault tolerance
python·agent·langgraph
IvanCodes9 小时前
Python 基础语法(一):变量、数据类型与运算符
python
程序员三藏10 小时前
浅谈性能测试
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·性能测试
l12586510 小时前
# RAG向量数据库优化实战:HNSW索引调参与生产级性能设计
数据库·python·mysql·langchain
meilindehuzi_a11 小时前
Python 基础语法(1):常量、变量、类型、输入输出与运算符
开发语言·python