GCP Billing CLI: Automate Cost Savings in GCP
Managing costs in Google Cloud Platform (GCP) is a critical challenge, especially for projects that don’t need to run 24/7. Turning off billing for idle projects can significantly reduce costs, but doing it manually is cumbersome and error-prone. Enter the GCP Billing CLI: a powerful command-line tool that helps you enable, disable, and schedule billing operations for your GCP projects.
This guide will walk you through the problem it solves, its features, and how to set it up and use it effectively.
Unnecessary Costs for Idle Projects
At any given point our team have numerous projects running in GCP, many of which are development deployments or test environments for various projects we’re working on. These projects often have many different GCP services enabled, incurring unnecessary costs during nights, weekends, and holidays when they aren’t actively in use. While we occasionally remember to turn billing off manually, we often forget, leading to unexpectedly large bills. We needed a more flexible and automated way to manage this, enabling us to turn billing on and off as required to avoid wasting money.
GCP Billing CLI
Frustrated by the recurring costs for unused resources, I decided to create a solution. I needed a straightforward CLI tool to efficiently turn billing on or off as needed. The GCP Billing CLI streamlines billing management with the following capabilities:
- List Projects and Billing Accounts: Easily view all your projects and check if billing is enabled or not.
- Enable/Disable Billing: Quickly toggle billing status for any project.
- Schedule Billing Operations: Automate billing status changes with time-based or advanced CRON expressions. For example, disable billing over weekends or outside business hours and enable it during work hours.
Setting Up the GCP Billing CLI
The CLI tool is built using TypeScript and uses a lot of Google Cloud APIs to work. All the instructions on how to set up and run the tool can also be found in the repo readme. Before setting up the CLI, ensure you have the following:
- Node.js (v14 or higher)
- Google Cloud SDK and a selected project.
- GCP APIs enabled (see below).
- Appropriate permissions for your GCP account.
1. Enable Required APIs
First we need to run the following command to enable all necessary GCP APIs:
gcloud services enable \
cloudfunctions.googleapis.com \
cloudbuild.googleapis.com \
artifactregistry.googleapis.com \
cloudscheduler.googleapis.com \
cloudbilling.googleapis.com \
cloudresourcemanager.googleapis.com
2. Deploy the Cloud Function
If we also want to be able to schedule enabling/disabling billing for a project we also need to set up a Cloud Function to do this. The CLI uses Google Cloud Scheduler to schedule a call to the Cloud Function which handles the enabling or disabling of the billing on the project. Follow these steps:
# Create a service account for the Cloud Function
gcloud iam service-accounts create billing-function-sa \
--display-name="Billing Function Service Account"
# Grant permissions to the service account
gcloud organizations add-iam-policy-binding $(gcloud organizations list --format='value(ID)') \
--member="serviceAccount:billing-function-sa@YOUR_PROJECT_ID.iam.gserviceaccount.com" \
--role="roles/billing.admin"
# Deploy the Cloud Function
gcloud functions deploy billing-manager \
--runtime nodejs18 \
--trigger-http \
--allow-unauthenticated \
--entry-point manageBilling \
--region us-central1 \
--memory 256MB
3. Install the CLI
# Install dependencies
npm install
# Build the CLI
npm run build
# Link the CLI globally
npm link
Using the GCP Billing CLI
Listing Projects and Billing Accounts
To view all projects and their billing status:
gcp-billing list
You can also filter projects with specific prefixes:
# Exclude projects starting with 'prod-'
gcp-billing list -e prod-
# Show only projects starting with 'prod-'
gcp-billing list -f prod-
Enabling/Disabling Billing
Enable billing for a project:
gcp-billing enable <projectId> <billingAccountId>
Disable billing for a project:
gcp-billing disable <projectId>
Scheduling Billing Operations
The most useful feature of this tool might be the ability to automate billing changes with flexible scheduling options:
# Enable billing at 9 AM Pacific time
gcp-billing schedule-enable my-project-id billing-account-id --time 09:00 --timezone America/Los_Angeles
# Disable billing at 6 PM Eastern time
gcp-billing schedule-disable my-project-id --time 18:00 --timezone America/New_York
You can also use CRON-based scheduling:
# Enable billing every Monday at 9 AM Pacific time
gcp-billing schedule-enable my-project-id billing-account-id --cron "0 9 * * 1" --timezone America/Los_Angeles
# Disable billing every weekday at 6 PM Eastern time
gcp-billing schedule-disable my-project-id --cron "0 18 * * 1-5" --timezone America/New_York
Examples of Use Cases
Here are some useful examples to shut off billing during periods of downtime.
Turn Off Billing Over Weekends
gcp-billing schedule-disable my-project-id --cron "0 0 * * 6" --timezone UTC
gcp-billing schedule-enable my-project-id --cron "0 0 * * 1" --timezone UTC
Reduce Costs Outside Business Hours
gcp-billing schedule-disable my-project-id --time 18:00 --timezone America/New_York
gcp-billing schedule-enable my-project-id --time 09:00 --timezone America/New_York
Enable Billing for Specific Monthly Tasks
gcp-billing schedule-enable my-project-id billing-account-id --cron "0 0 1 * *" --timezone UTC
gcp-billing schedule-disable my-project-id --cron "0 23 1 * *" --timezone UTC
Conclusion
The GCP Billing CLI was a game-changer for managing our GCP costs effectively. By automating billing operations and providing flexible scheduling options, it helps reduce waste and optimize spending. Whether you’re a small team or managing multiple projects, this tool makes GCP billing management simple and efficient.
Check out the GitHub repo here!
Have any questions about the CLI tool or need help with automation? Feel free to reach out to us at Automate Army. We’re a friendly bunch who love solving automation challenges and helping teams like yours save time and money!