mcpField
A
aws-cost-finops

aws-cost-finops

github.com/ahmedasmar云与 DevOps204105 次安装29 周前更新
代码仓库

关于 aws-cost-finops

aws-cost-finops 是面向 AWS 工程与 FinOps 团队的成本优化技能,提供从资源盘点、支出分析到治理和采购决策的流程。它配有诊断脚本、参考指南和月度报告模板。

主要功能

  1. 01识别闲置资源与成本异常
  2. 02分析实例规格调整机会
  3. 03评估 RI、Savings Plans 和 Spot
  4. 04提供成本治理与月度复盘流程
  5. 05提供 AWS 成本诊断脚本和报告模板

适用场景

  1. 01调查 AWS 账单异常或支出增长
  2. 02开展月度成本优化审查
  3. 03评估稳定工作负载的承诺折扣
  4. 04制定预算、标签和成本治理流程

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/ahmedasmar/devops-claude-skills --skill aws-cost-finops

安装后在 Claude Code 中输入/aws-cost-finops确认已加载。

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

.claude-plugin/
plugin.json
assets/templates/
monthly_cost_report.md
references/
best_practices.md
finops_governance.md
service_alternatives.md
scripts/
analyze_ri_recommendations.py
cost_anomaly_detector.py
detect_old_generations.py
find_unused_resources.py
rightsizing_analyzer.py
spot_recommendations.py

详细介绍

AWS 成本优化与 FinOps

用于 AWS 成本优化和财务运营管理的系统化工作流。

何时使用此技能

在以下情况下使用此技能:

  • 寻找节省成本的机会:识别未使用的资源、调整规格的机会或承诺折扣
  • 分析支出:了解成本趋势、检测异常或拆分成本
  • 优化架构:选择经济高效的服务、存储层或实例类型
  • 实施 FinOps:设置治理、标签、预算或月度审查
  • 做出购买决策:评估 Reserved Instances、Savings Plans 或 Spot 实例
  • 排查成本问题:调查意外账单或成本激增
  • 规划预算:预测成本或评估新项目的影响

成本优化工作流

按照以下系统化方法优化 AWS 成本:

┌─────────────────────────────────────────────┐
│ 1. DISCOVER                                 │
│    What are we spending money on?           │
│    Run: find_unused_resources.py            │
│    Run: cost_anomaly_detector.py            │
└─────────────────────────────────────────────┘
                    ↓
┌─────────────────────────────────────────────┐
│ 2. ANALYZE                                  │
│    Where are the optimization opportunities?│
│    Run: rightsizing_analyzer.py             │
│    Run: detect_old_generations.py           │
│    Run: spot_recommendations.py             │
│    Run: analyze_ri_recommendations.py       │
└─────────────────────────────────────────────┘
                    ↓
┌─────────────────────────────────────────────┐
│ 3. PRIORITIZE                               │
│    What should we optimize first?           │
│    - Quick wins (low risk, high savings)    │
│    - Low-hanging fruit (easy to implement)  │
│    - Strategic improvements                 │
└─────────────────────────────────────────────┘
                    ↓
┌─────────────────────────────────────────────┐
│ 4. IMPLEMENT                                │
│    Execute optimization actions             │
│    - Delete unused resources                │
│    - Rightsize instances                    │
│    - Purchase commitments                   │
│    - Migrate to new generations             │
└─────────────────────────────────────────────┘
                    ↓
┌─────────────────────────────────────────────┐
│ 5. MONITOR                                  │
│    Verify savings and track metrics         │
│    - Monthly cost reviews                   │
│    - Tag compliance monitoring              │
│    - Budget variance tracking               │
└─────────────────────────────────────────────┘

核心工作流

工作流 1:月度成本优化审查

频率:每月执行(每月第一周)

步骤 1:查找未使用的资源

# Scan for waste across all resources
python3 scripts/find_unused_resources.py

# Expected output:
# - Unattached EBS volumes
# - Old snapshots
# - Unused Elastic IPs
# - Idle NAT Gateways
# - Idle EC2 instances
# - Unused load balancers
# - Estimated monthly savings

步骤 2:分析成本异常

# Detect unusual spending patterns
python3 scripts/cost_anomaly_detector.py --days 30

# Expected output:
# - Cost spikes and anomalies
# - Top cost drivers
# - Period-over-period comparison
# - 30-day forecast

步骤 3:识别调整规格的机会

