Cost management and optimisation

Google Cloud · Advanced · 13 min

Cloud costs scale with usage and mistakes; visibility and the right discounts often save 30% or more.

Cost control is visibility first, then discounts, then architecture.

Visibility:

  • Budgets on a billing account, optionally scoped to projects, services or labels, send alerts at thresholds of actual or forecasted spend. They do not stop spending; for a hard stop you would wire the budget's Pub/Sub notifications to automation.
  • Billing export to BigQuery gives line-item costs (standard or detailed per-resource), queryable with SQL and connectable to dashboards.
  • Labels (team=checkout, env=prod) on resources flow into the export, enabling chargeback. Agree on a label scheme early and enforce it in Terraform.

Discounts:

  • Committed use discounts: commit to resources or spend for 1 or 3 years for large savings on steady workloads (Compute Engine, Cloud SQL, Cloud Run, GKE and more).
  • Sustained use discounts apply automatically to some VM families that run most of the month.
  • Spot VMs and Spot GKE nodes for interruptible work.

Waste: the Recommender flags idle VMs, oversized machines, unattached disks and unused IPs. Common leaks are forgotten static IPs and disks, log ingestion, BigQuery SELECT *, cross-region egress, and non-zero min instances on idle services.

Review cost weekly per project and label; surprises found in a week are cheap.

Example

gcloud billing budgets create --billing-account=01AB23-45CD67-89EF01 \
  --display-name="shop-prod monthly" --budget-amount=2000USD \
  --filter-projects=projects/shop-prod-1234 \
  --threshold-rule=percent=0.5 --threshold-rule=percent=0.9 \
  --threshold-rule=percent=1.0,basis=forecasted-spend

# Top services last month from the standard billing export
bq query --use_legacy_sql=false '
SELECT service.description AS service,
       ROUND(SUM(cost) + SUM(IFNULL((SELECT SUM(c.amount) FROM UNNEST(credits) c), 0)), 2) AS net
FROM `shop-billing.billing_export.gcp_billing_export_v1_01AB23_45CD67_89EF01`
WHERE invoice.month = "202609"
GROUP BY service ORDER BY net DESC LIMIT 5'

Remember

  • Budgets alert; they do not cap spend
  • Billing export + labels = cost per team and environment
  • CUDs for steady load, Spot for interruptible, Recommender for waste

Watch out for

  • Creating a budget and assuming it will stop a runaway workload.

Check yourself

  1. Spend jumps and the budget hits 100%. What happens by default?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: cloud.google.com/billing/docs/how-to/budgets

Builds on

Unlocks

Nothing builds on this yet.