💰

Claude Code Action + AWS Cost Explorer MCP で AWS 料金分析

に公開

はじめに

READYFOR では、AI Agent の活用を進めており、Claude Code Action で何かできることはないかと模索していました。
その中で、インフラチームが抱えていた「AWS 料金の定期確認作業」を、Claude Code Action と AWS Cost Explorer MCP を組み合わせれば、GitHub の issue から簡単に AWS 料金分析ができるのでは?と思い、試してみました。

このスクショのように、GitHub の issue で Claude にメンションするだけで料金分析ができるようになったので、その仕組みを紹介します。

実装の流れ

1. Claude の GitHub App をインストール

https://github.com/apps/claude から Claude GitHub Appをインストールし、 Claude Code Action を動作させるリポジトリのアクセス権限を設定します。

2. Anthropic API キーの設定

Anthropic API キーを GitHub Secrets に設定します。リポジトリの設定から ANTHROPIC_API_KEY という名前で追加しています。

3. MCP サーバーの設定

.mcp.json に AWS Cost Explorer MCP Server の設定を追加

{
  "mcpServers": {
    "core-mcp-server": {
      "command": "uvx",
      "args": ["awslabs.core-mcp-server@latest"]
    },
    "cost-explorer-mcp-server": {
      "command": "uvx",
      "args": ["awslabs.cost-explorer-mcp-server@latest"]
    }
  }
}

4. GitHub Actions の設定

.github/workflows/claude.yml に Claude Code Action の設定を追加

name: Claude Code

on:
  issue_comment:
    types: [created]
  pull_request_review_comment:
    types: [created]
  issues:
    types: [opened, assigned]
  pull_request_review:
    types: [submitted]

jobs:
  claude:
    if: |
      (github.event_name == 'issue_comment' && contains(github.event.comment.body, '@claude')) ||
      (github.event_name == 'pull_request_review_comment' && contains(github.event.comment.body, '@claude')) ||
      (github.event_name == 'pull_request_review' && contains(github.event.review.body, '@claude')) ||
      (github.event_name == 'issues' && (contains(github.event.issue.body, '@claude') || contains(github.event.issue.title, '@claude')))
    runs-on: ubuntu-latest
    permissions:
      contents: read
      pull-requests: read
      issues: read
      id-token: write
    steps:
      - name: Checkout repository
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
        with:
          fetch-depth: 1

      - name: Set up uv
        uses: astral-sh/setup-uv@bd01e18f51369d5a26f1651c3cb451d3417e3bba # v6.3.1
        with:
          enable-cache: true

      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@b47578312673ae6fa5b5096b330d9fbac3d116df # v4.2.1
        with:
          role-to-assume: arn:aws:iam::000000000000:role/read-only-role
          aws-region: ap-northeast-1

      - name: Run Claude Code
        id: claude
        uses: anthropics/claude-code-action@00f9595fb44d49fdc15049286d89247d29a08f2b # v0.0.27
        with:
          anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
          allowed_tools: |
            mcp__core-mcp-server__prompt_understanding
            mcp__cost-explorer-mcp-server__get_today_date
            mcp__cost-explorer-mcp-server__get_dimension_values
            mcp__cost-explorer-mcp-server__get_tag_values
            mcp__cost-explorer-mcp-server__get_cost_forecast
            mcp__cost-explorer-mcp-server__get_cost_and_usage_comparisons
            mcp__cost-explorer-mcp-server__get_cost_comparison_drivers
            mcp__cost-explorer-mcp-server__get_cost_and_usage

実際の使い方

設定が完了すると、GitHub の issue で以下のようにコメントするだけで自動分析が始まります

@claude 今月のAWS料金のサマリを教えて
@claude 5月と6月の料金比較をしてみて

実際の分析結果例

6月の料金サマリ(実例)

5月→6月の変動分析

6月に OpenSearch のリプレイスをしたのですが、その結果料金が削減されていることを確認できました。

運用での活用方法

1. 属人性の排除

今まではインフラエンジニアに確認する必要がありましたが、リポジトリのアクセス権限があれば誰でも料金確認ができます。

2. 異常検知

「今日のAWS料金が異常に高い気がする」といった質問に対して、即座に当日の料金分析を実行できます。

3. コスト最適化の検討

サービス別の料金内訳から、最適化対象の特定と効果測定が簡単に行えます。

今後の改善案

次は 複数の AWS アカウント対応や、Google Cloud の料金分析、 Terraform MCP も入れてリファクタリング作業なども取り組んでいきたいです。

参考資料

READYFORテックブログ

Discussion