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

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))

google-auth==1.11.3

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))

google-auth==1.11.3

--

--

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