A comprehensive Python script that queries Google Cloud Platform (GCP) billing data from BigQuery, generates detailed cost analysis reports, and sends them as well-formatted messages to Slack.
This tool helps teams monitor their GCP spending by providing daily summaries, month-over-month comparisons and granular break down expenses by projects, services and there SKU's
- Multi-Project Reporting: Monitor costs for multiple GCP projects in a single run.
- Detailed Slack Integration: Posts a main summary table by service, followed by detailed, threaded replies with granular SKU-level breakdowns for each service.
- Advanced Cost Analysis:
- Calculates a month-to-date forecast.
- Compares spending against a prorated last-month baseline.
- Displays a 7-day average and recent daily costs to identify trends.
- Highlights the most recent day's cost variance against the 7-day average.
- Smart Compute Engine Aggregation: Simplifies complex Compute Engine costs by grouping hundreds of SKUs into easy-to-understand categories (e.g., "VM Core Hours", "VM RAM").
- Custom Sorting: Prioritizes BigQuery costs and then sorts all other services by current spending, ensuring the most important information is always at the top.
- Intelligent Caching: Saves query results to minimize BigQuery costs and automatically refreshes the previous month's data during the first 7 days of a new month to ensure billing data is final.
- Clean & Readable Reports: Uses
prettytableto generate perfectly aligned, easy-to-read text tables with row numbers. - Highly Configurable: All settings, projects, and Slack channels are managed via an external
config.yamlfile. - Resilient Notifications: Includes a retry mechanism for sending Slack messages to handle temporary network or API issues.
- Cost-Aware Execution: Performs a BigQuery "dry run" before executing queries to estimate the cost of the query itself.
- Flexible Usage: Supports command-line arguments to force data refreshes and specify config file paths.
- Google Cloud Project: A GCP project with billing enabled.
- BigQuery Billing Export: You must have GCP billing data exported to a BigQuery dataset.
- Service Account: A GCP Service Account with the
BigQuery Userrole on the project containing the billing export. You'll need to generate a key and set up authentication (e.g., by setting theGOOGLE_APPLICATION_CREDENTIALSenvironment variable). - Slack App: A Slack App with a Bot Token that has the
chat:writepermission.
-
Clone the repository:
git clone [https://github.com/vadirajks/gcp-cost-reporter.git](https://github.com/vadirajks/gcp-cost-reporter.git) cd gcp-cost-reporter -
Create a Python virtual environment (recommended):
python3 -m venv venv source venv/bin/activate -
Install the required packages:
pip install -r requirements.txt
-
Configure the script:
- Copy the example configuration file:
cp config.yaml.example config.yaml
- Edit
config.yamlwith your specific details (BigQuery table ID, projects, Slack channel IDs).
- Copy the example configuration file:
-
Set environment variables:
- Set the path to your GCP service account key file.
export GOOGLE_APPLICATION_CREDENTIALS="/path/to/your/service-account-key.json"
- Set your Slack Bot Token.
export SLACK_BOT_TOKEN="xoxb-your-slack-bot-token"
- Set the path to your GCP service account key file.
Once configured, you can run the script from your terminal.
-
Standard Run (uses cache if available):
python main.py
-
Force a Data Refresh (ignores cache):
python main.py --force-refresh
-
Use a custom config file path:
python main.py --config /path/to/your/config.yaml
You can set this up to run automatically on a schedule using a cron job or a service like Cloud Scheduler with Cloud Functions.
This section outlines potential future improvements for the project. Contributions are highly welcome!
- Cost Anomaly Detection: Instead of just comparing to the previous month, implement statistical analysis (e.g. 30-day rolling average) to automatically detect and flag unusual cost spikes for a service with a "🔥 COST ANOMALY" warning.
- Label-Based Cost Breakdown: Extend the query and reporting logic to group costs by GCP resource labels (e.g.,
team: backend,env: prod). This would provide powerful, fine-grained cost attribution for different teams or applications. - Interactive Slack Bot: Add functionality for the script to listen to replies in Slack. For example, replying to a "Compute Engine" summary with the word
rawcould trigger the bot to post a new reply with the detailed, non-aggregated list of SKUs.
- Threshold-Based Budget Alerts: Enhance the
config.yamlto allow setting a monthly budget per project. If the forecasted cost exceeds this budget, the script would send a special, high-visibility alert (e.g., using Slack's colored message attachments and an @-mention). - Idle Resource Identification: A more advanced feature to query GCP Asset Inventory or monitoring APIs to identify costly but underutilized resources, such as unattached Persistent Disks or idle Cloud SQL instances, and flag them in the report.
- Cost Correlation with Events: Integrate with a CI/CD system (e.g., via webhooks) or Git history to correlate a sudden cost increase with a recent deployment or infrastructure change, adding valuable context to the alert.
Contributions are welcome! Please feel free to submit a pull request or open an issue to discuss a new feature.
This project is licensed under the MIT License - see the LICENSE file for details.