This document explains how to automatically disable billing on a project when your costs meet or exceed your project budget. When you disable billing on a project, you terminate all Google Cloud services in the project, including Free Tier services. For a more nuanced response to budget notifications, see Control resource usage with notifications.
You might limit costs because you have a maximum amount of money that you can spend on Google Cloud. In these cases, when your budget limit is reached, you might be willing to shut down all of your Google Cloud services and usage to stop incurring costs. Disabling billing on your project is an efficient method to stop incurring costs in that project.
Limitations
There's a delay between incurring costs and receiving budget notifications, so you might incur additional costs for usage that hasn't arrived at the time that all services are stopped. Following the steps in this example doesn't guarantee that you won't spend more than your budget. If you have a limited amount of funds, set your maximum budget below your available funds to account for billing delays.
You can't disable billing on a project that's locked to a billing account. To learn more about locking and unlocking projects, see Secure the link between a project and its billing account.
Before you begin
Before you begin, you must complete the following tasks:
- Enable the Cloud Billing API
- Create a budget that's scoped to a single project
- Set up programmatic budget notifications
Set up a Cloud Run function
To disable Cloud Billing for a project, create a Cloud Run function and configure it to call the Cloud Billing API.
- Complete the steps in Create a Cloud Run function. Ensure that the Trigger type is set to the same Pub/Sub topic that your budget will use.
Add the following dependencies:
Node.js
Copy the following to your
package.json
file:Python
Copy the following to your
requirements.txt
file:Copy the following code into your Cloud Run function:
Node.js
Python
Set the Entry point to the correct function to execute:
Node.js
Set the Entry point to
stopBilling
.Python
Set the Entry point to
stop_billing
.Review the list of environment variables set automatically to determine if you need to manually set the GOOGLE_CLOUD_PROJECT variable to the project that you want to disable Cloud Billing for.
Click DEPLOY.
Configure service account permissions
Your Cloud Run function runs as an automatically created service account. To disable billing, you need to grant the service account permissions to any services on the project that it needs to modify by completing the following steps:
- Identify the correct service account by viewing the details of your Cloud Run function. The service account is listed at the bottom of the page.
Go to the IAM page in the Google Cloud console to set the appropriate permissions.
To modify the billing account permissions, in the Google Cloud console, go to the Billing Account management page, add the service account as a principle on the Cloud Billing account, and set the appropriate billing account permissions.
Learn more about how to configure permissions for Cloud Billing accounts.
Test that Cloud Billing is disabled
When the budget sends a notification, the specified project will no longer have a Cloud Billing account associated with it. To ensure your function works as expected, follow the steps in Test a Cloud Run function.
If successful, the project is no longer visible under the Cloud Billing account and resources in the project are disabled, including the Cloud Run function if it's in the same project.
To continue using Google Cloud resources in the project, in the Google Cloud console, manually re-enable Cloud Billing for your project.
What's next
Review other programmatic notification examples to learn how to do the following: