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:                                 

3 thoughts on “Use Cloud function to start and stop Cloud sql

  • July 23, 2020 at 4:01 pm
    Permalink

    The structuring of your article is quite good. The use of images are quite supporting in understanding the process. Thanks for sharing. For base64-encoded string, you can also refer to that link
    https://url-decode.com/tool/base64-encode where you also find more tools related to that.

    Reply
  • August 29, 2020 at 8:37 pm
    Permalink

    A very good solution to reduce my cost. I was looking for a solution for this issue and at last found it here. I did it successfully and images supported me a lot. Thanks a lot.
    I am thinking to connect it with JavaFX program. Is Java good for database?

    Reply
    • August 30, 2020 at 10:07 am
      Permalink

      I think if you’re good at Java then keep using it; For me, Java is becoming too complex and my daily language is Python

      Reply

Leave a Reply

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

− 1 = 2

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