用 Amazon Quick + AgentCore 搭对话式 FinOps 助手:自然语言查 AWS 账单

每周 FinOps 团队都会收到同一类问题:"上个月哪个服务花得多?""这个月 EC2 费用比上月涨了多少?""哪几个账号用量异常?"数据明明在 Cost Explorer 里,但每次都要人肉查、截图、发邮件。

我试了一下用 Amazon Quick 的 Chat Agent 接 AgentCore 里跑的 Billing MCP Server,让业务方直接用中文问账单——"上个月按服务分组费用多少?",Agent 自己去调 Cost Explorer API 返回结果。整个搭建过程记录如下。

方案架构

三层结构:

用户(Amazon Quick Chat)
  → Amazon Bedrock AgentCore Runtime(托管 MCP Server)
  → AWS Billing & Cost Management API
  • Amazon Quick:提供对话界面,内置 Chat Agent 能力
  • AgentCore Runtime:Serverless 托管 MCP Server,不用自己管服务器
  • MCP Server:把 Cost Explorer、Budgets、Compute Optimizer 等 API 封装成 MCP 工具

用户在 Quick 里用自然语言提问,Chat Agent 调用 AgentCore 里的 MCP 工具,工具去查实际账单数据,返回结果。

前置条件

开始之前确认这几个东西:

  1. AWS 账号且有 Cost Explorer 访问权限(需要在管理账号或有 CE 权限的成员账号)
  2. Amazon Bedrock AgentCore 在目标 region 可用
  3. Amazon Quick 已在组织中启用
  4. Amazon Cognito 用户池(AgentCore Gateway 需要 OAuth 鉴权)
  5. Node.js 18+(MCP Server 开发和部署用)

第一步:准备 Billing MCP Server

AWS 在 GitHub 开源了一个 Billing MCP Server 的参考实现。我基于它做了些改造让它适配 AgentCore Runtime。

核心 tools 定义:

// tools/get-cost-and-usage.ts
export const getCostAndUsage = {
  name: "get_cost_and_usage",
  description: "查询指定时间范围内按服务/账号/标签分组的 AWS 费用",
  inputSchema: {
    type: "object",
    properties: {
      startDate: { type: "string", description: "开始日期 YYYY-MM-DD" },
      endDate: { type: "string", description: "结束日期 YYYY-MM-DD" },
      groupBy: { 
        type: "string", 
        enum: ["SERVICE", "LINKED_ACCOUNT", "TAG"],
        description: "分组维度" 
      },
      granularity: {
        type: "string",
        enum: ["DAILY", "MONTHLY"],
        description: "粒度"
      }
    },
    required: ["startDate", "endDate"]
  }
};

实际调用 AWS SDK:

import { CostExplorerClient, GetCostAndUsageCommand } from "@aws-sdk/client-cost-explorer";

const ce = new CostExplorerClient({ region: "us-east-1" });

async function handler(input: any) {
  const cmd = new GetCostAndUsageCommand({
    TimePeriod: { Start: input.startDate, End: input.endDate },
    Granularity: input.granularity || "MONTHLY",
    Metrics: ["UnblendedCost"],
    GroupBy: input.groupBy 
      ? [{ Type: "DIMENSION", Key: input.groupBy }] 
      : undefined
  });
  const resp = await ce.send(cmd);
  return resp.ResultsByTime;
}

类似的,还可以封装:

  • get_budget_status:查 Budgets 的当前消耗 vs 预算
  • get_savings_recommendations:Compute Optimizer 的优化建议
  • get_anomaly_alerts:费用异常检测告警

第二步:部署到 AgentCore Runtime

AgentCore Runtime 以 Serverless 方式托管 MCP Server。部署流程:

# 1. 初始化 AgentCore 项目
agentcore init --runtime mcp --name billing-assistant

# 2. 把 MCP Server 代码放进去
cp -r src/ agentcore-project/src/

