To support multiple VMs, please find out the full details: https://medium.com/@cyanzhoufeng/gcp-put-your-staging-env-into-sleep-mode-during-non-working-hours-bc557673028d This code is used to achieve the functions described in this Medium article
Below is the original README of the sample code, it also includes the instruction
======================Quote started===================
See the Scheduling Instances with Cloud Scheduler tutorial.
-
Read and follow the prerequisites.
-
Install dependencies:
npm install
-
Run the tests:
npm test
======================Quote ended===================
Checkout the code
git clone git@github.com:CyanZero/gcp-vms-sleep.git
No need to install as the code will be uploaded to GCP Cloud Function, so run the below Gloud API (Need to install GCloud util first)
gcloud functions deploy startInstancePubSubMul \
--trigger-topic start-instance-event \
--runtime nodejs6 \
--region asia-northeast1
gcloud functions deploy stopInstancePubSubMul \
--trigger-topic start-instance-event \
--runtime nodejs6 \
--region asia-northeast1
# The zone value depends on your actual settings
data=$(echo '{ "zone": "YOUR_ZONE", "instances": ["instance-a", "instance-b"] }' | base64)
# Notice the limited support for the cloud function, and the region used here is asia-northeast1
gcloud functions call stopInstancePubSub \
- region asia-northeast1 \
- project YOUR_PROJECT_NAME \
- data '{"data":$data}'
# Check if the instances are in terminated statusgcloud compute instances describe instance-a \
--zone YOUR_ZONE \
| grep status
status: TERMINATED
bash mul_sleep_scheduler.sh dev "YOUR_ZONE" "instance-a instance-b"