AWS Budgets + Cost Explorer — コスト管理

概要

AWS Budgets は予算を設定して「閾値を超えたら通知する」サービス。 AWS Cost Explorer は「実際にいくら使ったか」をサービス別・タグ別・期間別に可視化する分析ツール。 この 2 つを組み合わせるのが AWS のコスト管理基本セットです。

地味だが採用評価で「個人 / 中小企業の AWS 案件をやる人ですか」を判定する重要シグナル。 AWS は使った分だけ課金される(しかも単価がリージョン・サービス毎に複雑)ので、 「気付いたら今月の請求が予想の 5 倍」事故を防ぐ責任が運用者にあります。

主な機能

AWS Budgets

AWS Cost Explorer

使うべきパターン

使わない方が良いパターン (≒ 設定しない言い訳)

Terraform 最小サンプル(月予算 + Email 通知)

resource "aws_sns_topic" "billing" {
  name = "billing-alerts"
}

resource "aws_sns_topic_subscription" "email" {
  topic_arn = aws_sns_topic.billing.arn
  protocol  = "email"
  endpoint  = "contact@iigtn.com"
}

# 月 1万円 の予算
resource "aws_budgets_budget" "monthly" {
  name         = "monthly-cost-budget"
  budget_type  = "COST"
  limit_amount = "10000"
  limit_unit   = "JPY"
  time_unit    = "MONTHLY"

  cost_filter {
    name   = "Service"
    values = ["Amazon Elastic Compute Cloud - Compute", "Amazon Simple Storage Service"]
  }

  notification {
    comparison_operator        = "GREATER_THAN"
    threshold                  = 50
    threshold_type             = "PERCENTAGE"
    notification_type          = "ACTUAL"
    subscriber_email_addresses = ["contact@iigtn.com"]
  }
  notification {
    comparison_operator        = "GREATER_THAN"
    threshold                  = 80
    threshold_type             = "PERCENTAGE"
    notification_type          = "ACTUAL"
    subscriber_email_addresses = ["contact@iigtn.com"]
  }
  notification {
    comparison_operator        = "GREATER_THAN"
    threshold                  = 100
    threshold_type             = "PERCENTAGE"
    notification_type          = "FORECASTED"   # 予測でも通知
    subscriber_email_addresses = ["contact@iigtn.com"]
  }
}

# Cost Anomaly Detection
resource "aws_ce_anomaly_monitor" "main" {
  name              = "service-monitor"
  monitor_type      = "DIMENSIONAL"
  monitor_dimension = "SERVICE"
}

resource "aws_ce_anomaly_subscription" "main" {
  name      = "anomaly-alert"
  frequency = "DAILY"
  monitor_arn_list = [aws_ce_anomaly_monitor.main.arn]
  subscriber {
    type    = "EMAIL"
    address = "contact@iigtn.com"
  }
  threshold_expression {
    dimension {
      key           = "ANOMALY_TOTAL_IMPACT_ABSOLUTE"
      values        = ["1000"]   # 1000 円超の異常で通知
      match_options = ["GREATER_THAN_OR_EQUAL"]
    }
  }
}

コスト最適化の基本パターン

用語集

AWS Budgets
予算設定 + 閾値通知サービス。Cost / Usage / RI / Savings Plans の各種類あり。
AWS Cost Explorer
実費を可視化する分析ツール。サービス / タグ / 期間で集計、Forecast 機能も。
Cost Allocation Tags
Project / Env / Owner 等のカスタムタグでコストを集計する仕組み。
Cost Anomaly Detection
機械学習で「予想外の課金増加」を自動検知する機能。閾値ベース通知。
Reserved Instances (RI)
1〜3 年コミットで EC2/RDS 等を最大 72% 割引で使える購入オプション。
Savings Plans
RI の柔軟版。インスタンス種類を限定せず時間単位の支払額をコミット。
Forecasted Notification
「このまま行くと予算超過する」予測ベースの通知。早期警告に有効。
Budgets Action
予算超過時に IAM Policy 切替や EC2 停止等を自動実行する機能。dev 環境向け。

シリーズ完結

これで「AWS 基本サービス入門」シリーズ全 15 記事が完結しました。 個別サービスの詳細は 現場コマンドリファレンス で、 実際の構築過程は 構築日誌 で深掘りしています。あわせてご覧ください。