# 3. 配置 agentcore.yaml
cat > agentcore.yaml << 'EOF'
name: billing-assistant
runtime: mcp
entry: src/index.ts
tools:
  - get_cost_and_usage
  - get_budget_status
  - get_savings_recommendations
auth:
  type: cognito
  userPoolId: us-east-1_xxxxxxx
  clientId: xxxxxxxxxxxxxxxxxxxxxxxxx
permissions:
  - ce:GetCostAndUsage
  - ce:GetCostForecast
  - budgets:ViewBudget
  - compute-optimizer:GetRecommendationSummaries
EOF

# 4. 部署
agentcore deploy --region us-east-1

部署成功后会返回一个 Gateway Endpoint URL,后面 Amazon Quick 要用。

坑 1:IAM 权限。 AgentCore Runtime 的执行角色需要有 Cost Explorer 的读取权限。Cost Explorer API 只在 us-east-1 可用(全局服务),别配错 region。

坑 2:Cognito 配置。 AgentCore Gateway 强制要求 OAuth 认证。需要先建 Cognito User Pool + App Client,设置好回调 URL。

第三步:接入 Amazon Quick Chat Agent

在 Amazon Quick 管理后台:

  1. 进入 Chat Agent 配置
  2. 添加数据源 → 选择 "AgentCore MCP Endpoint"
  3. 填入上一步拿到的 Gateway URL
  4. 配置 OAuth credentials(Cognito Client ID + Secret)
  5. 测试连接

连通后,在 Quick 的对话框里就可以直接问:

  • "上个月 EC2 服务花了多少钱?"
  • "最近三个月费用趋势怎么样?"
  • "哪些账号这个月费用增长超过 20%?"
  • "Compute Optimizer 有什么优化建议?"

实际效果

我测试了几轮问答:

问:上个月按服务分组费用是多少?

Agent 自动调用 get_cost_and_usage,传入上月日期范围 + groupBy: SERVICE,返回格式化的表格结果。

问:这个月 EC2 比上个月多了多少?

Agent 分别调两次 cost API(本月和上月),自己做差值计算返回百分比。

问:有没有省钱的建议?

Agent 调用 get_savings_recommendations 拉 Compute Optimizer 数据,返回可优化的实例列表。

响应时间大概 3-5 秒(Cost Explorer API 本身就慢),但比人肉去控制台查快多了。

安全考虑

几个关键点:

  1. 最小权限——MCP Server 的 IAM 角色只给 CE/Budgets 的只读权限,不能改配置
  2. Cognito 鉴权——不是随便谁都能调 Gateway,必须通过 OAuth 流程
  3. 数据范围——通过 IAM Policy 的 Condition 限制只能查特定 Organization Unit 的数据
  4. 审计日志——所有 AgentCore 调用都进 CloudTrail,谁问了什么一目了然

适合什么场景

这套方案比较适合:

  • FinOps 团队日常答疑——业务方自助查账单
  • 管理层月度汇报——快速拉数据做总结
  • 异常发现——配合 Cost Anomaly Detection 做主动告警
  • 多账号管理——Organizations 下几十个账号的统一查询

不太适合:

  • 需要精细 RI/SP 管理的场景(操作类,不只是查询)
  • 数据量极大的细粒度分析(用 Cost and Usage Report + Athena 更合适)

小结

整个搭建流程:

  1. 封装 Billing API 为 MCP Tools
  2. 部署到 AgentCore Runtime(Serverless 托管)
  3. Amazon Quick Chat Agent 接入 AgentCore Gateway
  4. Cognito 做 OAuth 鉴权

从"有想法"到"能问答"大概一天时间。核心是 MCP 把 API 变成工具、AgentCore 做 Serverless 托管、Quick 给对话界面,三层各司其职。

如果你的 FinOps 团队每周都在重复回答"上月花了多少钱"这类问题,可以试试这个方案——让 Agent 去查,人去做决策。


参考资料:

posted @ 2026-06-15 11:30  亚马逊云开发者  阅读(1)  评论(0)    收藏  举报