Save your Compute cost by scheduling the start and stop of compute engine using cloud scheduler and cloud function

dharmendra mishra
4 min readApr 9, 2021

Objective:

There can be situations where developers forgot to shut down the compute 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.

Also, In order to save cost, we can spin down the compute instances when we have finished our work to avoid runaway cost. Most of the time compute engine VMs of testing and development environments are idle after the working hours.

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

Index:

1. Google Cloud Platform

2. Compute Instances

3. Cloud functions

4. Cloud Scheduler

Google Cloud Platform: Google cloud platform is a suite of cloud computing services offered by google and one of the fastest-growing cloud platforms.

Compute Engine: Compute Engine Abstract underlying hardware, provides control surface for infrastructure components. 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.

Cloud functions: Cloud functions are basically allows you to trigger your code from N number of components of google cloud.

In order to achieve our objective will have two cloud functions.

ServerStart — Python code to start target server.

ServerStop — Python code to stop target server.

Cloud Scheduler: Cloud Scheduler is a cron job scheduler. It’s basically allows you to schedule jobs. You can automate everything, including retries in case of failure to reduce manual toil and intervention.

To run our cloud functions will have two cloud scheduler jobs.

Schedulerstart — VM instance start at required time in morning. Consider 8:30 AM.

Schedulerstop — VM instances stop at required time in the evening. Consider 6:30 PM.

Go to the Google Cloud Console and login with your Google account. Assuming that you already have a Google account if not please create one. Then Select a project where you want to create a scheduler or create a new project.

Steps to Implement:

Note: No need to change Networking, Variables and Advance Settings.

Step 1: Create ServerStart Cloud Function.

a. Open the Cloud Functions page in the cloud console.

b. Click Create function.

c. Name your function as ServerStart.

d. Select Region as required. Suppose us-central1.

e. In the Trigger field, select HTTP Trigger.

f. In Authentication, Select Allow unauthenticated invocations.

g. Click Save.

Configurations are set. Click on next to Add code.

In Runtime, Select Python3.7

Select Source code as Inline Editor.

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

from google.auth.transport.requests import AuthorizedSession

from google import auth

PROJECT_ID=”my-project” # Update placeholder value

instance_zone=”my-zone” # Update placeholder value

instance_name=”my-instance” # Update placeholder value

def server_start(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))

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

google-auth==1.11.3

In Entry point, specify your function server_start

Click on Deploy.

Step 2: Create ServerStop Cloud Function.

a. Open the Cloud Functions page in the Cloud Console.

b. Click Create function.

c. Name your function as ServerStop.

d. Select Region as required. Suppose us-central1.

e. In the Trigger field, select HTTP Trigger.

f. In Authentication, Select Allow unauthenticated invocations.

g. Click Save.

Configurations are set. Click on next to Add code.

In Runtime, Select Python3.7

Select Source code as Inline Editor.

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

from google.auth.transport.requests import AuthorizedSession

from google import auth

PROJECT_ID=”my-project” # Update placeholder value

instance_zone=”my-zone” # Update placeholder value

instance_name=”my-instance” # Update placeholder value

def server_stop(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))

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

google-auth==1.11.3

In Entry point, specify server_ stop

Click on Deploy.

Note: use crontab guru to create cron expressions.

Step 3: Create server_start_scheduler Cloud Scheduler job.

a. Go to Cloud Scheduler.

b. Click on Create Jobs.

c. Name your Job as server_start_scheduler.

d. Give Description to job.

e. Select frequency of Job as required. You have to specify the Cron expression for frequency.

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

30 8 * * MON-FRI

Select the timezone as applicable to you.

f. In Target, Select HTTP.

g. For URL,

Go to Cloud function Overview page in the Cloud Console

Click on ServerStart cloud function

Go to Trigger tab

Copy the URL specified

Paste it in URL

Click on Create

Test the Scheduler by Clicking on Run Now button

h. Results should show success.

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

Step 4: Create server_stop_scheduler Cloud Scheduler job

Follow the same process written in step 3. change the required parameter like function name and url etc.

This is it!!

Post your questions in comment section.

--

--

dharmendra mishra

Data-driven Analytics/Engineering leader with 12+ years of experience in digital advertising company. Skills SQL, Python, Excel and GCP Google Cloud Platform.