Storage & Data Transfer

Save money by stopping and starting Compute Engine instances on schedule

Compute.png

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:

GCP cloud scheduler.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.

For a sample implementation of this design pattern, check out Scheduling compute instances with Cloud Scheduler. For more about what else you can accomplish with Cloud Scheduler, check out Reliable task scheduling on Compute Engine with Cloud Scheduler.