Cloud function set limit max instances

Viewed 326

I am currently trying to figuring out a way to limit cloud function request to protect myself from massive bills. What if I set Max instances to 0 will my function still take request and if yes then can I add a counter increment in firestore when it reaches expected limit cloud function will set Max instances 0 will it save me from surprise bill

Is there any other service which has limit options I don't want pay as u go

1 Answers

What if I set Max instances to 0 will my function still take request?

I doubt if you can actually set it to 0 at first place. If you hover on the info (❔) icon on Autoscaling input field, it says "Leave empty or set to 0 to clear this control and automatically create new instances as needed." That essentially means entering 0 will remove the max limit.

can I add a counter increment in firestore when it reaches expected limit cloud function

You could keep track of total number of invocations by incrementing the count in database after every invocation and try using the Cloud Functions REST API to update the maxInstances to a much lower number (not 0 for the reason explained above) as required.

From the documentation,

When you specify a max instances limit, you are specifying an upper limit. Setting a large limit does not mean that your function will scale up to the specified number of instances. It only means that the number of instances that co-exist at any point in time should not exceed the limit

In some cases, such as rapid traffic surges, Cloud Functions may, for a short period of time, create more instances than the specified max instances limit. If your function cannot tolerate this temporary behavior, you may want to factor in a safety margin and set a lower max instances value than your function can tolerate.

Related