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! Go to 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.
1. Running VM ( which you already have )
2. Then go to Compute Engine -> Instance Schedules
3. CREATE INSTANCE SCHEDULE
4. Fill out fields as you need. Then click SUBMIT
5. You will see something similar . Click on schedule name
6. ADD INSTANCES TO SCHEDULE
7. Pick VM which should be handled by this policy and then ADD