Cost Monitoring: ติดตาม Cloud Spending บน AWS/GCP

Cloud Spending เป็นหนึ่งในค่าใช้จ่ายที่ควบคุมได้ยากที่สุดของระบบ IT สมัยใหม่ เพราะ resource ถูกสร้างและลบได้ตลอดเวลาโดยทีมต่าง ๆ ทำให้ค่าใช้จ่ายพุ่งขึ้นโดยไม่รู้ตัว การติดตามค่าใช้จ่ายอย่างเป็นระบบจึงจำเป็น ทั้งเพื่อควบคุมงบประมาณและหา opportunity ในการ optimize — ทีมที่ไม่ monitor cost มักพบบิลเกินคาดในสิ้นเดือนโดยไม่ทราบสาเหตุ

บทความนี้จะอธิบายแนวคิดและเครื่องมือในการติดตามค่าใช้จ่าย Cloud บน AWS และ GCP ตั้งแต่การอ่าน billing data, การตั้ง budget alert, การใช้ Allocation Tag, การสร้าง dashboard ด้วย Grafana และการผนวก cost data เข้ากับระบบ Monitoring กลางเพื่อให้ทุกทีมมองเห็นค่าใช้จ่ายของตัวเองและรับผิดชอบได้

ทำไมการติดตามค่าใช้จ่าย Cloud จึงสำคัญ

หลายองค์กรที่ย้ายระบบขึ้น cloud พบว่าค่าใช้จ่ายสูงกว่าที่คาดไว้ 30-50% เนื่องจาก resource ที่ลืมปิด, instance ที่ size เกินความจำเป็น, egress traffic ที่ไม่ได้วางแผน, หรือ storage snapshot ที่สะสม — ปัญหาเหล่านี้แก้ได้เพียงเมื่อมีการ monitor ค่าใช้จ่ายอย่างต่อเนื่อง ไม่ใช่รอเห็นบิลสิ้นเดือน

ประโยชน์ของการ monitor ค่าใช้จ่าย

  • Early Warning — จับการใช้จ่ายผิดปกติได้ก่อนจะบานปลายถึงสิ้นเดือน
  • Accountability — ทีมแต่ละทีมรู้ว่าตัวเองใช้เท่าไหร่ ทำให้รับผิดชอบการใช้จ่ายได้
  • Optimization — หา resource ที่ใช้ไม่เต็มประสิทธิภาพเพื่อลดค่าใช้จ่ายโดยไม่กระทบงาน
  • Forecasting — คาดการณ์ค่าใช้จ่ายเดือนถัดไปได้แม่นยำขึ้น
  • Chargeback/Showback — แบ่งค่าใช้จ่ายตามทีมหรือโปรเจกต์สำหรับการคิดต้นทุนภายใน

FinOps: กรอบคิดในการจัดการ Cloud Spending

FinOps คือการผนวก Finance + DevOps + Business เพื่อให้ทีม engineering, finance, product ทำงานร่วมกันในการจัดการค่าใช้จ่าย cloud อย่างมีประสิทธิภาพ โดย FinOps Foundation แบ่งการทำงานเป็น 3 phase:

Phaseเป้าหมายกิจกรรม
Informมองเห็นค่าใช้จ่ายรวบรวม billing data, สร้าง dashboard, tag resource, จัดสรรค่าใช้จ่าย
Optimizeลดค่าใช้จ่ายที่ไม่จำเป็นRight-size, Reserved Instance, Spot, ลบ unused resource
Operateทำงานต่อเนื่องBudget alert, KPI, policy, automation

Cloud Spending บน AWS

AWS มีเครื่องมือจัดการค่าใช้จ่ายครบวงจร — ตั้งแต่ Cost Explorer สำหรับการ visualize, AWS Budgets สำหรับ alert, Cost and Usage Report (CUR) สำหรับข้อมูลละเอียด ไปจนถึง AWS Compute Optimizer สำหรับคำแนะนำ optimization

AWS Cost Explorer (เครื่องมือ visualize)

Cost Explorer เป็น UI สำหรับ explore billing data ย้อนหลังได้ถึง 13 เดือน สามารถแบ่งตาม service, region, account, tag, usage type โดยรองรับ forecast ค่าใช้จ่ายในอนาคต 12 เดือน — เข้าถึงผ่าน AWS Console หรือผ่าน GetCostAndUsage API

