Jump to Content
Storage & Data Transfer

Save money by stopping and starting Compute Engine instances on schedule

June 6, 2019
Hicham Bougdal

Cloud Consultant

Jani Patokallio

Solutions Architect

A key feature of running virtual machine instances (VMs) in the cloud is that you only pay for the compute resources you use. On Google Cloud Platform (GCP), Compute Engine VMs are charged per second. Production systems tend to run 24/7; however, some VMs, like those in development or test environments, are typically used only during business hours. Keeping them running in the middle of the night or on weekends serves no useful purpose, so turning them off saves money. But stopping and starting large groups of VMs by hand every day can be tedious and hard to encourage or enforce across an organization.

Cloud Scheduler, GCP’s fully managed cron job scheduler, provides a straightforward solution for automatically stopping and starting VMs. By employing Cloud Scheduler with Cloud Pub/Sub to trigger Cloud Functions on schedule, you can stop and start groups of VMs identified with labels of your choice (created in Compute Engine). Here you can see an example schedule that stops all VMs labeled "dev" at 5pm and restarts them at 9am, while leaving VMs labeled "prod" untouched:

https://storage.googleapis.com/gweb-cloudblog-publish/images/GCP_cloud_scheduler.max-1400x1400.png

Using this setup gives you a high degree of flexibility, since you can assign different schedules for different groups of VMs by creating multiple Cloud Scheduler cron jobs.

This also helps give you more control over Compute Engine VMs, since the functions use a GCP service account to start and stop Compute Engine instances. Using Cloud Identity and Access Management (IAM), you can follow the principle of least privilege and limit the permissions granted to this service account to the minimum necessary.

Since the shutdown is planned, the VMs will run shutdown scripts to ensure they're in a clean state. Any attached disks with auto-delete turned off will retain their contents and be available again when the VM is restarted. Last but not least, if someone needs a development or test VM at odd hours, you can temporarily remove the label in advance or simply restart the VM if it has already stopped.

To learn more about what you can accomplish with Cloud Scheduler, check out Reliable task scheduling on Compute Engine with Cloud Scheduler.

Posted in