

) Authorization for the Cloud FunctionĪt runtime, the Cloud Function uses a service account, which is listed at the bottom of the Cloud Function details page in the Google Cloud Platform Console. The payload that comes from the Pub/Sub topic looks like this.

I’m not going to explain all the lines of code, basically, we have a Pub/Sub trigger Cloud Function, when data starts flowing from billing it will trigger the function to execute, the cloud function just lives for a short time. I also use Environment variables to set my Environment variables.
#Google cloud money quota code#
The Cloud Function Code in Pythonīelow is the code for the cloud function, I’m using the Secret Manager to manage my Slack Hook Key, the mailgun credentials as I using Slack and Mailgun to send me a notification at the same time as I disable the billing account, hopefully, I will catch the notification on these mediums in time, so I can analyze why the billing was shutdown and enable billing if required. The google-cloud-secret-manager I’m using to interact with the secret manager. I’m using the oauth2client to authenticate the function with GCP so I can use the google-api-python-client to disable the Billing API. I’m using three different Python libraries for the Cloud Function. You need this when you publish your Cloud Function to GCP. Now you have the Secret Manager for Python installed in your environment, next you have to create your requirements.txt.

#Google cloud money quota how to#
You can read how to use the Secret Manager in this article Using Secret Manager in a Google Cloud Function with Python. I store all my secrets for the Cloud Function with the Secret Manager. When you created a budget alert, you had that the option to create a Pub/Sub topic – you can now navigate to Pub/Sub and look at your topic and to verify after you have published your Cloud Function that the Cloud Function is subscribed to your Pub/Sub topic.įor the Cloud Function to be able to disable billing, we need to enable the Cloud Billing API, navigate to Google Cloud Platform Console APIs & Services, and enable the Cloud Billing API. Now you Billing Alert is setup, lets move on. When you save the billing alert you would see something like this. Budget AlertsĬreate an alert use the project id as the name of your billing alert and select the alert to be sent to Pub/Sub – named the Sub/Pub topic to budgets-notifications. The first thing we need to do is to create a budget alert for our GCP Billing, in the Google Cloud Platform Console navigate to Billing -> Budgets & alerts. Let’s get started to set up all the GCP services to tie all the pieces together so that you will be able to disable billing for your GCP project. Say you are running your WordPress site on the Google Compute Engine, your WordPress site will go down, be careful when using the Cloud Function to disable Billing. It will shut down any services that you have on the GCP project. One word about disabling Billing through the Google Billing API, when you programmatically send a diable billing through the BIlling API, your billing account will go from active to inactive. If the total spend of a project exceeds the budget fixed for the project, the Billing will be disabled for the project using Cloud Billing API and also send a notification.

In the GCP I’m using the following services as show in the diagram below: Disabling Billing If it happens again, I now have a Cloud Function that will stop the billing and limit the cost. Google was very good with my Cloud Function that went down, I contacted their billing department and explained what happened, and they paid back the money. I published it, and there was something wrong in the code within minutes the incorrect code had generated over 100 dollars to my billing in seconds, things happen it’s better to be sorry and to play it safe than just go in a wimp till you get a bill and you say what did happen? I had an issue once when I wrote a Cloud Function.
#Google cloud money quota update#
There are times when you may have installed an update to the Cloud Engine App that occurs on the network edge, starting to generate a large amount of traffic, and your billing increases dramatically. You may ask why do I want to do this you can set up billing notifications in GCP that send you an email when your budget limits are achieved, yes that works well sometimes. In this article, I will explain how you can create a Google Cloud Platform (GCP) Cloud Function that you can use to cap the cost if the cost is higher than the budget the billing will be disabled.
