Start-Stop Cloud SQL with Cloud Scheduler

Art Krisada
3 min readDec 4, 2022

--

To lower the cost, we shut down Cloud SQL when it not in used. You can use Cloud Scheduler to start/stop Cloud SQL.

First you must enable Cloud SQL Admin API. You can use Search > Cloud SQL Admin API if you can not find it from the menu.

Then, Select Cloud Scheduler from the menu and Create Job. This one will be STOP Cloud SQL Job.

Fill in Name, Region, Frequency and Timezone. Frequency is in crontab style. You can go to https://crontab.guru/ for help you setting.

Next, Configure the execution for STOP your DB.

Select Target type = HTTP

URL = https://sqladmin.googleapis.com/sql/v1beta4/projects/your-project-id/instances/your-db-instance-id

HTTP metohd = PATCH

HTTP headers

Name = Content-Type

Value = application/json; charset=utf-8

Body =

{
"settings": {
"activationPolicy": "NEVER"
}
}

Auth header = Add OAuth token

Service account = Compute Engine default service account

For optional settings, your can leave it as default.

After you create the schedule, It will appear in Cloud Scheduler List.

For setting DB START schedule your can do it the same way as STOP.

The different is in Body.

Body =

{
"settings": {
"activationPolicy": "ALWAYS"
}
}

--

--

Art Krisada
Art Krisada

Written by Art Krisada

Never stop learning, because life never stop teaching.

Responses (1)