0
0
GCPcloud~5 mins

Cost management with billing reports in GCP - Commands & Configuration

Choose your learning style9 modes available
Introduction
Keeping track of your cloud spending helps avoid surprises in bills. Billing reports show how much you use and pay for cloud services, helping you manage costs better.
When you want to see how much each project or service costs in your cloud account
When you need to find which team or department is using the most cloud resources
When you want to set budgets and alerts to avoid overspending
When you want to export billing data for detailed analysis or reporting
When you want to understand trends in your cloud spending over time
Config File - billing_export_config.yaml
billing_export_config.yaml
billingExport:
  datasetId: billing_data
  projectId: my-gcp-project
  tableId: gcp_billing_export
  exportInterval: DAILY
  filter:
    projects:
      - my-gcp-project
    services:
      - Compute Engine
      - Cloud Storage

This YAML file configures billing export settings.

  • datasetId: The BigQuery dataset where billing data will be stored.
  • projectId: Your Google Cloud project ID.
  • tableId: The table name inside the dataset for billing data.
  • exportInterval: How often data is exported, here daily.
  • filter: Limits export to specific projects and services to reduce data volume.
Commands
This command sets up daily export of billing data to a BigQuery dataset and table for analysis.
Terminal
gcloud beta billing export create --dataset=billing_data --project=my-gcp-project --table=gcp_billing_export --interval=DAILY
Expected OutputExpected
Billing export created successfully. Dataset: billing_data Table: gcp_billing_export Export interval: DAILY
--dataset - Specifies the BigQuery dataset to store billing data
--project - Specifies the Google Cloud project ID
--interval - Sets how often billing data is exported
This command queries the last 30 days of billing data to find the top 5 most expensive services.
Terminal
bq query --use_legacy_sql=false 'SELECT service.description, SUM(cost) as total_cost FROM `my-gcp-project.billing_data.gcp_billing_export` WHERE usage_start_time >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 30 DAY) GROUP BY service.description ORDER BY total_cost DESC LIMIT 5;'
Expected OutputExpected
service.description total_cost Compute Engine 1234.56 Cloud Storage 789.01 BigQuery 456.78 Cloud SQL 234.56 Cloud Functions 123.45
--use_legacy_sql=false - Ensures the query uses standard SQL syntax
This command creates a budget alert to notify when spending reaches 50% of $1000 monthly budget.
Terminal
gcloud beta billing budgets create --billing-account=012345-6789AB-CDEF01 --display-name="Monthly Budget" --amount=1000 --threshold-rules=threshold_percent=0.5,spend_basis=CURRENT_SPEND
Expected OutputExpected
Created budget [Monthly Budget] with amount $1000 and alert threshold at 50%
--billing-account - Specifies the billing account to apply the budget
--amount - Sets the budget amount in USD
This command lists all budgets set on the billing account to review alerts and limits.
Terminal
gcloud beta billing budgets list --billing-account=012345-6789AB-CDEF01
Expected OutputExpected
NAME AMOUNT THRESHOLDS Monthly Budget 1000 50%
--billing-account - Specifies the billing account to list budgets for
Key Concept

If you remember nothing else from this pattern, remember: exporting billing data to BigQuery lets you analyze and control your cloud costs easily.

Common Mistakes
Not enabling billing export before running queries
Without export enabled, billing data won't be available in BigQuery, so queries return no data.
Always set up billing export first using gcloud billing export commands.
Using legacy SQL syntax in BigQuery queries
Legacy SQL syntax is outdated and may cause errors or unexpected results.
Use --use_legacy_sql=false flag to run standard SQL queries.
Setting budget alerts without specifying the correct billing account
Budgets won't apply if the billing account ID is wrong or missing.
Verify billing account ID with gcloud billing accounts list before creating budgets.
Summary
Use gcloud commands to enable daily billing export to BigQuery for cost tracking.
Query billing data in BigQuery to find expensive services and usage patterns.
Create budgets with alerts to monitor and control cloud spending.