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

12 REPLIES 12

Thanks for your reply.
I have seen this, but I don't understand how to create a resource policy.
When I search for it I get not a recognizable topic.
Going through some links I feel like standing in a forest.
What to do?

Hello @ReinhardW  ,Welcome on Google Cloud Community.

what kind of resource policy you talking about? Here you have example command or UI usage with explanation. 
https://cloud.google.com/compute/docs/instances/schedule-instance-start-stop#gcloud

Choose either Console for UI view, or gcloud if you want to use command. 

Example:

gcloud compute resource-policies create instance-schedule community-test \
    [--description='test'] \
    [--region=us-central1] \
    [--vm-start-schedule='0 8 * * MON-FRI'] \
    [--vm-stop-schedule='0 17 * * MON-FRI'] \
    [--timezone=UTC] \
    [--initiation-date=2024-08-27T08:00:00-2] \
    [--end-date=2024-08-29T08:00:00-2]

--
cheers,
DamianS
LinkedIn medium.com Cloudskillsboost

Hi DamianS,
documentation says:
"Scheduling a VM instance to start and stop
Instance schedules let you start and stop virtual machine (VM) instances automatically. To use instance schedules, create a resource policy detailing the start and stop behavior, and then attach the policy to one or more VM instances."
I don't know more.

Ahhh. Okay 😄 So basically, try to either use cloud command or UI for policy creation. UI should be easier to handle. If you will have any issues, let us know. We always happy to help 🙂 

Do you mean Cloud Console and SDK Shell (CLI) ?
What to do there?

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

 

 

wow, it works.
thanks a lot!
best regards

Happy to help. Remember to pick one of our reply as  solution to close this case 🙂

--
cheers,
DamianS
LinkedIn medium.com Cloudskillsboost

 

And now my instance is not available in the zone.
Is it now locked or what happend?
Where can I see the reason?
edit: in the logs I see ZONE_RESOURCE_POOL_EXHAUSTED
A e2-micro VM instance is currently unavailable in the europe-west3-b zone.
but why? it was available as I created it.

Sometimes due to internal error or high demand, some of regions are overutilized. As a results, when you starting or creating resources, you can get ZONE_RESOURCE_POOL_EXHAUSTED. Try different zone than europe-west3-b. 

Aah, I did find the instance schedule and tried it.
Let see what it will do.