Schedule the Start and Stop of GCP Compute Engine instances using Cloud Scheduler and Cloud Function

Krutika Pasalkar
5 min readSep 13, 2020

--

Google Cloud Platform

Google Cloud Platform is one of the fastest-growing cloud platforms. Compute Engine is an IaaS solution that GCP offers. Compute instances can be set up in the cloud to provide required amounts of computing power. Cloud allows you the pricing advantage of ‘pay as you go’. Hence, we just have to pay for VMs based on the number of hours they are up and running. Most of the compute engine VMs of development and testing environments are idle after the working hours. Hence in order to save cost, we can spin down the instances when we have finished our work to avoid runaway cost. There can be situations where developers forgot to shut down the instances after work. In order to avoid such situations, we can automate the process of spinning down an instance after work hours and spin them up next morning.

In order to achieve this, we can create Cloud Function which will be triggered by Cloud Scheduler to shut down or start up instances.

The process can be illustrated using below Architecture Diagram:

Architecture Diagram

We need two cloud scheduler jobs,
Start_server_scheduler To start a VM instance at required time in morning. Consider 9:00 AM.
Stop_server_scheduler — To stop VM instances at required time in the evening. Consider 7:00 PM.

And, We need two cloud Functions,
StartServer — Containing python code to start target server.
StopServer — Containing python code to stop target server.
[ Here the target is only one instance. We can use metadata in order to target multiple instances ].

Steps for implementation

Step 1: Create startserver Cloud Function

Assuming that you already have a Google account. Go to the Google Cloud Console and login with your Google account.

Select a project where you want to create a scheduler or create a new project.

  1. Open the Cloud Functions Overview page in the Cloud Console
  2. Click Create function.
  3. Name your function as StartServer
  4. Select Region as required. Suppose us-central1
  5. In the Trigger field, select HTTP Trigger
  6. In Authentication, Select Allow Unauthenticated invocations
  7. Click Save
  8. No need to change Networking, Variables and Advance Settings
Steps to create StartServer Cloud Function

9. Configurations are set. Click on next to Add code.

10. In Runtime, Select Python3.7

11. Select Source code as Inline Editor.

12. Select main.py file and click on edit. Add below lines of code.

from google.auth.transport.requests import AuthorizedSessionfrom google import authPROJECT_ID=”my-project” # Update placeholder valueinstance_zone=”my-zone” # Update placeholder valueinstance_name=”my-instance” # Update placeholder valuedef start_server(request):    credentials, project = auth.default(scopes = [“https://www.googleapis.com/auth/cloud-platform"])    authed_session = AuthorizedSession(credentials)    print(“starting server”)    # you can call start multiple instance by calling below line passing different instance name    response = authed_session.post(‘https://compute.googleapis.com/compute/v1/projects/{}/zones/{}/instances/{}/start'.format(PROJECT_ID,instance_zone,instance_name))

13. Select requirements.txt file and click on edit. Add below lines of code.

google-auth==1.11.3

14. In Entry point, specify start_server

15. Click on Deploy

Step 2: Create stopserver Cloud Function

  1. Open the Cloud Functions Overview page in the Cloud Console
  2. Click Create function. [Assuming You have required permissions to create Cloud Function]
  3. Name your function as StopServer
  4. Select Region as required. Suppose us-central1
  5. In the Trigger field, select HTTP Trigger
  6. In Authentication, Select Allow Unauthenticated invocations
  7. Click Save
  8. No need to change Networking, Variables and Advance Settings
Steps to create StopServer Cloud Function

9. Configurations are set. Click on next to Add code.

10. In Runtime, Select Python3.7

11. Select Source code as Inline Editor.

12. Select main.py file and click on edit. Add below lines of code.

from google.auth.transport.requests import AuthorizedSessionfrom google import authPROJECT_ID=”my-project” # Update placeholder valueinstance_zone=”my-zone” # Update placeholder valueinstance_name=”my-instance” # Update placeholder valuedef stop_server(request):    credentials, project = auth.default(scopes = [“https://www.googleapis.com/auth/cloud-platform"])    authed_session = AuthorizedSession(credentials)    print(“stopping server”)    # you can call stop multiple instance by calling below line passing different instance name    response = authed_session.post('https://compute.googleapis.com/compute/v1/projects/{}/zones/{}/instances/{}/stop'.format(PROJECT_ID,instance_zone,instance_name))

13. Select requirements.txt file and click on edit. Add below lines of code.

google-auth==1.11.3

14. In Entry point, specify stop_server

15. Click on Deploy

Step 3: Create start_server_scheduler Cloud Scheduler job

  1. Go to Cloud Scheduler using this link or go via navigation menu.
  2. Click on Create Jobs. [Assuming You have required permissions to create Cloud Scheduler jobs]
  3. Name your Job as start_server_scheduler
  4. Give Description to job. [optional]
  5. Select frequency of Job as required. You have to specify the Cron expression for frequency.

You can use crontab guru to create cron expressions.

Eg. If you want to start server from Mon to Fri at 9 AM then specify as

0 9 * * MON-FRI

Here is the expression explanation on crontab guru.

Cron expression

6. Select the timezone as applicable to you.

7. In Target, Select HTTP.

For URL,

Go to Cloud function Overview page in the Cloud Console

Click on StartServer cloud function

Go to Trigger tab

Copy the URL specified

Paste it in URL

8. Click on Create

Step for Cloud SCheduler

9. Test the Scheduler by Clicking on Run Now button

10. Results should show success.

11. If the job failed, view logs and try troubleshooting

Step 4: Create stop_server_scheduler Cloud Scheduler job

  1. Go to Cloud Scheduler using this link or go via navigation menu
  2. Click on Create Jobs
  3. Name your Job as stop_server_scheduler
  4. Give Description to job. [optional]
  5. Select frequency of Job as required. You have to specify the Cron expression for frequency.

You can use crontab guru to create cron expressions.

Eg. If you want to stop server from Mon to Fri at 7:00 PM then specify as

0 19 * * MON-FRI

Here is the expression explanation on crontab guru.

Cron expression

6. Select the timezone as applicable to you.

7. In Target, Select HTTP.

For URL,

Go to Cloud function Overview page in the Cloud Console

Click on StopServer cloud function

Go to Trigger tab

Copy the URL specified

Paste it in URL

8. Click on Create

Steps for cloud scheduler

9. Test the Scheduler by Clicking on Run Now button

10. Results should show success.

11. If the job failed, view logs and try troubleshooting

Done !

Hope this was helpful and saved your time googling all this stuff. Please post any questions below in the comments section. I Will be glad to answer.

--

--