I want to start/stop a set of Compute engine instances in Google Cloud Platform using Google Cloud Scheduler. How can I do it?
-
2Auto shutdown what? a Cloud SQL, a Compute Engine Instance? a government? Please elaborate more on your question– Chris32Nov 13, 2019 at 7:30
-
Can you provide more details in your wish?– guillaume blaquiereNov 13, 2019 at 8:05
-
Really sorry for the lack of information, i want to start/stop set of vm's using cloud scheduler.– anXlerNov 13, 2019 at 9:31
5 Answers
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!
-
Thanks Chris32 and pradeep, Finally able to resolve it with you guys help :)– anXlerNov 19, 2019 at 7:46
-
-
1The linked article (cloud.google.com/blog/products/storage-data-transfer/…) is not really a tutorial, it only outlines the basics. This one is a lot more descriptive cloud.google.com/scheduler/docs/… (thanks pradeep for linking it in his answer) Sep 2, 2020 at 12:53
-
-
Setting this up is like a full time job. Why isn't there an "Auto-Shutdown" one-click solution like in Azure? This totally sucks btw– A XMar 17, 2021 at 21:19
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
-
Create a new topic if not created...
That's the hard part. That's the question. That part requires about 15+ minutes of lock-in happy acrobatics, ask me how I know. Day later: This is a good thing to bring up, but its' not the full answer. Pub/Sub is a webhook proxy with a great management UI. It does't do anything else. Luckly, you don't need anything else... Use a service account to send a POST request to call the shutdown, that's your Pub/Sub subscription. The order of the answer is confusing... Start with a Pub/Sub topic, then go to Cloud Cronjob Scheduler– Ray FossMar 11, 2021 at 22:57
I use vm instance API directly. No need for a cloud function.
Here is the link to the api description: https://cloud.google.com/compute/docs/reference/rest/v1/instances/stop
The API Call: POST https://compute.googleapis.com/compute/v1/projects/{project}/zones/{zone}/instances/{resourceId}/stop
You can start the engine in a similiar way.
Example how to configure the scheduler:
-
-
just a note / FYI, from this article -- cloud.google.com/scheduler/docs/… --- google says that ---- "Important: HTTP functions in Cloud Functions have no authentication and are not secure. HTTP functions are unprotected and will respond to any HTTP request, which means anybody on the internet could start and stop your Compute Engine instances." Feb 11, 2021 at 16:46
-
That was easy. It's strange that you need to create a service account for this... It goes through the back and authenticates it without keys anyway. They are so close to making this super easy.– Ray FossMar 10, 2021 at 14:34
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.
The new Google Compute Engine feature of Instance Schedules can now be used to start and stop instances through the Cloud Console UI, using gcloud or via the API:
https://cloud.google.com/compute/docs/instances/schedule-instance-start-stop