Hands-on Guide to Scheduling VM Instances to Start and Stop

Romin Irani
Romin Irani’s Blog
9 min readApr 18, 2021

We recently covered a 3-part series on Compute Engine Pricing, which I list down over here:

In Part 1 , we covered On-demand pricing , where you are charged for the time that you keep the Virtual Machine instances running. We also covered the scenario, that in development and testing environments, one of the recommendations is not to keep the instances running all the time. You can save a lot of money by identifying the block of time that you typically use these VMs and then keeping them in a Stopped state during other days.

An example scenario could be that you would like all your Development machines to be started at 9:00 AM and stopped at 5:00 PM everyday. This will result in you getting billed for just 8 hours a day instead of 24 hours a day. This will result in significant savings just in on-demand pricing for that VM. Just for numbers, cost of running a n1-standard-1 instance for 24 hrs / day and all days a week v/s running it for 8 hours a day and all days a week is shown below. You can see the cost of approx. $29 v/s $13 / month.

Google Cloud recently announced the ability to schedule start and stopping your Compute Engine instances in GA and this article will show how we can do the same.

What is an Instance Schedule?

To get started, we need to first understand the concept of an Instance Schedule. The Instance Schedule is meta information about when you want to start an instance, stop an instance, in which compute engine region it can be applied to, the time zone for the start/stop time and whether you want apply it daily/weekly/monthly with various combinations. Think of Cron expressions that specific when it can be applied.

Having said that, a sample Instance Schedule might have the following definition:

Name + Description : <Sample Name + Description>
Compute Engine Region : asia-south1 (Mumbai)
Start Time : 9:00 AM
End Time : 5:00 PM
Time Zone : IST (UTC + 5:30)
Initiation Date (Optional) : Leave it empty
End Date (Optional) : Leave it empty
Frequency : Daily / Weekly / Monthly (Depending on that you can select days of the week and days of the month)

So, if we go back to our plan of having an Instance Schedule, where we want our machines to be running only from 9:00 AM to 5:00 PM on all days of the week (daily) and apply it to various instances in Mumbai region, I would go with the following values:

Name + Description : MyDevMachine-Instance-Schedule
Compute Engine Region : asia-south1
Start Time : 9:00 AM
End Time : 5:00 PM
Time Zone : IST (UTC + 5:30)
Frequency : Daily

That’s it. Before we get to the mechanics of setting this up, I suggest to read up on the limitations, a few of which I reiterate below:

  • An instance schedule is specific to a region and you can only attach it to instances in that region.
  • Each VM instance can be associate with only one instance schedule.
  • The instance schedule needs a window of about 15 minutes to the time that you have specified to complete the start and stop of the instance. In my sample runs, it took around 10 minutes after the stop time for example, for the instance to actually stop. So you might want to factor that but do it with care since you cannot predict it precisely.

Instance Schedules in Action

To see the instance schedules work in action, we are going to go with our original requirement i.e. start the VM instances at 9:00 AM and stop them at 5:00 PM, on all days of the week.

Given that, I went ahead and created two Compute Engine VMs — nothing fancy here, just go with the n1-standard-1 or smaller VMs. In my case, I created them in the region = asia-south1.

If you are familiar with gcloud, you can use the following commands to create a couple of VM instances.

gcloud compute instances create instance-1 --machine-type=n1-standard-1 --zone=asia-south1-agcloud compute instances create instance-2 --machine-type=n1-standard-1 --zone=asia-south1-a

If you visit the Google Cloud Console and Compute Engine → VM Instances, you should see the two of them listed as shown below:

If you notice, you see a tab INSTANCE SCHEDULE next to INSTANCES in the above screenshot. Simply click on that.

At this point, we don’t have any instance schedules. Remember, all of the stuff that I am showing can be done via the gcloud utility or even the API, but I will use the Cloud Console here.

Let’s create our instance schedule now. Click on CREATE INSTANCE SCHEDULE and you will see the following screen. Go ahead and populate it as I have shown below:

