AWS成本优化代理

AI驱动的AWS成本分析工具,通过自然语言查询提供成本分析、预测和优化建议。

  • by suresh-sre
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 suresh-subramanian2013/AWS-Cost-Optimization-Agent-using-MCP-Ollama-Local-Claude-Cloud

使用说明

🚀 AWS成本优化代理

基于MCP协议的人工智能成本分析

🖥️ 当地Ollama(免费) | ☁️ 克劳德/GPT云

用简单的英语询问有关AWS成本的问题,并获得AI支持的见解


🎯 这有什么作用?

询问有关AWS成本的自然语言问题:

💬 "What were my AWS costs last month?"
💬 "Which services cost the most?"
💬 "Are there any unusual spending patterns?"
💬 "How can I reduce my AWS costs?"

获得即时的人工智能答案,包括成本明细、预测和优化建议!


⚡ 快速入门(5分钟)

1.️⃣ 再进行

pip install -r requirements.txt

2.️⃣ 配置环境

cp .env.example .env
# Edit .env with your AWS credentials and LLM API key

3.️⃣ 启动MCP服务器

python aws_cost_mcp_server.py

4.️⃣ 提问!

python agent_orchestrator.py "What were my AWS costs last month?"

🏗️ 运作原理

graph LR
    A[👤 You Ask Question] --> B[🤖 AI Agent]
    B --> C{Choose LLM}
    C -->|FREE| D[🖥️ Ollama Local]
    C -->|Paid| E[☁️ GPT/Claude]
    B --> F[🔧 MCP Server]
    F --> G[☁️ AWS Cost API]
    G --> F
    F --> B
    D --> B
    E --> B
    B --> H[📊 Cost Report]
    
    style D fill:#90EE90
    style E fill:#87CEEB
    style G fill:#FFB366

🖥️ 什么在哪里跑?

组件 位置 成本
MCP服务器 您的计算机 免费
代理 您的计算机 免费
LLM(Ollama) 你的电脑 自由 ✨
LLM(GPT/Claude) 云 ~0.01美元/查询
AWS成本数据 AWS云 免费

🌟 主要特点

🆓 成本选项

  • 自由:使用当地Olama(Qwen)
  • 已支付:OpenAI GPT或Claude
  • 灵活的:随时切换

🔧 5强大的工具

  • 成本和使用数据
  • 成本预测
  • 异常检测
  • 服务故障
  • 优化提示

🤖 AI驱动

  • 自然语言查询
  • 智能分析
  • 人类可读的报告
  • 多LLM支持

🔒 隐私与控制

  • 在您的机器上运行
  • 本地LLM选项
  • 无数据共享(与Ollama)
  • 完全控制

💡 部署选项

选项1:完全本地(免费)🖥️

# Install Ollama
# Download from https://ollama.ai/

# Pull model (one-time, 4GB)
ollama pull qwen2.5:latest

# Start Ollama
ollama serve

✅ 赞成的意见: 免费、私人、离线(AWS呼叫除外)
❌ 欺骗: 需要8GB RAM,4GB下载

选项2:云LLM(简单)☁️

OPENAI_API_KEY=sk-proj-xxxxx
DEFAULT_LLM_PROVIDER=openai

✅ 赞成的意见: 质量更好,设置简单,速度更快
❌ 欺骗: ~5-20美元/月,需要上网


📖 用法示例

基本成本查询

from agent_orchestrator import AgentOrchestrator

agent = AgentOrchestrator()
response = agent.process_query("What were my total AWS costs for the last 30 days?")
print(response)
agent.close()

服务故障

python agent_orchestrator.py "Which AWS services cost the most?"

成本预测

python agent_orchestrator.py "What will my costs be next month?"

综合报告

python agent_orchestrator.py "Generate a full cost report with optimization recommendations"

🧪 测试

# Run all tests
pytest tests/

# Run with coverage
pytest tests/ --cov=.

# Run examples
python example_usage.py

🔐 AWS设置

所需IAM权限

{
  "Version": "2012-10-17",
  "Statement": [{
    "Effect": "Allow",
    "Action": [
      "ce:GetCostAndUsage",
      "ce:GetCostForecast",
      "ce:GetAnomalies",
      "ce:GetRightsizingRecommendation",
      "ce:GetSavingsPlansPurchaseRecommendation"
    ],
    "Resource": "*"
  }]
}

启用成本资源管理器

  1. 转到AWS控制台→ 成本管理
  2. 启用成本资源管理器
  3. 等待24小时以获取数据

📁 项目结构

sre-project-3/
├── agent_orchestrator.py    # AI agent with LLM
├── aws_cost_mcp_server.py   # MCP server (Flask)
├── mcp_client.py             # MCP client
├── aws_cost_service.py       # AWS API wrapper
├── config.py                 # Configuration
├── requirements.txt          # Dependencies
├── .env.example              # Config template
├── example_usage.py          # Examples
└── tests/                    # Test suite

🔍 故障排除

MCP Server won't start
  • 检查端口8080是否未使用
  • 验证是否已安装Python 3.8+
  • 检查 .env 文件存在
AWS authentication errors
  • 在中验证AWS凭据 .env
  • 检查IAM权限
  • 确保启用了成本资源管理器
Ollama connection issues
# Ensure Ollama is running
ollama serve

# Verify model is installed
ollama list

# Pull model if needed
ollama pull qwen2.5:latest

📚 文档


🤝 贡献

欢迎投稿!请随时提交拉取请求。


📄 许可证

MIT许可证-请参阅 许可证 详细信息文件


制作❤️ AWS成本优化

⭐ 如果你觉得这个仓库有用,就把它标上!