Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

schedule starting and stopping VM

Hi,
I am a newbie in GCP and just created a debian vm and I am able to use python.
I want that the VM will be started and stopped at special times.
When I try to read documentation I got lost.
I first tried to create a function:
import googleapiclient.discovery
import os

def start_vm(request):
project = '....'
zone = '....'
instance = '....'

compute = googleapiclient.discovery.build('compute', 'v1')
result = compute.instances().start(project=project, zone=zone, instance=instance).execute()

return 'VM gestartet'
Error:
Traceback (most recent call last): File "/layers/google.python.pip/pip/bin/functions-framework", line 8, in <module> sys.exit(_cli())

Could anyone help me please?

Solved Solved
0 12 690
1 ACCEPTED SOLUTION

Follow this documentation: https://cloud.google.com/compute/docs/instances/schedule-instance-start-stop#gcloud

Console: https://cloud.google.com/compute/docs/instances/schedule-instance-start-stop#console

CLI: https://cloud.google.com/compute/docs/instances/schedule-instance-start-stop#gcloud

Both options have field  / parameter description so you should be able to handle it.

Basically you need :

PS: Most probably you will get information ,that service account with name like mine (service-238313586149@compute-system.iam.gserviceaccount.com) needs specific permissions to stop VM. Simply before you do something, go to IAM -> Tick small check box " Include Google-provided role grants" at the top right corner -> Find your Service Account -> Click on pencil icon next to this account and grant Compute Instance Admin V1 role. Once done, follow policy creation.

DamianS_6-1724752876630.png

DamianS_7-1724752902537.png

 

 

1. Running VM ( which you already have )
2. Then go to Compute Engine -> Instance Schedules 

DamianS_0-1724752155543.png

3. CREATE INSTANCE SCHEDULE

DamianS_1-1724752188383.png

4. Fill out fields as you need. Then click SUBMIT

DamianS_2-1724752354427.png

5. You will see something similar . Click on schedule name

DamianS_3-1724752503694.png

6. ADD INSTANCES TO SCHEDULE

DamianS_4-1724752527209.png

7. Pick VM which should be handled by this policy and then ADD

DamianS_5-1724752552878.png

DamianS_8-1724752984600.png

 

 

View solution in original post