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! Go to 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.
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.