73

Some functions in the Google Developers Console, like the Analytics API, are free until you reach a quota. Other functions, like Google Cloud Storage, create costs from the first click.

When I upload a file under https://console.developers.google.com/ > Storage > Cloud Storage > Storage Browser and I make this file publicly available, I pay about $0.12 per GB traffic.

But theoretically the traffic to this link could explode, e.g. because of sudden popularity. Therefore I would like to set something like a daily or monthly cost limit.

Q: How do I protect myself from overly high costs in the Google Developers Console?

John Hanley
  • 74,467
  • 6
  • 95
  • 159
ptmr.io
  • 2,115
  • 4
  • 22
  • 34
  • 3
    As of July/2017, it still is not possible to limit GCE usage by budget. If some evil ones DDOS your application/database (e.g. download 1MB file again and again from hundreds of hosts/threads), it is possible that you will see hundreds to thousands of dollars of a bill after you wake up in the morning. Not only that, you won't simply be able to turn your app back on again, until you find a way to resolve that problem because DDOS can come back anytime later as your app gets bigger. – user482594 Jul 24 '17 at 06:10
  • 7
    I'm reluctant to use gcp if I can't even cap my spending in currency units. – Be Kind Apr 29 '19 at 09:51

7 Answers7

34

You cannot. I asked Google about this, here's their response, from May 7 2016:

(GCE = Google cloud engine. No spending limits.
GAE = Google app engine — yes it has spending limits.)

... you are eligible for support on ... only ...
... [various helpful links] ...

That been said, at the moment there is no a feature that allows you to configure a limited budget on GCE. This feature is certainly available for GAE [1]. As you mentioned in your comments, you either can totally shut down your VMs (will depend on your use case) or set the VMs to send you alerts if they reach a certain traffic limit [2].

Sincerely,

Someone's first name
Technical Solutions Representative
Google Cloud Platform

[1] https://cloud.google.com/appengine/docs/quotas
[2] https://cloud.google.com/monitoring/support/notification-options

@wmdry, you wrote: "traffic to this link could explode" — I'm afraid of this too. That's why I asked Google about this. And I'm planning to avoid Google's CDN because of this, and use another CDN provider instead, which has spending limits. Because, unlike Nginx, I don't see any way for me to rate limit / throttle Google's CDN.

I do plan to use GCE (Google Cloud Engine) though. Therefore, right now I'm reading about how to rate limit my Nginx server. Because if I just configure Nginx correctly, then those $0.12 / GB you mentioned, cannot possible explode to ... like $10k in a month? What if Google sends a $10k bill when I'm back from an a few week's vacation, just because of my hobby project and a few people downloading a 1 MB movie over and over again forever (because: evil). Hmm, & the bigger & faster my servers, the higher the risk.

I hope Google will add spending limits, because I did want to use Google's CDN.

Update 2020: Apparently this does bite people from time to time — look here:
"Burnt $72k testing Firebase and Cloud Run and almost went bankrupt", Dec 08, 2020, https://news.ycombinator.com/item?id=25372336, In that case, they could contact Google and in the end didn't need to pay.

KajMagnus
  • 11,308
  • 15
  • 79
  • 127
22

As of July 2017 you can set budgets that send notifications via email but do not cap spending:

screenshot of GCS documentation warning that budget does not cap API usage

To set an alert-only budget, which will not cap spending:

  1. Go to the Cloud Platform Console.
  2. Open the console left side menu and click Billing
  3. If you have more than one billing account, click the billing account name.
  4. On the left, click Budgets & alerts.

Official help page: https://support.google.com/cloud/answer/6293540?hl=en

Isaiah Norton
  • 4,205
  • 1
  • 24
  • 38
ptmr.io
  • 2,115
  • 4
  • 22
  • 34
20

I found that Google's documentation now provides two methods to actually limit the cost of a GCP project. It involves the following setup:

  1. Create a Cloud Function that checks the cost against the budget, and carries out a certain action if the cost exceeds the budget. Google's Documentation provides a sample code snip that can either shutdown all VM instances in a Project or disable the billing for a project. Shutting down all VMs would stop all VM-related cost but you get to keep your data (and still have to pay for the storage). Disabling the billing for a project would effectively zap all cost-related activities and you could lose data. You can name the Cloud Function "budget-enforcer".
  2. The Google code snip as provided above has a hard coded ZONE variable. Remember to change it to match your zone!
  3. Create a Service Account to run the Cloud Function "budget-enforcer". For shutting down VMs, the Service Account would need role "Compute Instance Admin (v1)". For disabling billing on a project, the Service Account would need role "Project Billing Manager".
  4. Set a Topic for the Cloud Function (I call mine "proj-name-stop-vm" and "proj-name-disable-bill").
  5. Set up a budget alert as usual, and connect it to one of the Pub/Sub topic above.

Please be noted that Google's documentation did mention that there could be a delay between the cost exceeds a budget and the function is triggered, so you should build in a buffer if you have an absolute hard cost limit. I use 90% of the budget as the trigger line for shutting down my instances.