# ดึงข้อมูล cost ผ่าน AWS CLI
aws ce get-cost-and-usage \
  --time-period Start=2026-03-01,End=2026-04-01 \
  --granularity MONTHLY \
  --metrics "UnblendedCost" "UsageQuantity" \
  --group-by Type=DIMENSION,Key=SERVICE

# แบ่งตาม tag (ต้อง activate tag ก่อน)
aws ce get-cost-and-usage \
  --time-period Start=2026-04-01,End=2026-04-18 \
  --granularity DAILY \
  --metrics "UnblendedCost" \
  --group-by Type=TAG,Key=Team

AWS Budgets

AWS Budgets ใช้ตั้ง budget และ alert เมื่อใช้จ่ายใกล้หรือเกิน threshold — รองรับ budget ประเภท Spend, Usage, Reservation Coverage, Savings Plans Coverage โดย alert ส่งได้ทาง Email, SNS, Slack (ผ่าน Lambda)

# ตัวอย่าง budget.json
{
  "BudgetName": "MonthlyProdBudget",
  "BudgetLimit": {"Amount": "10000", "Unit": "USD"},
  "TimeUnit": "MONTHLY",
  "BudgetType": "COST",
  "CostFilters": {
    "TagKeyValue": ["user:Environment$production"]
  }
}

# สร้าง budget ด้วย CLI
aws budgets create-budget \
  --account-id 123456789012 \
  --budget file://budget.json \
  --notifications-with-subscribers file://notifications.json

Cost and Usage Report (CUR)

CUR คือ dataset ละเอียดที่สุดของ AWS billing มีข้อมูลระดับ resource-hour พร้อม tag ทั้งหมด สามารถ export ลง S3 ในรูปแบบ Parquet/CSV แล้วใช้ Athena หรือ Redshift query ข้อมูลได้ — เหมาะสำหรับการวิเคราะห์ขนาดใหญ่ที่ UI สำเร็จรูปทำไม่ได้

# Query CUR ผ่าน Athena
SELECT
  line_item_product_code AS service,
  resource_tags_user_team AS team,
  SUM(line_item_unblended_cost) AS cost_usd
FROM aws_cur
WHERE year = '2026' AND month = '4'
  AND line_item_usage_start_date >= DATE '2026-04-01'
GROUP BY 1, 2
ORDER BY cost_usd DESC
LIMIT 20;

AWS Compute Optimizer

Compute Optimizer วิเคราะห์ CloudWatch metric ของ EC2, EBS, Lambda, Auto Scaling Group แล้วแนะนำการ right-size ตาม workload จริง — คำแนะนำมีทั้ง Under-provisioned, Over-provisioned, Optimized พร้อมประมาณการ cost saving

Cloud Spending บน GCP

GCP ใช้เครื่องมือชื่อ Cloud Billing และมี BigQuery Export สำหรับข้อมูลละเอียด — การ monitor ค่าใช้จ่ายทำผ่าน Billing Console, Budgets, Cost Breakdown Report และ BigQuery dataset

Billing Export to BigQuery

การ export billing data ไปยัง BigQuery ทำให้ query ข้อมูลด้วย SQL ได้ มีข้อมูล 2 แบบ — Standard usage cost (ทุก line item) และ Detailed usage cost (ระดับ resource) ควรเปิด Detailed usage cost ถ้าต้องการ granularity สูงสุด

-- Cost แบ่งตาม service และ label
SELECT
  service.description AS service,
  (SELECT value FROM UNNEST(labels) WHERE key = 'team') AS team,
  SUM(cost) AS total_cost,
  currency
FROM `billing_export_dataset.gcp_billing_export_v1_XXXXXX_XXXXXX_XXXXXX`
WHERE DATE(_PARTITIONTIME) BETWEEN "2026-04-01" AND "2026-04-18"
  AND project.id = "production-project"
GROUP BY service, team, currency
ORDER BY total_cost DESC;

GCP Budgets

GCP Budgets ตั้งค่าได้ต่อ Billing Account หรือ Project รองรับ threshold ทั้งแบบ percentage และ absolute amount — notification ส่งผ่าน Pub/Sub ได้ ทำให้เชื่อมต่อกับ Cloud Functions สำหรับ automation เช่น stop instance เมื่อใช้งบเกิน

