Better method to schedule instance stop and start in Google Compute Engine

google-cloud

Currently I schedule my instance shutdown in Google Cloud Platform (GCP) the following way via /etc/crontab:

30 23 * * * sudo poweroff

This is done inside GCP instance.

Is there better way to do it using GCP-API? And how can I schedule the instance starting? I can't seem to do that even with crontab.

Best Answer

There is a great tutorial in the Google Cloud Site. The address is

https://cloud.google.com/scheduler/docs/start-and-stop-compute-engine-instances-on-a-schedule

Extra note to help with the tutorial:

When you get to the part where you are setting up the Functions. You may need to do base64 conversion if you are using the free tier option, the tutorial is for the west zone and free tier is only available in the central zone. Also once you have the string converted you need to remove the OK from the end and add o=

There are my notes of the process:

sample string:

{"zone":"us-west1-b", "label":"env=dev"}

sample as base 64

{"data":"eyJ6b25lIjoidXMtd2VzdDEtYiIsICJsYWJlbCI6ImVudj1kZXYifQo="}

converter url:
https://www.base64encode.net/

results using sample string above

eyJ6b25lIjoidXMtd2VzdDEtYiIsICJsYWJlbCI6ImVudj1kZXYifQ0K
or
eyJ6b25lIjoidXMtd2VzdDEtYiIsICJsYWJlbCI6ImVudj1kZXYifQ==

The results are the same as the sample except the different in the last to characters.

desired input string:

{"zone":"us-central1-a", "label":"env=dev"}

output base 64 string

eyJ6b25lIjoidXMtY2VudHJhbDEtYSIsICJsYWJlbCI6ImVudj1kZXYifQ0K

Add the JSON formatting and adjust the last to characters:

{"data":"eyJ6b25lIjoidXMtY2VudHJhbDEtYSIsICJsYWJlbCI6ImVudj1kZXYifQo="}

Hope it helps you get the base64 string conversion bit.