GCP auto shutdown and startup using Google Cloud Schedulers
Asked Answered
O

6

9

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

Oakley answered 13/11, 2019 at 5:33 Comment(3)
Auto shutdown what? a Cloud SQL, a Compute Engine Instance? a government? Please elaborate more on your questionMarcelinomarcell
Can you provide more details in your wish?Disown
Really sorry for the lack of information, i want to start/stop set of vm's using cloud scheduler.Oakley
C
1

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

Congelation answered 16/6, 2024 at 4:19 Comment(0)
M
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!

Marcelinomarcell answered 13/11, 2019 at 14:27 Comment(6)
Thanks Chris32 and pradeep, Finally able to resolve it with you guys help :)Oakley
link broken in this replyHalloo
The 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)Inane
@JonášJančařík you are right. this is the right tutorialGodship
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 btwHenriettahenriette
It's possible now, see https://mcmap.net/q/1130218/-gcp-auto-shutdown-and-startup-using-google-cloud-schedulersInane
D
5

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

Dysfunction answered 29/11, 2020 at 17:21 Comment(3)
Works for me. Nice and easy. Thanks !Beggarweed
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."Mirk
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.Haletky
T
5

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

Tweeny answered 13/5, 2021 at 18:31 Comment(0)
H
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

Hymenopterous answered 7/7, 2020 at 11:7 Comment(1)
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 SchedulerHaletky
D
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.

Dyslogistic answered 13/11, 2019 at 16:34 Comment(0)
C
1

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

Congelation answered 16/6, 2024 at 4:19 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.