$ aws budgets create --budget-name critical-budget
你有没有过这种经历:月底看账单,发现云费用比预期多了几倍,然后疯狂追查是谁"搞事情"。云预算告警就是来解决这个问题的——让账单超支在发生之前就被发现。
为什么预算告警如此重要?
根据 2024 年 Flexera State of the Cloud Report:
- 32% 的企业 表示意外超支是最大的云成本痛点
- 平均云浪费率高达 15-32%
- 大多数超支发生在 非工作时间(测试环境、资源泄露)
三大云厂商预算告警对比
| 特性 | AWS Budgets | Azure Budgets | GCP Budgets |
|---|---|---|---|
| 免费额度 | 2个预算/月 | 无限制 | 5个预算 |
| 告警阈值 | 1个阈值 | 多个阈值 | 多个阈值 |
| 通知方式 | Email/SQS/Lambda | Email/Action Group | |
| 触发动作 | 支持自动停止 | 支持自动停止 | 仅告警 |
| 覆盖维度 | 账户/服务/标签 | 订阅/RG/资源 | 项目/SKU |
AWS Budgets 实战配置
创建月度总预算
{
"BudgetName": "Monthly-Total-Budget",
"BudgetLimit": {
"Amount": "10000",
"Unit": "USD"
},
"TimeUnit": "MONTHLY",
"BudgetType": "COST",
"CostFilters": {},
"CostTypes": {
"IncludeCredit": true,
"IncludeOtherSubscription": true
}
}
设置阈值告警
{
"Notification": {
"NotificationType": "ACTUAL",
"ComparisonOperator": "GREATER_THAN",
"Threshold": 80,
"ThresholdType": "PERCENTAGE"
}
}
Azure Budgets 配置
Azure 的预算管理更灵活,支持多层级告警:
az consumption budget create \ --resource-group my-rg \ --budget-name dev-budget \ --amount 500 \ --time-grain monthly \ --start-date 2025-05-01 \ --alert-scope subscriptions/xxxx/resourceGroups/my-rg \ --notification-threshold 0.8 \ --notification-threshold 1.0
GCP Budgets 配置
GCP 通过 Cloud Billing Budgets API 管理:
gcloud beta billing budgets create \ --billing-account=ABCD-1234-EFGH \ --display-name="Project Budget" \ --amount=5000 \ --threshold-rules percent=0.9 \ --threshold-rules percent=1.0
最佳实践
分层预算策略
建议设置多级预算:
- Level 1 (75%) —— 警告,通知 DevOps 团队
- Level 2 (90%) —— 严重警告,通知 CTO/CFO
- Level 3 (100%) —— 触发自动动作(如停止测试资源)
场景化预算
不要只设置一个总预算,建议按场景细分:
- 环境预算 —— Production $50K、Staging $5K、Dev $2K
- 服务预算 —— Data Transfer $1K、NLP API $3K
- 项目预算 —— 新项目初期限制 $500/月
- 团队预算 —— 按部门或成本中心设置
自动化响应(Optional)
AWS 和 Azure 都支持在超预算时自动触发动作:
- Lambda/Azure Function —— 自定义处理逻辑
- SSM Automation —— 停止特定资源
- Webhook —— 集成 Slack/PagerDuty
关键要点:
- 立即设置预算告警
- 分层设置阈值(80%/90%/100%)
- 按环境/服务/项目细分预算
- 配置自动响应减少人工干预
- 预算要定期回顾和调整
预算告警是云成本治理的第一步,但绝对不是唯一的一步。