How to Save Money on Google Cloud Platform

Use Cloud Scheduler to start and stop Compute Engine Instances automatically

Gaurav Agarwal
Jul 3 · 5 min read
Image for post
Image for post
Photo by Mitchell Luo on Unsplash

Google Cloud Platform is one of the fastest-growing cloud platforms, maintaining a steady position at a number three, behind AWS and Azure. It prides itself for its network quality and the edge on data science and engineering.

Its Kubernetes engine is one of the most mature in the market. And because Kubernetes was Google’s baby, it’s more versatile than Azure’s AKS or AWS’s ECS.

Compute Instances is one of the most competitive IaaS solutions that Google Cloud offers. With sustained used discounts, it works best for companies running pay as you go instances, rather than other cloud providers.

Azure is cheaper in the IaaS segment when you use dedicated instances, but that creates vendor lock-in, something that most organisations avoid. A multi-cloud model is the most suitable tactic — most companies are adapting to the times, where cost saving and optimisation are the highest priorities.

More than half of a company’s instances are development and test machines. While some devices do need to run continuously, most don’t need to run beyond office hours and some are even run only on a needs basis. Most enterprises educate employees on cost-effectively using the Cloud, and there are many ways of saving costs without compromising performance.

Some ways you can save costs are:

  • Using smaller instances wherever possible.
  • Trying to optimise network traffic, so that traffic doesn’t move between zones and regions. Most cloud providers charge you for egress traffic.
  • Spinning down or stopping compute instances when you have finished your job.

This is all good advice — but there has to be a way to enforce these guidelines at an organisational level. There may be instances where developers forget to turn off their VMs because they got busy in an urgent meeting. Cost savings should not be left solely to developers and admins to manage.

One way to managing this cost is by spinning down all development instances at the close of business automatically, to avoid runaway costs. That ensures that you are covered on the organisational level and don’t need to rely on developers to turn off their instances in case they forget.

Google Cloud offers a Cloud Scheduler service that helps you run cron jobs that can trigger specific endpoints, such as a Cloud Function, or a Pub-Sub Topic.

Let’s use Cloud Scheduler, Google Pub/Sub and Cloud Functions to schedule automatic shutdown and startup of development instances. The process is illustrated in this architectural diagram:

Image for post
Image for post

We could have used the Scheduler to trigger cloud functions directly, but instead, the Pub-Sub provides a buffer layer in between — helpful when we have a lot of startup and shutdown events.

Creating the Compute Instance

Let’s start by creating a dev compute instance with the following properties:

  • Name: instance-1
  • OS : Ubuntu 18.04LTS
  • Zone: europe-west2-c
  • Labels : env=dev

Creating the Service Account

Next, we need a service account that has privileges to start and stop VM Instances. Let’s create a service account named compute-instance-admin-sa:

Assign the computeInstanceAdmin role to the service account:

Creating the Pub/Sub Topics

Let’s now create Pub/Sub topics so the scheduler can publish start and stop events to them.

Creating Cloud Functions

The next step is to create cloud functions that listen to the startup and shutdown events from the Cloud Pub/Sub topics and start and shut down instances that have the provided label in the events.

Let’s clone this repository for a pre-written NodeJS cloud function :

Deploy the Start Cloud function

Create the Stop Cloud function

Let’s trigger the cloud function directly to see if they are working correctly.

Convert the JSON input to a base64 token, so you can send it to the Cloud Pub/Sub topic.

Send the base64 string to the stopInstancePubSub topic:

Check the instance status to see if the instance has terminated. It can take a few minutes to complete:

Now let’s start the instance by calling the startInstancePubSub function with the same message:

Check the instance status, and it should now be running.

Setting up the Cloud Scheduler

Let’s complete the chain by setting up the cloud scheduler jobs.

That requires the app engine API to be enabled and set to the same region that you’ve deployed your cloud functions.

Create the start job:

Create the stop job:

Trigger the task manually to see if its working end to end.

Stop the instance first:

Check the instance status to see if the instance has terminated. It can take a few minutes to complete:

Now let’s start the VM by manually triggering the scheduler job:

Check the instance status to see if the instance has started. It can take a few minutes to complete:

Congratulations! You’ve configured two scheduled tasks to automatically stop your dev instances at the close of business, and start them at the start of business.

This could help to save you a lot of money potentially lost during idle hours.

Conclusion

While there are many ways of saving money, turning off development instances when you don’t need them is a sensible approach in any organisation.

Automatically scheduling stop events at the close of business is a practical approach to preventing runaway costs and ensuring things remain on budget.

Thanks for reading! I hope you enjoyed the article!

Better Programming

Advice for programmers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store