# Find oversized instances
python3 scripts/rightsizing_analyzer.py --days 30

# Expected output:
# - EC2 instances with low utilization
# - RDS instances with low utilization
# - Recommended smaller instance types
# - Estimated savings

步骤 4:生成月度报告

# Use the template to compile findings
cp assets/templates/monthly_cost_report.md reports/$(date +%Y-%m)-cost-report.md

# Fill in:
# - Findings from scripts
# - Action items
# - Team cost breakdowns
# - Optimization wins

步骤 5:团队审查会议

  • 向工程团队展示发现
  • 分配优化任务
  • 跟踪待办事项直至完成

工作流 2:承诺购买分析(RI/Savings Plans)

时机:每季度一次,或使用模式趋于稳定时

步骤 1:分析当前使用情况

# Identify workloads suitable for commitments
python3 scripts/analyze_ri_recommendations.py --days 60

# Looks for:
# - EC2 instances running consistently for 60+ days
# - RDS instances with stable usage
# - Calculates ROI for 1yr vs 3yr commitments

步骤 2:审查建议

评估每项建议:

✅ Good candidate if:
  - Running 24/7 for 60+ days
  - Workload is stable and predictable
  - No plans to change architecture
  - Savings > 30%

❌ Poor candidate if:
  - Workload is variable or experimental
  - Architecture changes planned
  - Instance type may change
  - Dev/test environment

步骤 3:选择承诺类型

Reserved Instances:

  • Standard RI:折扣最高(63%),无灵活性
  • Convertible RI:折扣适中(54%),可更改实例类型
  • 最适用于:特定实例类型、稳定工作负载

Savings Plans:

  • Compute SP:可灵活适用于不同实例类型和区域(节省 66%)
  • EC2 Instance SP:可灵活适用于同一系列中的不同规格(节省 72%)
  • 最适用于:限制条件范围内的可变工作负载

决策矩阵:

Known instance type, won't change → Standard RI
May need to change types → Convertible RI or Compute SP
Variable workloads → Compute Savings Plan
Maximum flexibility → Compute Savings Plan

步骤 4:购买并跟踪

  • 通过 AWS Console 或 CLI 购买
  • 为承诺添加购买日期和负责人标签
  • 每月监控使用率
  • 目标使用率 >90%

参考:详细的承诺策略请参阅 references/best_practices.md


工作流 3:实例代际迁移

时机:架构审查期间或优化冲刺期间

步骤 1:检测旧实例

# Find outdated instance generations
python3 scripts/detect_old_generations.py

# Identifies:
# - t2 → t3 migrations (10% savings)
# - m4 → m5 → m6i migrations
# - Intel → Graviton opportunities (20% savings)

步骤 2:确定迁移优先级

快速见效(低风险):

t2 → t3: Drop-in replacement, 10% savings
m4 → m5: Better performance, 5% savings
gp2 → gp3: No downtime, 20% savings

中等工作量(需要测试):

x86 → Graviton (ARM64): 20% savings
- Requires ARM64 compatibility testing
- Most modern frameworks support ARM64
- Test in staging first

步骤 3:执行迁移

对于 EC2(x86 到 x86):

  1. 停止实例
  2. 更改实例类型
  3. 启动实例
  4. 验证应用

对于 Graviton 迁移:

  1. 创建 ARM64 AMI 或 Docker 镜像
  2. 启动新的 Graviton 实例
  3. 全面测试
  4. 切换流量
  5. 终止旧实例

步骤 4:验证节省金额

  • 在 Cost Explorer 中监控新成本
  • 确认性能符合要求
  • 记录迁移过程,供其他团队参考

参考:请参阅 references/best_practices.md → 计算优化


工作流 4:评估 Spot 实例

时机:适用于容错工作负载或 Auto Scaling Groups

步骤 1:识别候选对象

# Analyze workloads for Spot suitability
python3 scripts/spot_recommendations.py

# Evaluates:
# - Instances in Auto Scaling Groups (good candidates)
# - Dev/test/staging environments
# - Batch processing workloads
# - CI/CD and build servers

步骤 2:评估适用性

非常适合 Spot:

  • 无状态应用
  • 批处理作业
  • CI/CD 流水线
  • 数据处理
  • Auto Scaling Groups

不适合 Spot:

  • 数据库(没有副本时)
  • 有状态应用
  • 实时服务
  • 关键任务型工作负载

步骤 3:实施策略

