To use this cloud function follow these steps:
- Create a pub/sub topic which will be used to trigger the cloud function.
- Create the cloud function and copy in the code below.
- Make sure to set the correct project ID in line 8.
- Set the trigger to Pub/Sub and choose the topic created in step 1.
- Create a cloud scheduler job to trigger the cloud function on a regular basis.
- Choose the frequency when you want the cloud function to be triggered.
- Set the target to Pub/Sub and define the topic created in step 1.
- The payload should be set to
start [CloudSQL instance name]
orstop [CloudSQL instance name]
to start or stop the specified instance (e.g.start my_cloudsql_instance
will start the CloudSQL instance with the namemy_cloudsql_instance
)
thanks!! The indentation in my code was wrong, that's why.
now I got another error, it seems it's related not enough permissions to perform the patch action. I have to check that.
"Traceback (most recent call last):
File "/layers/google.python.pip/pip/lib/python3.9/site-packages/flask/app.py", line 2529, in wsgi_app
response = self.full_dispatch_request()
File "/layers/google.python.pip/pip/lib/python3.9/site-packages/flask/app.py", line 1825, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/layers/google.python.pip/pip/lib/python3.9/site-packages/flask/app.py", line 1823, in full_dispatch_request
rv = self.dispatch_request()
File "/layers/google.python.pip/pip/lib/python3.9/site-packages/flask/app.py", line 1799, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
File "/layers/google.python.pip/pip/lib/python3.9/site-packages/functions_framework/init.py", line 171, in view_func
function(data, context)
File "/workspace/main.py", line 18, in start_stop
stop(instance_name)
File "/workspace/main.py", line 28, in stop
patch(instance_name, "NEVER")
File "/workspace/main.py", line 43, in patch
response = request.execute()
File "/layers/google.python.pip/pip/lib/python3.9/site-packages/googleapiclient/_helpers.py", line 134, in positional_wrapper
return wrapped(*args, **kwargs)
File "/layers/google.python.pip/pip/lib/python3.9/site-packages/googleapiclient/http.py", line 907, in execute
raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://sqladmin.googleapis.com/sql/v1beta4/projects/instances/?alt=json returned "The client is not authorized to make this request.">"