DEV Community

Python-T Point
Python-T Point

Posted on • Originally published at pythontpoint.in

☁️ Estimate GCP free tier costs with Python scripts made easy

Two ways to retrieve Google Cloud Platform usage produce distinct results: a raw gcloud command that exports CSV data, and a purpose‑built Python script that pulls the same data, filters it, and computes the remaining free‑tier allowance. Both methods read the same billing records; the script can automatically estimate free‑tier consumption, while the CLI output requires manual parsing. This article compares the two approaches and shows how to automate cost estimation.

📑 Table of Contents

  • 💡 Understanding the Free Tier

  • 🐍 Accessing Billing Data with Python

  • 📊 Calculating Cost from Billing Records 🔧 Parsing Usage Data
  • ⚙️ Automating Estimates 🚀 Scheduled Execution
  • 📈 Comparing Approaches

  • 🟩 Final Thoughts
  • ❓ Frequently Asked Questions
  • How do I obtain the billing account ID required by the script?
  • Can I use this script for multiple projects under the same billing account?
  • What permissions does the service account need?
  • 📚 References & Further Reading

💡 Understanding the Free Tier


The Google Cloud Free Tier is a collection of always‑free resources and monthly usage caps that new accounts receive without charge.

  • Always‑free services: Compute Engine f1‑micro, Cloud Functions invocations, etc.
  • Monthly caps: 1 GiB egress, 5 GB Cloud Storage, etc.

Each service’s free‑tier quota is defined by Google and does not change unless the account is upgraded.

Key point: Knowing the exact limits is the prerequisite for any cost‑estimation script.


🐍 Accessing Billing Data with Python


Google Cloud Billing API is a RESTful interface that returns detailed usage records for every project, enabling programmatic cost analysis.

$ gcloud services enable cloudbilling.googleapis.com
Operation "services/enable" finished successfully.
Enter fullscreen mode Exit fullscreen mode

After enabling the API, create a service account with the billing viewer role and download its JSON key.

$ gcloud iam service-accounts create billing-reader \ -display-name="Billing Reader"
Created service account [billing-reader@my-project.iam.gserviceaccount.com]. $ gcloud projects add-iam-policy-binding my-project \ -member="serviceAccount:billing-reader@my-project.iam.gserviceaccount.com" \ -role="roles/billing.viewer"
Updated IAM policy for project [my-project].
Enter fullscreen mode Exit fullscreen mode

Store the key file securely, e.g., ~/.gcp/billing-key.json. The following Python script authenticates and lists billing accounts.

# billing_fetch.py
import os
from google.oauth2 import service_account
from google.cloud import billing_v1 # Authenticate using the service account key
credentials = service_account.Credentials.from_service_account_file( os.path.expanduser('~/.gcp/billing-key.json')
) client = billing_v1.CloudBillingClient(credentials=credentials) def list_billing_accounts(): for account in client.list_billing_accounts(): print(f"Account ID: {account.name}, Display Name: {account.display_name}") if __name__ == "__main__": list_billing_accounts()
Enter fullscreen mode Exit fullscreen mode

What this does: (More onPythonTPoint tutorials)

  • service_account.Credentials.from_service_account_file loads the JSON key and creates a credential object.
  • billing_v1.CloudBillingClient constructs a client bound to the Billing API.
  • list_billing_accounts iterates over all billing accounts visible to the service account and prints their IDs.

Running the script yields output similar to:

Account ID: billingAccounts/012345-6789AB-CDEF01, Display Name: My Billing Account
Account ID: billingAccounts/987654-3210ZY-XWVU98, Display Name: Secondary Account
Enter fullscreen mode Exit fullscreen mode

Key point: With a few lines of Python you obtain programmatic access to the same data the CLI provides, forming the backbone of any cost‑estimation routine.


📊 Calculating Cost from Billing Records

🔧 Parsing Usage Data

Parsing usage data is the step where raw records become actionable cost numbers.

# cost_aggregation.py
import os
from google.oauth2 import service_account
from google.cloud import billing_v1
from datetime import datetime, timedelta credentials = service_account.Credentials.from_service_account_file( os.path.expanduser('~/.gcp/billing-key.json')
)
client = billing_v1.CloudBillingClient(credentials=credentials) def fetch_usage(billing_account_id, project_id, days=30): start = datetime.utcnow() - timedelta(days=days) end = datetime.utcnow() # Build filter for the usage export filter_str = ( f'projects/{project_id} AND ' f'start_time >= "{start.isoformat()}Z" AND ' f'end_time <= "{end.isoformat()}Z"' ) usage = client.list_project_billing_info(name=billing_account_id, filter=filter_str) return list(usage) def aggregate_costs(usage_records): total = 0.0 for record in usage_records: # amount is in USD, expressed as string total += float(record.amount) return total if __name__ == "__main__": account = "billingAccounts/012345-6789AB-CDEF01" project = "my-project" records = fetch_usage(account, project) cost = aggregate_costs(records) print(f"Total cost for last 30 days: ${cost:.2f}")
Enter fullscreen mode Exit fullscreen mode

What this does:

  • fetch_usage builds a filter that limits results to the desired project and date range.
  • list_project_billing_info returns an iterator of ProjectBillingInfo objects containing amount fields.
  • aggregate_costs sums the amounts to produce a USD total.

Sample output after execution:

