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
)
Hi, this code really help me with the implementation I'm doing for my team, thanks!
I have a doubt about the payload, it should be in JSON format?
I'm sending:
start my_instance_name (of course I put my instancte name there)
and it's not working.
or should I have to put something like this??
{ "data": "stop my_instance_name"}
When I use this last payload example I got this message error:
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)
NameError: name 'stop' is not defined"
Thanks!