We have setup a Cloud sql database for development but would like to save cost by shutting it down during night. This article shows how to achieve this.

The overall architecture is Cloud function is used to hold Python code to start/stop the instance; Cloud scheduler is used to schedule these two tasks; Pub/Sub is used to bridge Cloud function and scheduler. (HTTP may be considered but it’s open to the internet so may be more suitable for open API related)

Below is how to configure the cloud function

Below is the actual code

To test the function, you’ll need to pass parameter START or STOP, below shows the GUI. Note ‘STOP’ is base64-encoded string, you can use any online tool e.g. https://www.base64encode.org/

Finally we can setup Cloud scheduler. Note the topic should be the same as what you specified in Cloud function above, payload is the parameter we want to pass to our Cloud function.

All done!

Use Cloud function to start and stop Cloud sql
Tagged on:                                 

Leave a Reply

Your email address will not be published. Required fields are marked *

24 − 18 =

This site uses Akismet to reduce spam. Learn how your comment data is processed.