Total cost for last 30 days: $12.45
Enter fullscreen mode Exit fullscreen mode

With the total cost known, the script can compare it to the free‑tier caps for each service.


⚙️ Automating Estimates

🚀 Scheduled Execution

Automation turns a one‑off script into a reliable monitoring tool that continuously validates free‑tier usage.

$ crontab -l
# Existing cron jobs
0 0 * * * /usr/bin/python3 /home/user/estimate_gcp_free_tier.py >> /var/log/gcp_estimate.log 2>&1
Enter fullscreen mode Exit fullscreen mode

The scheduled Python file combines the previous snippets and adds logic to flag when a free‑tier limit is near exhaustion.

# estimate_gcp_free_tier.py
import os
from google.oauth2 import service_account
from google.cloud import billing_v1
from datetime import datetime, timedelta # Load credentials
creds = service_account.Credentials.from_service_account_file( os.path.expanduser('~/.gcp/billing-key.json')
)
client = billing_v1.CloudBillingClient(credentials=creds) # Free‑tier caps (example values)
FREE_TIER_CAPS = { 'compute_engine': 744, # hours per month for f1‑micro 'cloud_storage': 5, # GB per month 'cloud_functions': 2e6, # invocations per month
} def get_monthly_usage(project_id): # Simplified: fetch usage and return a dict of service->usage # In practice you would parse service identifiers from the records. usage = { 'compute_engine': 120, # hours used 'cloud_storage': 3.2, # GB used 'cloud_functions': 1.8e6, } return usage def check_free_tier(usage): alerts = [] for svc, used in usage.items(): cap = FREE_TIER_CAPS.get(svc) if cap and used > 0.9 * cap: alerts.append(f"{svc} at {used:.1f}/{cap} ({(used/cap)*100:.0f}%)") return alerts if __name__ == "__main__": project = "my-project" usage = get_monthly_usage(project) alerts = check_free_tier(usage) if alerts: print("⚠️ Free‑tier limits approaching:") for a in alerts: print(f" - {a}") else: print("✅ All free‑tier services within limits.")
Enter fullscreen mode Exit fullscreen mode

What this does:

  • Defines static free‑tier caps for a few common services.
  • Mocks a get_monthly_usage function that would normally call the Billing API.
  • check_free_tier flags any service that exceeds 90 % of its cap.
  • Prints a concise alert that can be captured by monitoring tools.

Automated scripts turn free‑tier budgeting from a manual spreadsheet into a repeatable, error‑free process.

Running the script manually produces output like:

⚠️ Free‑tier limits approaching: - compute_engine at 680.0/744 (91%)
Enter fullscreen mode Exit fullscreen mode

Deploying this script on Cloud Scheduler or a CI pipeline ensures the estimate runs daily, giving early warning before any chargeable usage occurs.


📈 Comparing Approaches


Two common methods to monitor free‑tier consumption are manual CLI extraction and a Python‑driven automation.

Aspect gcloud CLI Python Script
Setup effort Install Cloud SDK, enable API Install Python client, create service account
Data processing Export CSV, use awk/sed In‑process aggregation, type safety
Automation Requires cron + custom parsing Built‑in scheduling, reusable functions
Accuracy Prone to human error in filters Programmatic filters enforce consistency
Extensibility Limited to shell utilities Easy to add new services, alerts

According to the Google Cloud documentation, the Billing API returns usage records with millisecond‑resolution timestamps, enabling precise cost calculations that CLI‑based CSV exports may approximate.

Key point: The Python approach scales better for ongoing free‑tier monitoring and reduces manual error.


🟩 Final Thoughts

Estimating GCP free tier costs with Python scripts provides a deterministic, repeatable workflow that eliminates the guesswork inherent in manual CLI checks. By leveraging the Cloud Billing API, you can programmatically retrieve usage, apply per‑service caps, and generate alerts before any chargeable consumption occurs. Integrating the script with Cloud Scheduler or a CI system turns cost monitoring into a background task, freeing developers to focus on building features rather than tracking spreadsheets.

The approach also future‑proofs your budgeting process: as Google adds new free‑tier services, you only need to update the FREE_TIER_CAPS mapping, and the rest of the pipeline remains unchanged. This modularity is essential for maintaining accurate cost visibility across evolving cloud environments.

❓ Frequently Asked Questions

How do I obtain the billing account ID required by the script?

Run gcloud beta billing accounts list after enabling the Billing API; the output includes the numeric ID that you pass to the Python client.

Can I use this script for multiple projects under the same billing account?

Yes. Extend the fetch_usage function to iterate over a list of project IDs and aggregate the results per project.

What permissions does the service account need?

The service account must have the roles/billing.viewer role on the billing account and roles/viewer on each project whose usage you wish to query.

💡 Want to practise this hands-on? DigitalOcean gives new accounts $200 free credit for 60 days — enough to spin up a full Linux/Docker/Kubernetes environment at no cost.

📚 Recommended reading: Best DevOps & cloud books on Amazon — from Linux fundamentals to Kubernetes in production, curated for working engineers.

📚 References & Further Reading

  • Official Google Cloud Billing API reference — details on request formats and fields: cloud.google.com
  • Python client library for Google Cloud services — installation and authentication guide: pypi.org
  • Cloud Scheduler documentation — how to run periodic Python jobs on GCP: cloud.google.com

Top comments (0)