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. 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:
- Automated start and stop of VM instances is possible. No additional manpower is required.
- It helps you in cost optimization.
- VM instances can be managed more efficiently if scheduled.
- Workloads can be scheduled on a recurring or one-time basis with the instance scheduler.
- 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
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).
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:
- Open the VM instance page in GCP.
- At the top of the page, select Instance schedules. All scheduled instances are displayed here.
For creating an instance, you need to have compute.resourcePolicies.create permission on a particular project.
- Go to the VM Instances page in GCP.
- At the top of the page, click the Instance schedules tab.
- Click Create schedule.
- Give it a name.
- You may enter a Description.
- Select a region for this instance schedule in the drop-down menu.
- Define when VM instances attached to the instance schedule will start and stop.
- Choose the time zone for the Start time and Stop time, from the Time-zone dropdown.
- Select the date and time when you want this instance scheduled to begin in the Initiate date field. It takes effect immediately if omitted.
- 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.
- Click on Submit.
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.
- At the top of the page, click the Use Cron expression
- 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:
- “ * ” symbolizes “any”
- “ – ” symbolizes “range”
- “, ” 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:
- You can delete instance schedules by selecting the checkboxes.
- At the top of the page, click delete.
- 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.