Table of Contents

Google Cloud’s most widely used service is Compute Engine, which is an Infrastructure as a Service offering provided by Google Cloud Platform. It is a general-purpose, flexible, highly scalable virtual machine. There are many types and sizes of compute engines. 

Compute Engine offers a wide range of options from general purpose machines to resource-specific optimised machines such as memory-optimised, compute-optimised, and GPU-optimised, all at different pricing. Additionally, the user can also customise their own machine types to better fit their needs.

Why should you schedule Instances?  

Instances are run in different environments, including production, testing, and others. Some environments don’t require instances to be on at all times. For example, you can only use a testing environment when you need to or development environment tied to the working hours.

By using scheduling, you can automate the start and stop of instances, so you do not have to worry about running an instance when it is not needed, or how much money you waste by leaving it running unnecessarily. 

The following are some benefits: 

  1. Automated start and stop of VM instances is possible. No additional manpower is required. 
  2. It helps you in cost optimization. 
  3. VM instances can be managed more efficiently if scheduled. 
  4. Workloads can be scheduled on a recurring or one-time basis with the instance scheduler. 
  5. It creates deployment and test environments. 

VM instances are enabled using instance schedules by creating and attaching a resource policy that defines the start and stops behavior. Here are some pre-requistes that needs to be ensured before scheduleding

Required Permissions 

Your project’s Compute Engine Service Agent must have the following permissions to use instance schedules: 

  • For VM instances to start, you need to have compute.instances.start permission. 
  • For VM instances to stop, you need to have compute.instances.stop permission.   

The Compute Engine Service Agent should have an IAM role if they don’t have the above permissions. The IAM role should have required permissions, such as Compute Instance Admin (v1) (roles/compute.instanceAdmin.v1).

Listing instances

A project or organization must have to compute.resourcePolicies.list permission in order to view all the instance schedules. Follow the below steps to list all the instances: 

  1. Open the VM instance page in GCP. 
  2. At the top of the page, select Instance schedules. All scheduled instances are displayed here. 

Scheduling instances

For creating an instance, you need to have compute.resourcePolicies.create permission on a particular project.

  1. Go to the VM Instances page in GCP. 
  2. At the top of the page, click the Instance schedules tab.
  3. Click Create schedule
  4. Give it a name.
  5. You may enter a Description.
  6. Select a region for this instance schedule in the drop-down menu.
  7. Define when VM instances attached to the instance schedule will start and stop.
  8. Choose the time zone for the Start time and Stop time, from the Time-zone dropdown.
  9. Select the date and time when you want this instance scheduled to begin in the Initiate date field. It takes effect immediately if omitted.
  10. Select the date and time when you want this instance scheduled to end in the End date field. In the absence of this clause, the schedule will remain in effect indefinitely.
  11. Click on Submit.
compute engine, google cloud compute engine. compute engine cost optimization, gcp compute engine optimization

To configure Start time, Stop time, Frequency, and more complex schedule, use Cron expressions. A cron expression is a series of six-seven fields that describes the details of the schedule. 

  1. At the top of the page, click the Use Cron expression 
  2. Choose whether to start or stop Cron.

  • Enter a cron expression that describes when VM instances will be started in the Start Cron expression field.
  • Enter a cron expression that describes when to stop VM instances in the Stop Cronexpression field. 

These fields are separated by white space and can contain values with the following special characters: 

  1.   * ” symbolizes “any” 
  2.   ” symbolizes “range” 
  3.   , ” symbolizes “list” 

Deleting an instance schedule  

When you no longer need a VM instance, delete it by removing its resource policies. Given below are the steps to be taken: 

  1. You can delete instance schedules by selecting the checkboxes. 
  2. At the top of the page, click delete. 
  3. Select delete in the dialog box to confirm your action. 

This way, you can create and schedule a Virtual Machine instance to automate your task. The resource policy is like a handbook describing when to start and stop your VM instance. Along with this, you can also mention the frequency, time zones, description, etc. The Compute Engine API, the Google Cloud console, or the Google Cloud CLI are all available for creating new instances or groups of managed instances. This article has delivered the way to schedule VM instances using Compute Engine. 

Related Articles