This tutorial demos how to use Cloud Scheduler to schedule Compute Engine instances to start and stop at specified or periodical time.
The main componentes used in this turial includes Compute Engine instances (with labels), Cloud Pub/Sub, Cloud Functions and Cloud Scheduler. The event flow looks like as below:
Cloud Scheduler -> Cloud Pub/Sub -> Cloud Functions -> Start/Stop Compute Engine instances
Select the project which you have permissions to access Compute Engine, Cloud Pub/Sub, Cloud Functions and Cloud Scheduler. Remember to enable these APIs first.
Create a sample instance with label env=dev which will be used as filter when start/stop action
executed by Cloud Functions.
gcloud compute instances create dev-instance \
--network default \
--zone us-west1-b \
--labels=env=devCreate 2 Cloud Pub/Sub topics which will be used as the triggers of following Cloud Functions.
gcloud pubsub topics create start-instance-eventgcloud pubsub topics create stop-instance-eventCreate 2 Cloud Funtions, one for starting instances, the other for stopping instances. They are triggered by the message of above 2 Cloud Pub/Sub topics.
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.gitcd nodejs-docs-samples/functions/scheduleinstance/gcloud functions deploy startInstancePubSub \
--trigger-topic start-instance-event \
--runtime nodejs8 \
--allow-unauthenticatedgcloud functions deploy stopInstancePubSub \
--trigger-topic stop-instance-event \
--runtime nodejs8 \
--allow-unauthenticatedOnce the functions created, we can manually trigger it to verify the instance being stopped or started.
gcloud functions call stopInstancePubSub \
--data '{"data":"eyJ6b25lIjoidXMtd2VzdDEtYiIsICJsYWJlbCI6ImVudj1kZXYifQo="}'gcloud compute instances describe dev-instance \
--zone us-west1-b \
| grep statusNow we have 2 Cloud Functions with corresponding Cloud Pub/Sub topics as their triggers ready. Then we will create 2 Cloud Scheduler jobs, one for starting the instance at 9AM every weekday (Mon. to Fri.), the other for stopping the instance at 10AM every weekday.
gcloud beta scheduler jobs create pubsub startup-dev-instances \
--schedule '0 9 * * 1-5' \
--topic start-instance-event \
--message-body '{"zone":"us-west1-b", "label":"env=dev"}' \
--time-zone 'Asia/Taipei'gcloud beta scheduler jobs create pubsub shutdown-dev-instances \
--schedule '0 10 * * 1-5' \
--topic stop-instance-event \
--message-body '{"zone":"us-west1-b", "label":"env=dev"}' \
--time-zone 'Asia/Taipei'Once the jobs created, we don't need to wait until the scheduled time, we can manaully run the jobs to verify the following actions being ran accordingly.
gcloud beta scheduler jobs run shutdown-dev-instancesgcloud compute instances describe dev-instance \
--zone us-west1-b \
| grep statusgcloud beta scheduler jobs run startup-dev-instancesgcloud compute instances describe dev-instance \
--zone us-west1-b \
| grep status- Delete the Cloud Scheduler jobs
- Delete the Pub/Sub topics
- Delete the Cloud Functions functions
- Delete the Compute Engine instance
