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
Why not use the newer built in features?
https://cloud.google.com/compute/docs/instances/schedule-instance-start-stop
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.
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
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.