future-mano / gcp-instance-scheduler

Tools that shutdown GCP Instance on your schedule.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

gcp-instance-scheduler

Tools that shutdown GCP Instance on your schedule.

Abstract

Config

Getting Started

# Deploy Cloud Function
gcloud functions deploy ReceiveEvent --project <project-id> \
  --runtime go111 \
  --trigger-topic instance-scheduler-event

# Create Cloud Scheduler Job
gcloud beta scheduler jobs create pubsub shutdown-workday \
  --project <project-id> \
  --schedule '0 22 * * *' \
  --topic instance-scheduler-event \
  --message-body '{"command":"stop"}' \
  --time-zone 'Asia/Tokyo' \
  --description 'automatically stop instances'

Example: create target resources

Set label for target instance

# GCE
gcloud compute instances update <insntance-name> \
  --project <project-id> \
  --update-labels state-scheduler=true

# Instance Group
gcloud compute instance-templates create <tmeplate-name> ... \
  --project <project-id> \
  --labels state-scheduler=true

# Cloud SQL (master must be running)
gcloud beta sql instances patch <insntance-name> \
  --project <project-id> \
  --update-labels state-scheduler=true

# GKE
gcloud container clusters update <cluster-name> \
  --project <project-id> \
  --zone <cluster-master-node-zone> \
  --update-labels state-scheduler=true

Tips: Debug Function

  • publish message to pub/sub
    • gcloud pubsub topics publish stop-instance-event --project <project-id> --message "{"command":"stop"}"
  • confirm Functions log
    • gcloud functions logs read --project <project-id> --limit 50
  • manual launch for job of scheduler
    • gcloud beta scheduler jobs run shutdown-workday-instance

License

This project is licensed under the Apache License 2.0 License - see the LICENSE file for details

About

Tools that shutdown GCP Instance on your schedule.

License:Apache License 2.0


Languages

Language:Go 100.0%