Overview and What You Will Learn
A GCP budget watches spend on a Billing Account or on selected projects and sends an alert when actual or forecasted cost crosses a percentage of the amount you set. It warns you, but it does not stop anything from running.
In this lab, you will:
- Link a project to a Billing Account
- Create a budget with four alert rules
- Publish alerts to a Pub/Sub topic
- Export billing data to BigQuery for cost analysis
Why This Matters in Production
A platform team at a company like Spotify runs hundreds of projects. One forgotten test cluster in a single project can quietly add thousands of dollars to the monthly bill before anyone looks at the invoice. A budget with a 50% rule and a forecasted-spend rule turns that surprise into an early warning, while there is still time to shut the resources down.
Core Principles
A Billing Account is a separate object from a Project. It holds the payment method, and one Billing Account can pay for many Projects. A Budget belongs to the Billing Account and can be scoped to specific projects or services.
payment method)] --> P1[Project: prod-app] BA --> P2[Project: dev-app] BA --> BU{{Budget
50, 90, 100 percent}} BU -->|Email| AD([Billing admins]) BU -->|Publish| PS[Pub/Sub topic] classDef acct fill:#326CE5,color:#FFFFFF classDef proj fill:#DBEAFE,color:#1E293B classDef alert fill:#F97316,color:#FFFFFF classDef people fill:#D1FAE5,color:#1E293B class BA acct class P1,P2 proj class BU,PS alert class AD people
A budget fires on two kinds of rules:
| Rule type | Triggers when |
|---|---|
| Actual spend | Money has already been spent |
| Forecasted spend | Google predicts you will cross the amount this period |
A budget alert is only a notification. To turn it into enforcement, publish alerts to Pub/Sub and subscribe a Cloud Function that acts on them.
crossed}} --> T[Pub/Sub topic] T --> F[Cloud Function] F --> A([Disable billing
on project]) classDef tool fill:#F97316,color:#FFFFFF classDef code fill:#8B5CF6,color:#FFFFFF classDef stop fill:#EF4444,color:#FFFFFF class B,T tool class F code class A stop
SecurityDisabling billing on a project stops all paid services in it, and resources can be shut down or lost. Use automated enforcement only on sandbox and dev projects, never on production.
Detailed Step-by-Step Practical Lab
Set the variables used throughout the lab. Project IDs are globally unique, so change the suffix if the create command fails. Replace the Billing Account ID with yours.
export PROJECT_ID=gcp-billing-lab-mumbai-2026export BILLING_ACCOUNT_ID=012345-6789AB-CDEF01- Create the lab project and make it the active project:
gcloud projects create $PROJECT_ID --name="Billing Lab"gcloud config set project $PROJECT_ID- List your Billing Accounts, then link the project to one of them. You need the Billing Account User role on the account:
gcloud billing accounts list gcloud billing projects link $PROJECT_ID \ --billing-account=$BILLING_ACCOUNT_ID- Confirm the link. The output should show
billingEnabled: true:
gcloud billing projects describe $PROJECT_ID- Enable the APIs this lab needs and create the Pub/Sub topic that will receive budget alerts:
gcloud services enable billingbudgets.googleapis.com \ pubsub.googleapis.com bigquery.googleapis.comgcloud pubsub topics create budget-alerts-topic- Create a budget scoped to this project. The currency in
--budget-amountmust match your Billing Account's currency. The 50% rule is the early warning, and the last rule fires on forecasted spend. The command saves the new budget's name so cleanup is easy:
export PROJECT_NUMBER=$(gcloud projects describe $PROJECT_ID \ --format="value(projectNumber)") export BUDGET_NAME=$(gcloud billing budgets create \ --billing-account=$BILLING_ACCOUNT_ID \ --display-name="lab-budget-mumbai" \ --filter-projects=projects/$PROJECT_NUMBER \ --calendar-period=month \ --budget-amount=500INR \ --threshold-rule=percent=0.5 \ --threshold-rule=percent=0.9 \ --threshold-rule=percent=1.0 \ --threshold-rule=percent=1.0,basis=forecasted-spend \ --notifications-rule-pubsub-topic=projects/$PROJECT_ID/topics/budget-alerts-topic \ --format="value(name)") echo $BUDGET_NAMERememberBudgets also email billing admins by default. The Pub/Sub topic is an extra channel, and it is what lets a Cloud Function react automatically.
- Verify the budget exists:
gcloud billing budgets list --billing-account=$BILLING_ACCOUNT_ID- Create a BigQuery dataset, then enable standard usage cost export to it in the Console. Export cannot be enabled with gcloud:
bq --location=asia-south1 mk --dataset $PROJECT_ID:billing_exportConsole: Billing -> Billing export -> BigQuery export -> Standard usage cost -> Edit settings Project: gcp-billing-lab-mumbai-2026 Dataset: billing_export- After export has run for a day or more, query cost per service. The table name ends with your Billing Account ID, with hyphens replaced by underscores:
export TABLE="$PROJECT_ID.billing_export.gcp_billing_export_v1_${BILLING_ACCOUNT_ID//-/_}" bq query --use_legacy_sql=false \ "SELECT service.description AS service, ROUND(SUM(cost), 2) AS total_cost FROM \`$TABLE\` GROUP BY service ORDER BY total_cost DESC"- Clean up. A budget lives on the Billing Account, so deleting the project does not remove it:
gcloud billing budgets delete $BUDGET_NAME --billing-account=$BILLING_ACCOUNT_IDgcloud pubsub topics delete budget-alerts-topicgcloud projects delete $PROJECT_ID --quietProduction Best Practices & Common Pitfalls
Common MistakeRunning projects with no budget, or with a single 100% rule. By the time a 100% alert arrives, the money is already spent. Add 50% and 90% rules plus a forecasted-spend rule.
TipCreate one budget per environment (prod, staging, dev) instead of one for the whole account. A crossed dev budget and a crossed prod budget call for very different responses.
- Billing export is not retroactive. Data only accumulates from the moment export is enabled, so turn it on when the project is created.
- A budget alert does not stop spend. Crossing a threshold changes nothing about running resources unless Pub/Sub is wired to automation.
- Alerts are not real time. Billing data can lag by hours, so treat budgets as a safety net and not as a hard spending cap.
- Check how credits are counted. A budget's credit setting decides whether free credits or discounts reduce the tracked cost, which changes when alerts fire.
- Grant the right roles. Creating budgets needs Billing Account Administrator or Billing Account Costs Manager on the account, not just project owner.
Quick Reference & Troubleshooting Commands
| Command | Description |
|---|---|
gcloud billing accounts list |
List available Billing Accounts |
gcloud billing projects link PROJECT_ID --billing-account=ID |
Link a project to a Billing Account |
gcloud billing projects describe PROJECT_ID |
Confirm a project's billing link status |
gcloud billing budgets list --billing-account=ID |
List budgets and their IDs |
gcloud billing budgets delete BUDGET --billing-account=ID |
Remove a budget |
bq query --use_legacy_sql=false 'SQL' |
Query exported billing data |