How to limit or cap google cloud budget

Tags:

The documents are there, but a bit hard to comprehend and follow. So I’m writing the steps here.

Firstly, create a budget. I am receiving email alerts already when the 50%, 70%, etc. of budget is hit. It can be set up following this.

Now you need to shutdown your service if the budget reaches 100%. Follow the steps here.

  • Esp., I follow the steps to completely shutdown.
  • When creating the function, be sure to create a new service account. I find it easier that way, and it’s safer to limit the access.
  • While creating the function, assign GCP_PROJECT environment variable your project name. Or, just change the the source code to hard code it.
  • Give the following roles to the newly created service account. Only one of the below bullets should be satisfied. (If you already created an account or not sure if you gave the right role, go to “IAM & Admin” in the cloud console. There, you’ll see the account.)
    • On the project: Project Billing Manager + Project Viewer OR Project Owner
    • On the target Cloud Billing account: Billing Account Administrator

Now, it’s time to test. Since we don’t want to stop the service actually, we’ll pass numbers that didn’t reach the limit. Note that costAmount < budgetAmount in the below for that reason.

Be sure to change the “budgetDisplayName” as your topic name! If you forgot the name, go to “Topics” in the console and pick the “Topic name”.

{
    "budgetDisplayName": "name-of-budget",
    "alertThresholdExceeded": 1.0,
    "costAmount": 50.0,
    "costIntervalStart": "2019-01-01T00:00:00Z",
    "budgetAmount": 100.00,
    "budgetAmountType": "SPECIFIED_AMOUNT",
    "currencyCode": "USD"
}

For publishing that message, I find the console easy.

Once done, go to your cloud function, open the log tab and check the following message of “No action necessary. (Current cost: 50.0). You may need to wait or scroll down to find the lastest log.

Congrats! That’s it!