选项 1:Fargate Spot(最简单)

# ECS task definition
requiresCompatibilities:
  - FARGATE
capacityProviderStrategy:
  - capacityProvider: FARGATE_SPOT
    weight: 70  # 70% Spot
  - capacityProvider: FARGATE
    weight: 30  # 30% On-Demand

选项 2:使用 Spot 的 EC2 Auto Scaling

# Mixed instances policy
MixedInstancesPolicy:
  InstancesDistribution:
    OnDemandBaseCapacity: 2
    OnDemandPercentageAboveBaseCapacity: 30
    SpotAllocationStrategy: capacity-optimized
  LaunchTemplate:
    Overrides:
      - InstanceType: m5.large
      - InstanceType: m5a.large
      - InstanceType: m5n.large

选项 3:EC2 Spot Fleet

# Create Spot Fleet with diverse instance types
aws ec2 request-spot-fleet --spot-fleet-request-config file://spot-fleet.json

步骤 4:实施中断处理

# Handle 2-minute termination notice
# Instance metadata: /latest/meta-data/spot/instance-action

# In application:
1. Poll for termination notice
2. Gracefully shutdown (save state)
3. Drain connections
4. Exit

参考:请参阅 references/best_practices.md → 计算优化 → Spot 实例


快速参考:成本优化脚本

所有脚本的位置
ls scripts/
# find_unused_resources.py
# analyze_ri_recommendations.py
# detect_old_generations.py
# spot_recommendations.py
# rightsizing_analyzer.py
# cost_anomaly_detector.py
脚本使用模式

月度审查(运行全部脚本):

python3 scripts/find_unused_resources.py
python3 scripts/cost_anomaly_detector.py --days 30
python3 scripts/rightsizing_analyzer.py --days 30

季度优化:

python3 scripts/analyze_ri_recommendations.py --days 60
python3 scripts/detect_old_generations.py
python3 scripts/spot_recommendations.py

仅限特定区域:

python3 scripts/find_unused_resources.py --region us-east-1
python3 scripts/rightsizing_analyzer.py --region us-west-2

指定 AWS 配置文件:

python3 scripts/find_unused_resources.py --profile production
python3 scripts/cost_anomaly_detector.py --profile production --days 60
脚本要求
# Install dependencies
pip install boto3 tabulate

# AWS credentials required
# Configure via: aws configure
# Or use: --profile PROFILE_NAME

特定服务优化

计算优化

关键操作:

  • 迁移到 Graviton(节省 20%)
  • 对容错工作负载使用 Spot(节省 70%)
  • 为稳定的工作负载购买 RI(节省 40-65%)
  • 为过度配置的实例调整规格

参考:references/best_practices.md → 计算优化

存储优化

关键操作:

  • 将 gp2 转换为 gp3(节省 20%)
  • 实施 S3 生命周期策略(节省 50-95%)
  • 删除旧快照
  • 使用 S3 Intelligent-Tiering

参考:references/best_practices.md → 存储优化

网络优化

关键操作:

  • 将 NAT Gateways 替换为 VPC Endpoints(每个每月节省 $25-30)
  • 使用 CloudFront 降低数据传输成本
  • 尽可能将资源放置在同一个 AZ 中

参考:references/best_practices.md → 网络优化

数据库优化

关键操作:

  • 为 RDS 实例调整规格
  • 使用 gp3 存储(比 gp2 便宜 20%)
  • 评估 Aurora Serverless 是否适用于可变工作负载
  • 购买 RDS Reserved Instances

参考:references/best_practices.md → 数据库优化


服务替代方案决策指南

需要帮助选择服务吗?

问题:“我应该使用 EC2、Lambda 还是 Fargate?” 答案:参阅 references/service_alternatives.md → 计算替代方案

问题:“我应该使用哪种 S3 存储类别?” 答案:参阅 references/service_alternatives.md → 存储替代方案

问题:“我应该使用 RDS 还是 Aurora?” 答案:参阅 references/service_alternatives.md → 数据库替代方案

问题:“NAT Gateway、VPC Endpoint 还是 NAT Instance?” 答案:参阅 references/service_alternatives.md → 网络替代方案


FinOps 治理与流程

设置 FinOps

阶段 1:基础(第 1 个月)

  • 启用 Cost Explorer
  • 设置 AWS Budgets
  • 定义标签策略
  • 激活成本分配标签

阶段 2:可见性(第 2-3 个月)

  • 实施标签强制执行
  • 运行优化脚本
  • 设置月度审查
  • 创建团队成本报告

阶段 3:文化(持续进行)

  • 将成本指标纳入工程 KPI
  • 在架构决策中进行成本审查
  • 定期开展优化冲刺
  • 每个团队设立 FinOps 推动者

完整指南:参阅 references/finops_governance.md

月度审查流程

第 1 周:数据收集

  • 运行所有优化脚本
  • 导出 Cost & Usage Reports
  • 汇总调查结果

第 2 周:分析

  • 识别趋势
  • 寻找优化机会
  • 确定操作优先级

第 3 周:团队审查

  • 向工程团队汇报
  • 讨论优化方案
  • 分配待办事项

第 4 周:高管报告

  • 创建执行摘要
  • 预测下个季度
  • 汇报优化成果

模板:参阅 assets/templates/monthly_cost_report.md

详细流程:参阅 references/finops_governance.md → 月度审查流程


成本优化清单

快速见效(优先执行)
  • 删除未挂载的 EBS 卷
  • 删除旧的 EBS 快照(超过 90 天)
  • 释放未使用的 Elastic IP
  • 将 gp2 卷转换为 gp3
  • 停止/终止闲置的 EC2 实例
  • 启用 S3 Intelligent-Tiering
  • 设置 AWS Budgets 和提醒
中等工作量(本季度完成)
  • 为过度配置的实例调整规格
  • 迁移到较新的实例代际
  • 为稳定的工作负载购买 Reserved Instances
  • 实施 S3 生命周期策略
  • 在适用的情况下,将 NAT Gateways 替换为 VPC Endpoints
  • 启用自动资源调度(开发/测试)
  • 实施标签策略并强制执行
战略举措(进行中)
  • 迁移到 Graviton 实例
  • 为容错工作负载实施 Spot
  • 建立每月成本审查流程
  • 设置按团队进行的成本分摊
  • 实施成本分摊/成本展示模式
  • 培养 FinOps 文化并建立实践

成本问题排查

“我的账单突然增加了”
  1. 运行成本异常检测:

    python3 scripts/cost_anomaly_detector.py --days 30
    
  2. 在 Cost Explorer 中查看服务细分

  3. 在 CloudTrail 中检查资源创建事件

  4. 检查 AutoScaling 事件

  5. 确认预留实例没有到期

“我需要将成本降低 X%”

按照优化工作流操作:

  1. 运行所有发现脚本
  2. 计算潜在节省总额
  3. 按以下指标确定优先级:节省金额 × (1 / 工作量)
  4. 优先处理快速见效的项目
  5. 实施战略性变更以实现长期优化
“我怎么知道预留实例是否适用?”

运行 RI 分析:

python3 scripts/analyze_ri_recommendations.py --days 60

关注以下情况:

  • 实例持续运行 60 天以上
  • 不会发生变化的工作负载
  • 节省幅度超过 30%
“哪些资源可以安全删除?”

运行未使用资源查找工具:

python3 scripts/find_unused_resources.py

通常可以安全删除:

  • 未挂载的 EBS 卷(验证后)
  • 超过 90 天的快照(其他位置已有备份时)
  • 未使用的弹性 IP(验证其未用于 DNS 后)
  • 停止超过 30 天的 EC2 实例(确认已弃用后)

删除前务必与资源所有者确认!


最佳实践摘要

  1. 为所有内容添加标签:一致的标签有助于成本分摊和责任追踪
  2. 持续监控:每周运行脚本,及早发现浪费
  3. 每月审查:定期审查可防止成本偏离
  4. 主动调整规格:不要等到出现成本问题才进行优化
  5. 合理使用承诺计划:仅对稳定的工作负载使用 RI/SP
  6. 迁移前进行测试:尤其是迁移到 Graviton 或 Spot
  7. 自动清理:定时关闭开发/测试资源
  8. 分享成果:庆祝成本节省成果,培育 FinOps 文化

其他资源

详细参考资料:

  • references/best_practices.md:全面的优化策略
  • references/service_alternatives.md:经济高效的服务选择
  • references/finops_governance.md:组织层面的 FinOps 实践

模板:

  • assets/templates/monthly_cost_report.md:月度报告模板

脚本:

  • scripts/ 目录中的所有脚本均可使用 --help 查看用法

AWS 文档:

评论

支持 Markdown