Skip to content
Tutorials Center
My learning

Topics

Lesson 35 of 45 ยท Production Practice

Cost Management and Optimization

Control Google Cloud spending: billing export to BigQuery, labels, budgets, committed use discounts, right-sizing and Spot VMs, with a pandas billing analysis stand-in.

Cloud bills surprise people because costs are created by actions (starting a VM, storing data, running a query) and not by a purchase order. Nobody signs off on each one. Good cost management is therefore a habit with three parts: see what you spend, attribute it to the people and projects that cause it, and reduce it without hurting reliability.

This lesson covers the tools Google Cloud gives you for each part. The console and billing data need a real account, so the commands are read-only. A pandas stand-in then shows how to analyze billing data yourself. Prices in examples are invented; check Google's current price lists for real numbers.

See: reports, budgets and the billing export

Three tools, in the order you should set them up:

  1. Budgets and alerts on the billing account (see Account, Free Tier and Billing Setup). A budget does not stop spending by default. It sends notifications at thresholds you choose, such as 50%, 90% and 100% of an amount. You can also connect a budget to a Pub/Sub topic and write automation that reacts, for example by disabling billing on a sandbox project. Test any such automation carefully, because switching off billing stops resources.
  2. Cost reports in the console: filter and group by project, service, SKU, location and label.
  3. Billing export to BigQuery: a continuous export of detailed usage and cost rows into a dataset. This is the source for any serious analysis, because you can query it with SQL (BigQuery basics). Turn it on early, since it only records data from the moment you enable it.

A typical query against the export (read-only; the table name and exact schema depend on your setup, so check your dataset):

SQL
SELECT
  service.description AS service,
  ROUND(SUM(cost), 2) AS total_cost
FROM `my-project.billing.gcp_billing_export_v1_XXXXXX`
WHERE usage_start_time >= TIMESTAMP('2025-01-01')
GROUP BY service
ORDER BY total_cost DESC
LIMIT 10;

Attribute: labels and structure

A bill that says "Compute Engine: $4,000" does not tell you who to talk to. Labels are key-value pairs you attach to resources, such as team=checkout and env=prod, and they flow into the billing export. A short, enforced list works best: team, env, app and owner. Combine labels with a sensible project structure, for example one project per environment per team, so that cost per project already tells a story.

Terminal
gcloud compute instances update web-1 --zone=europe-west1-b \
  --update-labels=team=checkout,env=prod

Labels are only useful if they are consistent. Add them in your Terraform modules so every resource gets them by default, and report on the resources that have none.

Reduce: the main levers

Five ways to spend less

Right-size Match machine size to real usage.
Delete idle Stop forgotten VMs, disks and IPs.
Commit Trade a commitment for a discount.
Spot Use interruptible capacity for batch work.

Start with the cheapest change: removing what nobody uses.

1 / 2

Right-sizing and cleanup

Google Cloud's Recommender suggests smaller machine types for underused VMs and lists idle resources, such as unattached disks and unused external IP addresses. A VM that averages 5% CPU is probably too large. A custom machine type or a smaller predefined one can cut the cost with no visible effect. See Compute Engine for machine types. Delete what is unused; stopped VMs still pay for their disks.

Committed use discounts

Committed use discounts (CUDs) give a lower price in exchange for committing to a level of usage for one or three years. There are resource-based commitments (for example a number of vCPUs and amount of memory in a region) and spend-based commitments (a fixed hourly spend on certain services). You pay for the commitment whether or not you use it, so commit only to your steady baseline, such as the load that runs all night, and leave spikes on normal on-demand pricing. Separately, many services apply sustained use style discounts automatically; the rules differ by product and change over time.

Spot VMs

Spot VMs use spare capacity at a large discount, but Google can reclaim them at any time with a short warning. They suit work that tolerates interruption: batch jobs, rendering, CI workers and stateless workers in a managed instance group. They do not suit a single database. Design the job so it can checkpoint its progress and resume.

Storage, network and queries

Set lifecycle rules to move old objects to colder classes or delete them, and watch for egress: traffic that leaves a region or leaves Google Cloud is charged, so keep chatty services in the same region (regions and zones). In BigQuery, partition and cluster tables and avoid SELECT * (BigQuery performance).

Stand-in: analyze a billing export with pandas

Real billing data lives in BigQuery, but the analysis is the same everywhere. This stand-in builds a small made-up export in memory and answers the questions you would ask first: who spends what, and what has no owner. It runs in Python (Pyodide with pandas), not on Google Cloud.

Python Stand-in: cost by team and unlabeled spend

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Unlabeled spend is a flag: nobody owns it, so nobody will reduce it. Many teams set a goal, such as "less than 5% unlabeled".

Stand-in: should we commit?

This stand-in compares on-demand pricing with a one-year commitment for a given usage pattern. The prices and discount are invented for illustration. It shows why you commit to the baseline, not to the peak.

Python Stand-in: on-demand versus commitment

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Committing to 8 vCPUs, the lowest hour of the day, is a safe saving. Committing to the 40-vCPU peak means paying for capacity that sits idle most of the day, which can cost more than not committing at all. Run the numbers on your own pattern before committing.

A monthly routine

  • Review the cost report by project and label; investigate any jump over, say, 20% week to week.
  • Act on Recommender suggestions and delete idle resources.
  • Check that budgets and alert recipients are still current.
  • Review commitments against actual baseline usage once a quarter.
  • Tear down practice and test environments when you finish them. See Common Mistakes and Troubleshooting for the forgotten-resource trap.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does a Cloud Billing budget do by default?

    Budgets notify you. Stopping spending requires your own automation, and that should be tested carefully.
  2. 2. Why enable the billing export to BigQuery early?

    The export is not backfilled, so enabling it late means missing history.
  3. 3. Which workload suits Spot VMs best?

    Spot VMs can be reclaimed at short notice, so interruption-tolerant work is the right fit.
  4. 4. How much should you commit to with a committed use discount?

    A commitment is paid whether or not it is used, so cover the steady baseline and leave spikes on demand.

Summary

  • Cost management means seeing, attributing and reducing spending.
  • Set budgets and alerts, use the cost reports, and enable billing export to BigQuery early.
  • Apply consistent labels (team, env, app, owner) so each cost has an owner.
  • Right-size, delete idle resources, commit to your baseline, and use Spot VMs for interruptible work.
  • Watch storage classes, network egress and BigQuery scans, and review costs on a regular schedule.

Next: Reliability, Security and Architecture Patterns