You have a bastion host server which need to run only in office hour(9:00–18:00). There is a compute instance which runs ETL job only on weekend. In the cases like this, keeping the instance turned ON 24 hours will be waste of money and resource. By turning the instance in running state only at the required time will save a lot irrespective of the type of instance. We already know a feature of cloud: on-demand. We can activate the resource on our will at our timing and keep it in terminated state otherwise.
Gone were days where we used to play with Google Cloud SDK, write a cloud function to interact with the compute engine API, create a schedule to run the cloud function and keep track of that also.
But now the instance scheduling is made a lot more easier with few clicks and completely cloud managed. We need to create schedule mentioning the time, start date if any along with the region where the instances are created. Head over to VM Instances and INSTANCE SCHEDULE tab and create one.
Now, we can add instance to the schedule which are on same region where the schedule is created.
On first attempt, it will show access error like this:
Compute Engine System service account firstname.lastname@example.org needs to have [compute.instances.start,compute.instances.stop] permissions applied in order to perform this operation.
This means the schedule will run as the mentioned service account and we have to add the instance start-stop privilege from cloud IAM. On the IAM page, don’t forget to tick Include Google-provided role grants to see the system IAM service account. The account should be under Compute Engine Service Agent. And add the permission to the service account. If the role is not created, have one like below:
After adding the IAM permissions, go back to the scheduler page and it should allow you to attach required instance to a schedule.