</>Học Dev
Bài học

Tuần 10 - Ngày 1: Cost Explorer và AWS Budgets

Tuần 10 – Ngày 1

Tuần 10 - Ngày 1: Cost Explorer và AWS Budgets

Mục tiêu học tập

  • Phân tích chi phí với Cost Explorer
  • Setup AWS Budgets với alerts
  • Sử dụng Cost Anomaly Detection
  • Apply Cost Allocation Tags

1. AWS Cost Management Suite

Tổng quan các tools

  • AWS Billing Console: hóa đơn, thanh toán
  • Cost Explorer: visualize + phân tích chi phí
  • AWS Budgets: set alerts về cost/usage
  • Cost Anomaly Detection: ML detect anomalies
  • AWS Cost Categories: group costs by custom logic
  • AWS Pricing Calculator: estimate before deploy
  • Cost & Usage Report (CUR): detailed CSV/Parquet

2. Cost Explorer (Recap + Advanced)

Visualize options

  • Granularity: Hourly (14 days history), Daily (38 months), Monthly (38 months)
  • Group by: Service, Region, Account, Tag, Usage Type, Instance Type, Linked Account, Charge Type, Purchase Option
  • Filter: similar dimensions
  • Forecast: predict next 1-12 months based on past

Reports

  • Daily, Monthly, Year-over-Year
  • Save filter as Custom Report
  • Schedule email reports (Cost Reports)

Reserved Instance / Savings Plans Recommendations

  • Analyze past usage
  • Suggest RI/SP purchase for savings
  • Show estimated savings

Right-sizing recommendations

  • Identify underutilized EC2
  • Suggest smaller instance type
  • Combined với Compute Optimizer (more detailed)

3. AWS Budgets

4 budget types

  • Cost budget: dollars
  • Usage budget: hours, GB, requests
  • RI/SP budget: utilization, coverage
  • Action-based budget: trigger actions on threshold

Actions (Budget Actions)

  • Send SNS notification
  • Apply IAM policy (restrict permissions)
  • Stop EC2/RDS instances
  • Target SCP in Organizations

Alerting

  • Email (up to 10 recipients)
  • SNS topic
  • AWS Chatbot (Slack, Teams)

Cost

  • 2 budgets free
  • $0.02/budget/day after

4. Cost Anomaly Detection

Định nghĩa

ML-powered service detect unexpected cost spikes.

Setup

  • Monitor types:
    • AWS services (per service)
    • Linked Account (per account in Organization)
    • Cost Categories
    • Cost Allocation Tags
  • Threshold: dollar amount OR percentage increase
  • Subscription: email/SNS

Detection

  • Algorithm learns normal pattern (14+ days)
  • Alerts on anomalies (statistical significance)
  • Suggests root cause (which service/resource increased)

Cost

  • Free

Use case

  • Detect hacker mining crypto on EC2
  • Catch app bug causing Lambda invocation spike
  • Identify cost surprises before month-end

5. Cost Allocation Tags

2 types

  • AWS-generated: aws:createdBy, aws:cloudformation:stack-name
  • User-defined: Project, Environment, CostCenter, Owner

Activation

  1. Tag resources (manual hoặc via Tag Policies)
  2. Billing Console → Cost Allocation Tags → Activate
  3. Wait ~24 hours for tag to appear in Cost Explorer
  4. Filter/group by tag in Cost Explorer

Best practice

  • Mandatory tags via SCP: enforce tagging
  • Tag policies via Organizations: standardize values
  • Tag missing → automation: Lambda finds untagged → notify owner

Common tag schema

Project       (string): "ProjectX"
Environment   (enum): "prod", "staging", "dev", "test"
Owner         (email): "alice@company.com"
CostCenter    (string): "CC-1234"
Application   (string): "billing-service"

6. Cost Categories

Định nghĩa

Cost Categories = group costs by custom rules for reporting.

Setup

  • Define category with rules:
    • Match by account, service, tag, charge type
    • Multiple categories per cost

Example

CostCategory:TeamRule:Account=111111ORtag:Team="engineering""Engineering"Rule:Account=222222ORtag:Team="marketing""Marketing"Rule:Account=333333ORtag:Team="operations""Operations"

Use case

  • Multi-team chargeback
  • Department billing
  • Project P&L

7. Cost & Usage Report (CUR)

Định nghĩa

CUR = most detailed billing data, delivered to S3 daily.

Format

  • CSV, Parquet, ORC
  • Includes: every line item with usage, cost, tax, credits, etc.

Analysis

  • Athena: SQL queries
  • QuickSight: visualizations
  • 3rd party tools: Cloudability, CloudHealth

vs Cost Explorer

  • Cost Explorer: visual UI, summarized
  • CUR: raw data, granular, customizable

Use case

  • Custom dashboards beyond Cost Explorer
  • FinOps team analysis
  • Chargeback to departments

8. AWS Pricing Calculator

