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

Set use time for instance and follow used time

Is there a way when creating an instance to set a max X hours of use (example 40 hours) and a way to check how many of those hours have been used or remain?

And is it posible to do it with the console or API?

 

Thank you.

Solved Solved
0 2 306
1 ACCEPTED SOLUTION

There is nothing in the GCP services (that I am aware off) that will constrain a Compute Engine to shutdown after a finite amount of consumption.  To achieve what you ask ... it may be possible to use GCP Monitoring and calculate an aggregate consumption and then publish an alert when the consumption limit is reached.  This could be published to PubSub which could shut down or delete the Compute Engine.  If GCP Monitoring can't be used, then perhaps a form of Audit Logging could be used.

View solution in original post

2 REPLIES 2

There is nothing in the GCP services (that I am aware off) that will constrain a Compute Engine to shutdown after a finite amount of consumption.  To achieve what you ask ... it may be possible to use GCP Monitoring and calculate an aggregate consumption and then publish an alert when the consumption limit is reached.  This could be published to PubSub which could shut down or delete the Compute Engine.  If GCP Monitoring can't be used, then perhaps a form of Audit Logging could be used.

@kolban 's suggestion sounds like a great one.  To throw another idea into the mix you could look at Cloud Workflow, it has a sleep_until function: https://cloud.google.com/workflows/docs/reference/stdlib/sys/sleep_until

You could then execute the Workflow immediately after you provision the VM - or even as a startup script in the VM, or have the workflow even provision the VM  - then use the number of hours as a parameter to the workflow. It then uses the sleep_until function then after that time it shuts down the VM.

Then to figure out how much time is left you could have a simple Cloud Function that could query the workflow's time and just return the delta between now and the end time on the workflow.