gcloud billing budgets create \
  --billing-account=012345-6789AB-CDEF01 \
  --display-name="Production Monthly Budget" \
  --budget-amount=10000USD \
  --threshold-rule=percent=0.5 \
  --threshold-rule=percent=0.9 \
  --threshold-rule=percent=1.0 \
  --filter-projects=projects/prod-project \
  --all-updates-rule-pubsub-topic=projects/prod-project/topics/budget-alerts

Recommender (Active Assist)

GCP Recommender ให้คำแนะนำอัตโนมัติเกี่ยวกับการ optimize ทั้ง VM right-sizing, idle VM, committed use discount, unattached disk ฯลฯ — เข้าถึงผ่าน gcloud หรือ UI หน้า Recommendations

# ดู VM sizing recommendation
gcloud recommender recommendations list \
  --project=prod-project \
  --location=us-central1-a \
  --recommender=google.compute.instance.MachineTypeRecommender \
  --format="table(recommendation, content.operationGroups[0].operations[0].resource)"

Allocation Tag และ Label

การ tag resource ทุกตัวเป็นสิ่งที่ควรทำตั้งแต่เริ่มต้นใช้ cloud — tag คือ key-value metadata ที่ใช้แบ่งค่าใช้จ่ายตามทีม, โปรเจกต์, environment, application หากไม่มี tag จะแยกค่าใช้จ่ายไม่ได้เลย

Tag Policy ที่แนะนำ

  • Environment — production, staging, dev, sandbox
  • Team — ทีมที่เป็นเจ้าของ resource
  • Project / Application — ชื่อโปรเจกต์หรือ application
  • CostCenter — รหัสศูนย์ต้นทุนสำหรับการคิดต้นทุนภายใน
  • Owner — email ของเจ้าของ resource สำหรับติดต่อ
  • ManagedBy — terraform, cloudformation, manual

บังคับใช้ Tag ด้วย Policy

การบังคับให้ทุก resource มี tag ทำได้หลายวิธี — AWS ใช้ Service Control Policy (SCP) หรือ Tag Policy, GCP ใช้ Organization Policy พร้อม Label Constraint, หรือใช้ IaC (Terraform/Pulumi) ที่กำหนด default tag ทุก resource

# Terraform: บังคับ default tag ทุก AWS resource
provider "aws" {
  region = "us-east-1"

  default_tags {
    tags = {
      Environment = "production"
      Team        = "platform"
      ManagedBy   = "terraform"
      Repo        = "github.com/org/infra"
    }
  }
}

รวมข้อมูลค่าใช้จ่ายเข้ากับ Grafana

การดูค่าใช้จ่ายใน console ของ cloud provider อย่างเดียวทำให้ทีม engineering ไม่เห็นข้อมูลร่วมกับ metric อื่น ๆ — การนำข้อมูล spending เข้า Grafana dashboard ที่รวมกับ performance metric ทำให้เห็นภาพรวมและตัดสินใจ optimize ได้ง่ายขึ้น

วิธีดึงข้อมูลค่าใช้จ่ายเข้า Prometheus

  • Custom Exporter — เขียน exporter เล็ก ๆ ที่เรียก API Cost Explorer/Billing แล้ว expose เป็น Prometheus metric
  • CloudQuery + Prometheus — sync billing data ไปยัง database แล้วให้ exporter query
  • OpenCost — open source project ที่รองรับ Kubernetes spending monitoring ส่งเป็น Prometheus metric
  • Vector/Fluentbit — ดึง CUR หรือ BigQuery export แล้วส่งเข้า Loki/Prometheus

ตัวอย่าง Prometheus Metric ที่ควรมี

# Gauge: cost ต่อวันต่อ service
cloud_cost_daily_usd{service="ec2", account="prod", team="platform"} 1234.56

# Gauge: budget utilization (%)
cloud_budget_usage_ratio{budget="prod-monthly"} 0.73

# Counter: cost สะสมต่อเดือน
cloud_cost_month_to_date_usd{project="prod-project"} 8540.22

# Gauge: idle resource count
cloud_idle_resource_count{type="ec2", criteria="cpu_lt_5_percent_7d"} 12

