I need to develop a Cloud Function script to stop billing for specific services in case of cost explosion.
Example: Imagine that Pub/Sub, for some reason, has a big cost.
My Cloud Function has to detect this event (I already know how to do it) and disable only this service billing.
Is there a way to do that? I saw I can disable API Service . Would It be possible to disable the pub/sub API service using Cloud Function? Is there a code example? Would It disable billing for this service? Or a better approach would be delete the problematic pub/sub?
When you disable an API you may delete all the resources that were created by the API, and you will also disable other APIs that are dependent of the API you are disabling. Depending on the products you are using in your project in the specific case you mention, if you disable the Pub/Sub API you'll may disable the following APIs:
cloudbuild.googleapis.com
cloudfunctions.googleapis.com
containerregistry.googleapis.com
run.googleapis.com
...among others
If you are aware of the risks and disruption that disabling the API (and therefore all the other dependent APIs) may cause on your Project if you have something in production the following code using the Service Usage API's disable method from the Python Client Library will work will disable the Dataflow API:
from googleapiclient import discovery
import google.auth
def hello_world(request):
credentials, project_id = google.auth.default()
name = 'projects/[PROJECT-NUMBER-NOT-ID]/services/dataflow.googleapis.com'
body = {'disableDependentServices': True,}
service = discovery.build('serviceusage', 'v1', credentials=credentials, cache_discovery=False)
request = service.services().disable(name=name, body=body)
try:
response = request.execute()
except Exception as e:
print(e)
return "API disabled"
By checking your activity logs you should see similar messages to the ones below after triggering the Cloud Function:
9:12 AM Completed: google.api.serviceusage.v1.ServiceUsage.DisableService [PROJECT-ID]@appspot.gserviceaccount.com has executed google.api.serviceusage.v1.ServiceUsage.DisableService on dataflow.googleapis.com
9:12 AM google.api.serviceusage.v1.ServiceUsage.DisableService [PROJECT-ID]@appspot.gserviceaccount.com has executed google.api.serviceusage.v1.ServiceUsage.DisableService on dataflow.googleapis.com
Notice that depending on the product you are using this method of disabling the API will may stop all the billing charges associated with for example network traffic, but in general charges like storage pricing in a Cloud SQL instances will still continue to accrue.
In general it is my personal opinion that this is not the best approach (since disabling APIs can be quite dangerous if you have an application in production and other APIs depend on this API) and I would in general consider using budgets and budget alerts (that also use Cloud Functions and other service like Pub/Sub in order to receive notifications). Find all the relevant section of the documentation regarding budgets and budget alerts here and here.