How-Tos

[How to] Schedule Start/Stop for your VMs

How to Series

Create a Schedule to Start and Stop your VMs/Instances as per your requirements

1*5O6cMZNkJ9EpwGdg67yYpw
Image generated using Gemini

Running your virtual machines (VMs) 24/7 in the cloud can get expensive, especially if they’re only needed during specific times, like a standard work day. By scheduling your VMs to start and stop as needed, you can significantly optimize costs and improve resource management. Google Cloud Platform (GCP) provides a native feature called Instance Schedules that makes this automation straightforward. This blog post will walk you through the process.


What is an Instance Schedule?

An Instance Schedule is a resource policy in Google Cloud that defines a set of start and stop times for your VMs. Think of it as a set of instructions that you can attach to one or more virtual machines, telling them when to turn on and when to turn off.

Why Use Instance Schedules?

  • Cost Savings: This is the most significant benefit. By only running your VMs when they’re actively used, you reduce the billed hours from 24/7 to the hours they are actually running. For a development or testing environment used only during business hours, this can lead to substantial savings.
  • Resource Management: Automating the start and stop process frees you from the manual task of managing your VMs. This ensures that resources are available when your team needs them and are shut down when they don’t, improving overall efficiency.
  • Predictable Workloads: Instance schedules are ideal for predictable workloads, such as a development server needed from 9:00 AM to 5:00 PM, Monday through Friday.

Step-by-Step Guide to Scheduling Your VMs

The process is simple and can be done entirely through the Google Cloud console.

Step 1: Create an Instance Schedule

  1. Navigate to the Compute Engine section in the Google Cloud console.
  2. Click on the Instance schedules tab.
  3. Click on CREATE SCHEDULE.
  4. A new pane will open. Give your schedule a Name (e.g., daily-business-hours-schedule). You can also add a description.
  5. Select the Region for the schedule. 
    Note: The schedule must be in the same region as the VM instances you want to attach it to.
  6. Define your start and stop times. You can use the default time and frequency fields or, for more complex schedules, click the Use CRON expression toggle.
    Default fields: Set a Start time and a Stop time (e.g., 9:00 AM and 5:00 PM) and select the frequency (e.g., Daily, Weekly).
    CRON expression: This provides granular control. For example, a start CRON expression of 0 9 * * 1-5 will start the VM at 9:00 AM, Monday through Friday.
  7. Select the Time zone for your schedule.
  8. Click Submit to create the schedule.

Step 2: Attach the Schedule to Your VMs

Once you’ve created the schedule, you need to apply it to your VM instances.

  1. On the Instance schedules page, click on the schedule you just created.
  2. On the details page, click Add instances to schedule.
  3. A pane will open showing a list of your VMs in that region.
  4. Select the checkbox for each VM you want to attach to this schedule.
  5. Click Add.

Key Considerations

  • Permissions: Ensure that the service account used by the instance scheduler has the necessary permissions, specifically compute.instances.start and compute.instances.stop. The default Compute Engine service account usually has these permissions.
  • 15-Minute Window: Be aware that scheduled start and stop operations can take up to 15 minutes to initiate. If a VM needs to be running by 9:00 AM, set the start time a little earlier, for example, at 8:45 AM.
  • Manual Overrides: You can still manually start or stop a VM that has a schedule attached. If you stop a running VM before its scheduled stop time, it will stay stopped until the next scheduled start time.

By implementing this simple and effective strategy, you can take control of your cloud costs and ensure your resources are used efficiently.

Rajesh Mishra

I'm a developer who loves sharing insights, technical how-tos, and lessons learned from the world of code. While much of what I write may not be groundbreaking, I believe in documenting for future me—and for anyone else who might find it useful. Beyond tech, I also dive into life's experiences and moments, reflecting on personal growth and sharing stories that resonate. Whether you're here for practical tips or a fresh perspective on life, I hope you find something meaningful.

Leave a Reply

Your email address will not be published. Required fields are marked *