AdvancedLesson 8 of 12

Cost Management

Optimize cloud spend through pricing models, discounts, and monitoring. Save 30-50% without sacrificing performance.

Simple Explanation (ELI5)

Cloud costs scale with usage: more instances = higher bills. GCP charges per second for compute, per GB for storage/data transfer. Smart cost management means: (1) right-size instances (don't waste), (2) use discounts (commit to 1- or 3-year plans), (3) monitor spending (catch unexpected spikes), (4) optimize data transfer (cheaper to process data locally than transfer worldwide). Get these right and bills drop 30-50% with zero downtime.

Why Cost Management Matters

Technical Explanation

1. GCP Pricing Models

ServiceUnitTypical Cost (US)Example
Compute EnginePer vCPU-hour + Per GB-hour$0.035/vCPU-hr, $0.0094/GB-hrn1-standard-4 (4 vCPU, 15 GB): $0.19/hr
Cloud StoragePer GB/month$0.020 (Standard), $0.010 (Nearline)100 GB Standard = $2/month
Cloud SQLPer vCPU-hour + Storage$0.36/vCPU-hr (db-n1-standard-1)Cloud SQL instance: ~$50-300/month
BigQueryPer TB scanned (queries) + Per GB (storage)$6.25/TB scan, $0.02/GB storage1000 GB scan = $6.25
Cloud RunPer vCPU-second + Per GB-second$0.000024/vCPU-sec, $0.0000050/GB-sec1M requests (0.1 vCPU, 1 min avg): ~$3
bash
# Calculate monthly cost for an n1-standard-4 instance running 24/7
# n1-standard-4: 4 vCPU @ $0.035/hr + 15 GB @ $0.0094/hr = $0.19/hr
# $0.19/hr * 730 hours/month = $138.70/month

# Add 100 GB persistent disk storage: 100 GB @ $0.04/GB-month = $4/month
# Total: $142.70/month for one instance

# Using 3-year commitment (35% discount):
# $142.70 * (1 - 0.35) = $92.76/month

2. Discounts: Commitment and Sustained-Use

Commitment Discounts: Pay upfront for 1-year or 3-year compute/storage. Saves 25-35%. Locked in; can't easily downsize.

Sustained-Use Discounts: Automatic rebate (5-30%) if you use an instance for 25%+ of the month. No commitment needed; kicks in automatically.

bash
# View commitment discounts
gcloud compute commitments list

# Create 3-year commitment for 100 vCPU-hours (e.g., for 25 instances of n1-standard-4)
gcloud compute commitments create my-commitment \
  --plan=3-year \
  --resources=vcpu:100,memory:375 \
  --region=us-central1

# Check spending trends
gcloud billing accounts list
gcloud billing accounts budgets create my-budget \
  --billing-account=ACCOUNT_ID \
  --display-name="Monthly Budget" \
  --budget-amount=5000 \
  --threshold-rule=percent=50 \
  --threshold-rule=percent=100

3. Cost Optimization Patterns

Pattern 1: Right-Sizing

Don't run n2-highmem-96 if n1-standard-4 works. Use Compute Recommender to analyze actual CPU/memory usage. Right-size instances to reduce waste.

bash
# Get rightsizing recommendations
gcloud recommenders list --location=us-central1
gcloud recommenders describe \
  projects/MY_PROJECT/locations/us-central1/recommenders/google.compute.instance.MachineTypeRecommender

# View recommendations
gcloud recommender recommendations list \
  --recommender=google.compute.instance.MachineTypeRecommender \
  --location=us-central1

Pattern 2: Preemptible VMs

Trade availability for cost. Preemptible instances cost 70% less but can be terminated anytime (Google reclaims capacity). Perfect for batch jobs, test environments, CI/CD runners.

bash
# Create a preemptible VM (70% cheaper)
gcloud compute instances create my-preempt \
  --machine-type=n1-standard-4 \
  --preemptible \
  --image-family=debian-11 \
  --image-project=debian-cloud

# Cost: $0.19/hr normal -> $0.057/hr preemptible

Pattern 3: Scheduled Scaling

Scale down non-critical workloads at night/weekends. Dev environments don't need to run 24/7; scale down at 10pm, up at 7am.

bash
# Create a Cloud Scheduler job to scale down at 10pm
gcloud scheduler jobs create app-engine my-scale-down-job \
  --schedule="0 22 * * *" \
  --timezone="America/New_York" \
  --http-method=POST \
  --uri=https://my-function-url/scale-down \
  --oidc-service-account-email=my-service-account@project.iam.gserviceaccount.com

# Scale up at 7am
gcloud scheduler jobs create app-engine my-scale-up-job \
  --schedule="0 7 * * *" \
  --timezone="America/New_York" \
  --http-method=POST \
  --uri=https://my-function-url/scale-up