Chapter 18 — Cost Management

Cloud bills can spiral out of control fast. Professional operations include cost awareness as a first-class concern.

Cloud Pricing Models

ModelHow It WorksBest ForSavings vs On-Demand
On-DemandPay per hour/second of useVariable workloads, testing0% (baseline)
Reserved Instances1-3 year commitment, fixed rateSteady-state production30-72%
Savings PlansCommit to $/hr spend (flexible)Predictable spend, flexible instances20-50%
Spot/PreemptibleBid on unused capacity (can be terminated)Batch jobs, CI runners, stateless workers60-90%

Cost Optimization Strategies

  1. Right-sizing: Monitor actual CPU/memory usage. Most instances are over-provisioned. A t3.medium using 10% CPU should be a t3.small.
  2. Auto-scaling: Scale down during off-hours. Many apps have 10x traffic difference between peak and trough.
  3. Spot instances: Use for CI/CD runners, batch processing, and stateless workers. Save 60-90%.
  4. Reserved capacity: For databases and always-on servers, commit for 1-3 years.
  5. Storage tiering: Move old data to cheaper storage (S3 Glacier, cold storage).
  6. Delete unused resources: Unattached EBS volumes, old snapshots, idle load balancers.
  7. Use ARM instances: Graviton (AWS) / T2A (GCP) are 20% cheaper with same or better performance.

TCO Comparison: Self-Hosted vs Cloud

FactorVPS ($40/mo)AWS (equivalent)Self-Hosted
Compute$40/mo$70-150/mo$500 one-time + power
DatabaseIncluded (self-managed)$30-100/mo (RDS)Included
BandwidthUsually generous$0.09/GB out (adds up!)ISP cost
Your timeMedium (manage server)Low (managed services)High (manage everything)
ScalingManual (resize/add VPS)AutomaticBuy more hardware
Reliability99.9% SLA typical99.99% possibleDepends on you

Billing Alerts

# AWS: Set up billing alarm via CLI
aws cloudwatch put-metric-alarm \
  --alarm-name "MonthlyBillingAlarm" \
  --alarm-description "Alert when bill exceeds $100" \
  --metric-name EstimatedCharges \
  --namespace AWS/Billing \
  --statistic Maximum \
  --period 21600 \
  --threshold 100 \
  --comparison-operator GreaterThanThreshold \
  --evaluation-periods 1 \
  --alarm-actions arn:aws:sns:us-east-1:ACCOUNT:billing-alerts \
  --dimensions Name=Currency,Value=USD

# Also set alerts at 50%, 80%, 100% of budget
# AWS Budgets is more powerful than CloudWatch for this
The hidden costs of cloud:
Data transfer out: AWS charges $0.09/GB. A site serving 1TB/month = $90 just in bandwidth.
NAT Gateway: $0.045/hr + $0.045/GB processed. Can easily be $30-100/mo.
Load Balancer: $16-25/mo minimum even with zero traffic.
Managed databases: 3-5x the cost of self-managed on a VPS.

Mitigation: Use Cloudflare (free CDN, absorbs bandwidth), minimize NAT Gateway usage, consider Hetzner/DO for predictable pricing.