Workflow

  1. Add services to estimate (EC2, S3, etc.)
  2. Configure: instance type, hours, storage, transfer
  3. Calculator estimates monthly + 12-month cost
  4. Export PDF/CSV
  5. Share via URL

Use case

  • Estimate before deploy
  • Present to stakeholders / management
  • Compare options (EC2 vs Lambda vs Fargate)
  • Migration planning

URL

9. Cost Optimization Workflow

Monthly cycle

  1. Review Cost Explorer (last month)
  2. Identify top cost drivers, anomalies
  3. Right-size with Compute Optimizer
  4. Reserved Instance / Savings Plans recommendations
  5. Tag compliance check (untagged resources)
  6. Budget review vs actual
  7. Action items: optimization, cleanup

Quarterly

  • Audit unused resources (idle EC2, unattached EBS)
  • Review storage classes (S3 lifecycle)
  • Check Reserved Instance utilization
  • Renew or modify RI as needed

10. Common Cost Optimization Strategies

1. Right-sizing

  • Compute Optimizer recommendations
  • Schedule shutdowns for dev/test (off-hours)
  • AWS Instance Scheduler service

2. Reserved Instances / Savings Plans

  • 3-year AURI: up to 72% off
  • Compute Savings Plans: flexible

3. Spot Instances

  • Fault-tolerant workloads
  • Up to 90% off

4. Storage tiering

  • S3 lifecycle to IA/Glacier
  • EBS gp2 → gp3 (~20% saving)
  • EFS Lifecycle Management

5. Reduce data transfer

  • VPC Endpoints (avoid NAT/IGW)
  • CloudFront caching
  • Same-region traffic

6. Delete unused resources

  • Idle EC2, RDS, ELB
  • Unattached EBS volumes
  • Unused EIPs
  • Old snapshots

7. Lambda vs EC2 for low traffic

  • Lambda free tier: 1M requests/month
  • Often cheaper for sporadic workload

8. Use ARM (Graviton)

  • t4g, m6g, c7g instances ~20% cheaper

Câu hỏi ôn tập

  1. Cost Explorer granularity options là gì?

    Xem đáp án

    Hourly (data retained 14 ngày), Daily (38 tháng), Monthly (38 tháng). Hourly granularity có phí thêm nếu dùng API nhưng miễn phí trong console. Hourly hữu ích để identify spikes trong ngày. Monthly đủ cho high-level budget review. Daily cho trend analysis theo tuần. Cũng có thể filter theo Service, Linked Account, Tag, Region, Usage Type, Instance Type.

  2. Cost Anomaly Detection có miễn phí không?

    Xem đáp án

    Miễn phí — Cost Anomaly Detection (2020) không có phí riêng. Sử dụng ML để detect cost anomalies so với historical patterns, gửi alert qua email hoặc SNS khi phát hiện. Có thể setup monitors theo individual service, AWS account, cost category, hoặc cost allocation tag. Phát hiện được ransomware mining, data exfiltration, code bugs gây cost spike — trước khi nhận bill cuối tháng.

  3. Cost Allocation Tags hiện trong Cost Explorer sau bao lâu?

    Xem đáp án

    24 giờ sau khi activate. Điều quan trọng: chỉ cost data sau thời điểm activate mới được tag — không retroactive. Và tags phải được activate trong Billing Console (Billing → Cost Allocation Tags) — chỉ tag resource không đủ. Workflow: tag resources → activate tag → đợi 24h → filter theo tag trong Cost Explorer.

  4. CUR khác Cost Explorer ở điểm gì?

    Xem đáp án

    Cost and Usage Report (CUR): raw, granular CSV/Parquet data export sang S3 — mọi line item (hourly), resource IDs, pricing details, reservation usage. Dùng với Athena, QuickSight, third-party tools cho custom analysis. Cost Explorer: interactive visualization, predefined dashboards, forecasting, recommendations — không granular như CUR nhưng dễ dùng hơn. CUR cho advanced analytics; Cost Explorer cho quick insights.

  5. Compute Optimizer khác Cost Explorer recommendation ở điểm gì?

    Xem đáp án

    Compute Optimizer: dùng ML analyze CloudWatch utilization metrics (CPU, network, memory từ agent) để recommend optimal EC2 instance type, Lambda memory, EBS size — rất cụ thể với performance trade-offs. Cost Explorer right-sizing: recommend based on CloudWatch metrics đơn giản hơn, focus trên cost savings của EC2. Compute Optimizer có ML model tốt hơn, analyze Lambda và EBS — Cost Explorer chỉ EC2.

Bài tập thực hành

  • Mở Cost Explorer, group by Service, identify top 3 costs
  • Tạo monthly cost budget $50 với email alert
  • Enable Cost Anomaly Detection
  • Activate user-defined tags ProjectEnvironment
  • Estimate cost cho 1 EC2 t3.medium + RDS db.t3.small via Pricing Calculator

Tài liệu tham khảo chính thức


Tiếp theo: Reserved Instances và Savings Plans Deep Dive