Overview and What You Will Learn
In this lab, you will link a project to a Billing Account, create a Budget with alert thresholds, and enable billing export to BigQuery for detailed cost analysis - the same proactive cost safeguard every cloud platform needs, configured the GCP-specific way.
Why This Matters in Production
A developer creates a test project on the free trial, spins up a few resources for an experiment, and forgets about them entirely - three weeks later, the free trial credit is gone, consumed almost entirely by compute nobody actually used after the first day. A budget alert set at 50% would have flagged this well before the credit ran out, turning a silent surprise into an early warning with time to react.
Core Principles
A Billing Account is a separate object from a Project - it holds the actual payment method, and one Billing Account can be linked to many different Projects, all of whose usage gets billed to that same account.
+------------------------------------------+| Billing Account (holds payment method) |+------------------------------------------+ | | v v+------------------+ +------------------+| Project: prod-app | | Project: dev-app |+------------------+ +------------------+A Budget defines a spending threshold scoped to either a specific project or the whole billing account, and triggers alerts once actual or forecasted spend crosses a configured percentage of that threshold.
Detailed Step-by-Step Practical Lab
- Create a Project for this lab:
gcloud projects create gcp-billing-lab-2026 --name="Billing Lab"- List available Billing Accounts and link this project to one:
gcloud billing accounts list gcloud billing projects link gcp-billing-lab-2026 \ --billing-account=YOUR_BILLING_ACCOUNT_ID- Confirm the link was successful:
gcloud billing projects describe gcp-billing-lab-2026- Create a budget through the Console, since Budgets are most reliably configured there rather than via CLI:
Console: Billing -> Budgets & alerts -> Create Budget Scope: gcp-billing-lab-2026 (this specific project) Amount: a small threshold appropriate for a lab, e.g. 500 INR Alert thresholds: 50%, 90%, 100% Notification: email to billing admins- Optionally, route the budget alert to a Pub/Sub topic instead of only email, enabling automated responses:
gcloud pubsub topics create budget-alerts-topicNoteOnce a budget is configured to publish to this Pub/Sub topic, a Cloud Function subscribed to it could automatically take action when a threshold is crossed - like disabling billing on a runaway test project - turning a passive notification into an actual automated safeguard.
- Enable billing export to BigQuery for detailed, queryable cost data:
Console: Billing -> Billing export -> BigQuery export -> Edit settings Select or create a BigQuery dataset to export daily billing data into- Once export has been running for at least a day, query the exported billing data directly:
bq query --use_legacy_sql=false \ 'SELECT service.description, SUM(cost) as total_cost FROM `gcp-billing-lab-2026.billing_export.gcp_billing_export_v1` GROUP BY service.description ORDER BY total_cost DESC'- Clean up:
gcloud pubsub topics delete budget-alerts-topicgcloud projects delete gcp-billing-lab-2026 --quietProduction Best Practices & Common Pitfalls
Common MistakeCreating projects and resources on a free trial or pay-as-you-go account without ever setting a budget alert. A single oversized resource or an unexpectedly popular test workload can consume far more than expected before anyone notices, since nothing was actively watching for it.
TipSet a budget alert threshold at 50% in addition to higher thresholds like 90% and 100%. An early warning at 50% gives genuinely useful lead time to investigate and adjust before spend becomes a real problem, rather than only being told once it's already largely happened.
- Billing export data only starts accumulating from the point the export is enabled, not retroactively - set this up early in a project's life if serious cost analysis is ever going to be needed later.
- A budget alert is a notification, not an enforcement mechanism, unless explicitly wired to automation via Pub/Sub and a Cloud Function. On its own, crossing a budget threshold does not stop any resource from continuing to run or accrue cost.
Quick Reference & Troubleshooting Commands
| Command | Description |
|---|---|
gcloud billing accounts list |
List available Billing Accounts |
gcloud billing projects link |
Link a project to a Billing Account |
gcloud billing projects describe |
Confirm a project's billing link status |
bq query |
Query exported billing data in BigQuery |