I want to start/stop a set of Compute engine instances in Google Cloud Platform using Google Cloud Scheduler. How can I do it?
There has been a steady improvement on this in the years since the question was posted. As of today, you can attach a schedule, or multiple schedules, directly to a vm via the console in the console; there's no need to use API auth or gcloud commands.
Navigate to the 'VM Instances' page (not an individual instance details page) and select the 'Instance Schedules' tab on the top of that page, then select 'Create Schedule'
This feature will start your instance and then stop it after a predetermined amount of time. That is, there is not an independent schedule for stopping an instance.
https://cloud.google.com/compute/docs/instances/schedule-instance-start-stop
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!
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:
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
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 –
Haletky 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.
There has been a steady improvement on this in the years since the question was posted. As of today, you can attach a schedule, or multiple schedules, directly to a vm via the console in the console; there's no need to use API auth or gcloud commands.
Navigate to the 'VM Instances' page (not an individual instance details page) and select the 'Instance Schedules' tab on the top of that page, then select 'Create Schedule'
This feature will start your instance and then stop it after a predetermined amount of time. That is, there is not an independent schedule for stopping an instance.
https://cloud.google.com/compute/docs/instances/schedule-instance-start-stop
© 2022 - 2025 — McMap. All rights reserved.