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: