7

I want to start/stop a set of Compute engine instances in Google Cloud Platform using Google Cloud Scheduler. How can I do it?

Chris32
  • 4,716
  • 2
  • 18
  • 30
anXler
  • 175
  • 1
  • 1
  • 12

5 Answers5

5

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.

  1. Set up your Compute Engine instances
  2. Deploy the starter Cloud Function. You can see an example in here
  3. Deploy the stop Cloud Function. You can see an example in here
  4. Set up the Cloud Scheduler jobs

If you need any help with the tutorial please just let me know!

Chris32
  • 4,716
  • 2
  • 18
  • 30
  • Thanks Chris32 and pradeep, Finally able to resolve it with you guys help :) – anXler Nov 19 '19 at 07:46
  • link broken in this reply – Sam Shleifer Jun 05 '20 at 19:51
  • 1
    The linked article (https://cloud.google.com/blog/products/storage-data-transfer/save-money-by-stopping-and-starting-compute-engine-instances-on-schedule) is not really a tutorial, it only outlines the basics. This one is a lot more descriptive https://cloud.google.com/scheduler/docs/start-and-stop-compute-engine-instances-on-a-schedule (thanks pradeep for linking it in his answer) – Jonáš Jančařík Sep 02 '20 at 12:53
  • @JonášJančařík you are right. this is the right tutorial – Amitk Nov 04 '20 at 14:49
  • 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 X Mar 17 '21 at 21:19
  • It's possible now, see https://stackoverflow.com/a/67524396/1334688 – Jonáš Jančařík Aug 27 '21 at 11:53
4

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

Masood
  • 121
  • 4
  • `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 Foss Mar 11 '21 at 22:57
4

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:

enter image description here

nikelone
  • 121
  • 3
  • Works for me. Nice and easy. Thanks ! – Damien Dec 21 '20 at 04:13
  • just a note / FYI, from this article -- https://cloud.google.com/scheduler/docs/start-and-stop-compute-engine-instances-on-a-schedule --- 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." – Yev Guyduy Feb 11 '21 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 Foss Mar 10 '21 at 14:34
3

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.

Pradeep Bhadani
  • 4,435
  • 6
  • 29
  • 48
3

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

enter image description here

JonathanSimon
  • 326
  • 1
  • 5