Cut Cloud Run service from running - safety reasons

Viewed 2585

Let's assume, I run a Cloud Run service of Google. Let's also assume someone wants to really harm you and finds out all API routes or is able to send a lot of post-requests by spamming the site.

There is a Email notification, which will popup on certain limits you set up before.

Is there also a way to automatically cut the Cloud Run service, or set it temporarily offline? I couldn't find any good resource or solution to this.

2 Answers

There are several solution to remove from traffic Cloud Run service, in addition of authentication solution proposed by Dondi

You can't really "turn off" a Cloud Run service as it's fully managed by Google. A Cloud Run instance automatically scales down to zero if there are no requests, but it will continue on serving traffic.

To emulate what you want to do, make sure that your service requires authentication then revoke access on the offending user (or all users). As mentioned in the docs:

Cloud Run (fully managed) does not offer a direct way to make a service stop serving traffic, but you can achieve a similar result by revoking the permission to invoke the service to identities that are invoking the service. Notably, if your service is "public", remove allUsers from the Cloud Run Invoker role (roles/run.invoker).

Update: Access to a resource is managed through an IAM policy. In order to control access programmatically, you have to get the IAM policy first, then revoke the role to a user or a service account. Here's the documentation that gives an overview.

Related