Click on SUBMIT button and the schedule gets created.

The instance schedule is not yet associated (attached) to any Compute Engine VM Instances. So let’s apply it to both our VMs (instance-1 and instance-2).

Click on the instance schedule record i.e. (dev-start-stop-schedule under the name column). You should see the screen below. It contains the definition of our instance schedule (dev-start-stop-schedule) and you can see that there are no attached instances yet.

Click on the ADD INSTANCES TO SCHEDULE link shown above and you will see the list of VMs that can be attached.

Select the VMs (instance-1 and instance-2 in our case) and click on the ADD button. You see that the instances are now attached to the instance schedule and it is in effect (if you get an error about service account permission, see the next section).

Service Account Permission

Keep in mind that when we create an instance schedule and attach it to the Compute Engine instances, Compute Engine behind the scenes will need to have the permission to perform a start instance and stop instance operation on those compute resources (VM instances : instance-1 and instance-2).

For that to happen, you must ensure that the Google Managed Compute Engine System Service account (not your default Compute Engine service account) has the permissions to start and stop the instance.

The Google Managed Compute Engine System Service Account is of the form:

service-PROJECT_NUMBER@compute-system.iam.gserviceaccount.com

This account is not visible in the list of service accounts when you go to that section in the IAM or Service Accounts section. We will get to that in a while, but first up, we will create a Custom Role : Instance-Start-Stop-Role , which only has permissions to start and stop Compute Engine instances.

Go to IAM & Admin → IAM section in the Google Cloud Console and click on CREATE ROLE at the top as shown below:

This will bring up a form, where you can fill it up with the Custom Role name (in my case, it is Instance-Start-Stop-Role):

Click on ADD PERMISSIONS. This will bring up the following screen , where you can filter by role by typing in Compute Instance Admin and selecting Compute Instance Admin (v1) as shown below:

Then from the list of permissions, filter it first by compute.instances.start and then click on ADD.

Similarly, filter it again via compute.instances.stop and click on ADD.

Ensure that you have both these permissions and then create the role.

This will create a Custom Role. Now, all we need to do is assign this role to the Google Managed Compute Engine System Service Account. Let’s do that.

Visit IAM and Admin → IAM. Select the checkbox for Include Google-provided role grants as shown below.

This will show the system accounts too and you should look out for the service account of the form:

service-PROJECT_NUMBER@compute-system.iam.gserviceaccount.com

We can now edit this role by clicking on the Edit Pencil icon at the end. This will bring up the current list of roles assigned to this service account. Click on ADD ANOTHER ROLE and select our Custom Role that we just defined i.e. Instance-Start-Stop-Role. Click on Save. You will be brought back to the list of Accounts, just ensure that the new custom role is visible in addition to other roles for the service account.

Note : In case you got the permission error while attaching the instance schedule to the instance, go back now and try the same again. You should be able to move attach the same successfully.

That is pretty much what you have to do to schedule your instances to start and stop as per your pattern.

Monitoring the Start / Stop Schedule in Action

You can use Cloud Logging to view the start and stop schedule in Action. I created an instance schedule of just 30 minutes to test the same in action i.e. I defined the instance schedule to start at 12:00 PM and stop at 12:30 PM.

To view the same, I visited Cloud Logging → Logs Explore as shown below:

Next up, click on Query builder. Then do the following:

  • For the Resource dropdown, select VM Instances from the Resource Type as shown below:
  • For Log name, we need to go to the Cloud Audit Log and Activity as shown below:

Now run the log query again , and you see a lot more specific logs. I went back to the logs and noticed that the stop command was given as shown below:

You can see that the stop command was given a good 10 minutes after my instance schedule specified time i.e. 12:30 PM. So as I earlier mentioned and which the documentation states clearly, we should factor a 15 minute window after the start / stop time for the action to be performed.

Hope this was useful. Do note that we also have available in beta, the ability to suspend/resume a VM.

--

--

Romin Irani
Romin Irani’s Blog

My passion is to help developers succeed. ¯\_(ツ)_/¯