GCP auto shutdown and startup using Google Cloud Schedulers
In order to start and stop a Compute Engine using the Cloud Scheduler you can follow Google this tutorial, or this other
I won’t be copy-pasting the required code here because the tutorial it's very complete but I will resume here the steps to follow.
- Set up your Compute Engine instances
- Deploy the starter Cloud Function. You can see an example in here
- Deploy the stop Cloud Function. You can see an example in here
- Set up the Cloud Scheduler jobs
If you need any help with the tutorial please just let me know!
You can look at Google Article to achieve your goal https://cloud.google.com/scheduler/docs/start-and-stop-compute-engine-instances-on-a-schedule.
Also, If these VM instances are stateless then I would suggest to look at Google Cloud Run service which can help you to save cost and operation overhead to configure auto-shutdown/auto-startup.
Hope this helps.
I still wonder why gcp has still not have this feature in the first place. Anyways These simple steps did the job for me
- Create a new JobScheduler.
- Fill in the required details
- Choose frequency which suits your requirement.
- Choose the target to Pub/Sub.
- Choose the topic name (Create a new topic if not created ).
- In the payload section use this stop script
gcloud compute instances stop instance-name
.
To verify the change you can run the job manually and check