Alert Rule สำหรับค่าใช้จ่าย

groups:
- name: cost.alerts
  rules:
  - alert: BudgetNearExceeded
    expr: cloud_budget_usage_ratio > 0.85
    for: 10m
    labels:
      severity: warning
    annotations:
      summary: "Budget {{ $labels.budget }} ใช้ไป {{ $value }}% แล้ว"

  - alert: DailyCostSpike
    expr: >
      cloud_cost_daily_usd
      > (avg_over_time(cloud_cost_daily_usd[14d]) * 1.5)
    for: 30m
    labels:
      severity: critical
    annotations:
      summary: "{{ $labels.service }} cost เพิ่มเกิน 50% เทียบ 14 วันที่ผ่านมา"

  - alert: IdleResourceGrowing
    expr: cloud_idle_resource_count > 20
    for: 1h
    labels:
      severity: info
    annotations:
      summary: "พบ idle resource {{ $value }} ตัว ที่ควรลบหรือ right-size"

เครื่องมือ Third-Party

นอกจากเครื่องมือของ cloud provider ตรง ๆ แล้ว ยังมี third-party tool ที่ช่วยรวมการจัดการ multi-cloud และเพิ่มความสามารถ optimization ให้ครอบคลุมขึ้น:

เครื่องมือLicenseจุดเด่น
OpenCostOpen SourceKubernetes spending allocation, namespace level
KubecostFreemiumBuilt on OpenCost + UI, alert, savings recommendation
CloudabilityCommercialMulti-cloud enterprise, governance, showback
CloudHealthCommercialMulti-cloud, policy, RI optimization
InfracostOpen Sourceประมาณค่าใช้จ่ายใน Pull Request ของ Terraform
Spot.ioCommercialAutomation การใช้ Spot Instance

Best Practices

  • Tag ตั้งแต่เริ่ม — การเพิ่ม tag ย้อนหลังยากกว่าการบังคับตั้งแต่เริ่มมาก ใช้ IaC ช่วย
  • ตั้ง Budget Alert หลายระดับ — 50%, 80%, 100% ทำให้มีเวลาตอบสนองก่อนเกินงบ
  • Review Weekly — ทีมควร review dashboard ค่าใช้จ่ายสัปดาห์ละครั้ง ไม่ใช่รอสิ้นเดือน
  • ใช้ Anomaly Detection — ระบบตรวจจับความผิดปกติของ AWS/GCP พร้อม ML จับ spike ได้โดยอัตโนมัติ
  • แสดงค่าใช้จ่ายให้ทีมเห็น — dashboard แยกตามทีม ทำให้แต่ละทีมเห็นและรับผิดชอบการใช้จ่ายของตัวเอง
  • Automate การลบ Resource ไม่ใช้ — ใช้ Lambda/Cloud Functions ลบ resource ที่ tag ไว้ว่าเป็น temporary อัตโนมัติ
  • Reserved/Committed Use — resource ที่ใช้งาน predictable ควรใช้ RI/CUD เพื่อลดค่าใช้จ่าย 30-70%
  • Review ทุกไตรมาส — review architecture ว่ามี opportunity เปลี่ยน service เช่น จาก EC2 เป็น Fargate หรือ GCE เป็น Cloud Run

สรุป

การ monitor ค่าใช้จ่ายไม่ใช่งานของทีม finance อย่างเดียว แต่เป็นความรับผิดชอบของทีม engineering ด้วย — เพราะ resource ถูกสร้างและใช้งานโดย engineer การผสมผสานเครื่องมือ native ของ cloud provider เช่น AWS Cost Explorer, Budgets, CUR และ GCP Billing Export, Budgets, Recommender เข้ากับ dashboard กลางเช่น Grafana ทำให้ทุกทีมมีข้อมูลเพียงพอในการตัดสินใจ

การทำ FinOps อย่างเป็นระบบ — tag resource ตั้งแต่เริ่ม, ตั้ง budget alert หลายระดับ, review รายสัปดาห์, และ automate การลบ resource ไม่ใช้ — จะช่วยลด cloud spending ได้ 20-40% โดยไม่กระทบประสิทธิภาพของระบบ พร้อมสร้างวัฒนธรรมการใช้ cloud อย่างรับผิดชอบ