DEV Community

Giuliano Ribeiro
Giuliano Ribeiro

Posted on • Edited on

Programmatically start and stop Google Cloud Compute Engine VM without code

Hi there, long time since my last post, but here I'm!
Today I want to show you a simple but great tip for users/admins of GCE VMs.

Intro

In the past, when we needed to periodically stop or start a VM in GCE was a pain! As we don't have an easy way to do it, we should write a Cloud Functions or include a cron job in a VM (like a central VM with all your cron jobs).
But a few months ago, GCP added a new way to do it using the API directly in the Google Cloud Scheduler (a serverless cron service).

Now, let's do it!

Service Account

First things first: to configure it properly, you should have a service account. As best practice, you need to create a new service account and give the Compute Admin role. But for this tutorial, I'll show using the default Compute Engine service account.
Go to Google Cloud Console > Navigation menu > IAM & Admin > Service Accounts

1_SA
Copy the email address associated to Compute Engine default service account. This email is always something like 999999999999-compute@developer.gserviceaccount.com

Next!

Cloud Scheduler

The Cloud Scheduler is a serverless cron-like service. It is pretty handy for this kind of job. You can setup jobs to do a variety of calls, like general HTTP calls, App Engine services calls and post to Pubsub topics.
2_CS

Next, create the jobs. Just click in the Create Job button and let's do it!

New job

The create job form is simple and straight to the point.
The important parts are the HTTP URL and the Auth header ones.

For the HTTP URL you should fill with something like this:

https://compute.googleapis.com/compute/v1/projects/<PROJECT-ID>/zones/<VM-ZONE>/instances/<VM-ID>/<start OR stop>

But first is required to have the Instance ID.

Get Instance ID

Just go to Navigation menu > Compute Engine > Click in your VM to get details

Instance_ID

For example, my URL to start would be like this:
https://compute.googleapis.com/compute/v1/projects/titanium-campus-305808/zones/europe-west1-d/instances/514142841644/start

Now we have all information, this is the Create job form:
3_create-job

All Scheduler jobs

4_jobs

As you can see I have 2 jobs, one to start and another to stop my Minecraft server. You can also enable or disable the jobs, so if you don't want the job to run for a specific period. Or, of course, you can setup the frequency of the job for a more complex form, as it respects the cron syntax.

So that's it, my friends, I hope it can help you in any way and as always, send me feedbacks. I'll always try to help you about GCP!

Thanks,

Dynatrace image

Observability should elevate – not hinder – the developer experience.

Is your troubleshooting toolset diminishing code output? With Dynatrace, developers stay in flow while debugging – reducing downtime and getting back to building faster.

Explore Observability for Developers

Top comments (0)

Redis image

Short-term memory for faster
AI agents

AI agents struggle with latency and context switching. Redis fixes it with a fast, in-memory layer for short-term context—plus native support for vectors and semi-structured data to keep real-time workflows on track.

Start building

👋 Kindness is contagious

Delve into a trove of insights in this thoughtful post, celebrated by the welcoming DEV Community. Programmers of every stripe are invited to share their viewpoints and enrich our collective expertise.

A simple “thank you” can brighten someone’s day—drop yours in the comments below!

On DEV, exchanging knowledge lightens our path and forges deeper connections. Found this valuable? A quick note of gratitude to the author can make all the difference.

Get Started