Yan Li
  • 762
  • 6
  • 10
  • Yan -- I tried this, but am getting a "KeyError": https://stackoverflow.com/questions/60099501/disabling-billing-on-google-cloud-using-google-cloud-function-keyerror-data any chance you know what's going on? – Vincent Feb 06 '20 at 19:51
  • 1
    I've commented on your question. – Yan Li Feb 08 '20 at 04:38
12

The API usage can be limited with a hard limit:

Depending on the API, you can explicitly cap requests in a variety of ways, including: requests per day, requests per 100 seconds, and requests per 100 seconds per user. You might want to limit the billable usage by setting caps. For example, to prevent getting billed for usage beyond the free courtesy usage limits, you can set requests per day caps

Source

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Florian
  • 874
  • 1
  • 8
  • 17
  • 1
    Documentation says: "**Quota limits are not always entirely precise**, because there is some latency between when a quota is surpassed and when the enforcement begins. When using quota as a tool to limit billing, allow some buffer." https://cloud.google.com/apis/docs/capping-api-usage There are no details in the doc what "some buffer" exactly means. => Actually, there is no hard limit !? – Stefan Jul 06 '21 at 08:25
  • I just chatted with support they said a buffer of 20 % is fine. If I use the following three APIs and corresponding limits per day, I won't reach the free total limit of $200 per month. * Distance Matrix API: 170 Elements per day * Directions API: 170 Elements per day * Geocoding API: 340 requests per day – Stefan Jul 06 '21 at 09:42
6

You can combine budget pub/sub alerts with a cloud function that can disable billing on your entire account if a threshold is met.

Full Tutorial Here: https://www.youtube.com/watch?v=KiTg8RPpGG4

GitHub Repo Here: https://github.com/aioverlords/Google-Cloud-Platform-Killswitch

To Disable Billing

const _disableBillingForProject = async projectName => {
const res = await billing.updateBillingInfo({
    name: projectName,
    resource: {
        billingAccountName: ''
    }, // Disable billing
});
console.log(res);
console.log("Billing Disabled");
return `Billing disabled: ${JSON.stringify(res.data)}`;
};
Timothy Moody
  • 479
  • 1
  • 7
  • 9
  • Great idea actually, although I have one suggestion, since we should follow the principle of least privilege, we should ideally have a service account attached to that Cloud Function which will only have the required permissions. https://cloud.google.com/billing/docs/how-to/billing-access Though as you mentioned, there's still a huge issue as the budget alerts isn't real time, but it is the next best thing. Maybe we can have a higher threshold at which our budget alert is triggered, so the notification gets triggered earlier and as a consequence, this CF is executed earlier. – Anshuman Kumar Dec 31 '22 at 06:32
4

Simply go to the developer console:

https://console.developers.google.com/project

  • Select your project.
  • Select "billings & settings"
  • Enable billing.

Then go to Compute/AppEngine/Settings and set a daily budget.

enter image description here

Paul Collingwood
  • 9,053
  • 3
  • 23
  • 36
  • 1
    I do not have this option. I have never created an app engine application. I use the cloud storage function. This is my sidebar: [link](http://i.imgur.com/DvuSvvV.png) – ptmr.io Dec 23 '14 at 11:23
  • You have an app-engine tag however. – Paul Collingwood Dec 23 '14 at 12:14
  • Try to create a new project from the google dev console. This is not the same as a new app engine project and therefore the option you described will not appear (afaik, see my link/screenshot above) – ptmr.io Dec 23 '14 at 17:41
  • it asks me for a billing account now on doing that, so things seem to have changed a little lately. – Paul Collingwood Dec 23 '14 at 18:00
  • 1
    Does this setting really affect cloud storage though? Isn't this just for app engine? – quano May 06 '15 at 14:55
  • Excellent question. you "enable billing" but yes, I don't know the answer. – Paul Collingwood May 06 '15 at 18:40
  • @quano as I understand it, Google calls the whole project (for which you activate billing for) a "App". So this billing limit should effect every used service (no matter if Storage, Compute, Big Data, etc.) within the "App" (project). But if you want to be 100% sure I would suggest you to contact Google support directly (they should know this for sure :D ). – Smort May 08 '16 at 19:15
  • 8
    I'm fairly sure this answer is incorrect. The spending limits are just for AppEngine, not for GCE in general. See my answer below — I actually contacted Google, and asked. – KajMagnus Jun 30 '16 at 14:43
  • @quano Yes only for app engine. GCE = no spending limits. See my answer below. – KajMagnus Jun 30 '16 at 14:44
-2

Go to Google Cloud console, and then to Billing / Budgets and Alerts and create a new budget for one or all your projects. You can select which services should be included in the limit and set a monthly amount that should not be exceeded.

enter image description here

ivanacorovic
  • 2,669
  • 4
  • 30
  • 46
  • Thanks! just to be clear, this means that no matter what (assuming i selected all projects and services). I will not pay above the cost I've specified, right? – MTZ4 Jan 21 '21 at 17:53
  • 1
    @MTZ4 I think so, but you might want to check with someone from Google. – ivanacorovic Jan 22 '21 at 20:39
  • 24
    This option just sends you notifications, it doesn't limit your spending. – Stefan Feb 01 '21 at 18:48