https://github.com/future-architect/gcp-instance-scheduler
Tools that shutdown GCP Instance on your schedule.
https://github.com/future-architect/gcp-instance-scheduler
gcp gcp-cloud-functions go golang
Last synced: about 1 year ago
JSON representation
Tools that shutdown GCP Instance on your schedule.
- Host: GitHub
- URL: https://github.com/future-architect/gcp-instance-scheduler
- Owner: future-architect
- License: apache-2.0
- Created: 2019-06-20T23:51:08.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2023-02-24T19:03:33.000Z (over 3 years ago)
- Last Synced: 2025-05-07T21:08:42.551Z (about 1 year ago)
- Topics: gcp, gcp-cloud-functions, go, golang
- Language: Go
- Size: 141 KB
- Stars: 31
- Watchers: 5
- Forks: 14
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# gcp-instance-scheduler
[](LICENSE)
[](https://goreportcard.com/report/github.com/future-architect/gcp-instance-scheduler)
Tools that shutdown GCP Instance on your schedule.
## Abstract
* Shutdown target
* GCE, GKE, SQL
* The label `state-scheduler: true` is required to stop / restart the instance.
* In order to be processed, it is necessary to assign a label to Instance, InstanceGroup or Cluster.
If a label is assigned to Cluster or InstanceGroup, this tool will reduce the size of InstanceGroup to 0.
* Architecture
* Cloud Scheduler --> Pub/Sub --> CloudFunction
* https://cloud.google.com/scheduler/docs/start-and-stop-compute-engine-instances-on-a-schedule
## Config
* nothing special
* [GCP_PROJECT is automation set by CloudFunction](https://cloud.google.com/functions/docs/concepts/go-runtime#contextcontext)
## QuickStart
Shutdown instances with CLI command.
### Install
```bash
go get -u github.com/future-architect/gcp-instance-scheduler/cmd/scheduler
```
### Usage
Need to set `GOOGLE_APPLICATION_CREDENTIALS` in environment variables before cli execution.
[See setup](https://cloud.google.com/docs/authentication/getting-started)
And then you set label to gcp resource
```bash
gcloud compute instances create --zone us-central1-a
gcloud compute instances update --project --update-labels state-scheduler=true
```
Then you can do below commands.
```bash
# stop
$ scheduler stop --project
# restart
$ scheduler restart --project
```
#### Options
You can designate project id and timeout length by using flags.
If you use slack notification, you have to enable slack notification by adding the flag `--slackNotifyEnable`.
```console
>scheduler stop --help
stop is execution command that shutdown gcp resources that assigned target label.
Usage:
scheduler stop [flags]
Flags:
-h, --help help for stop
-p, --project string project id (default $GCP_PROJECT)
-c, --slackChannel string Slack Channel name (should enable slack notify) (default SLACK_CHANNEL)
-s, --slackNotifyEnable Enable slack notification
-t, --slackToken string SlackAPI token (should enable slack notify) (default $SLACK_API_TOKEN)
--timeout int set timeout seconds (default 60)
>scheduler restart --help
restart is launch shutdown gcp resource.
Usage:
scheduler restart [flags]
Flags:
-h, --help help for restart
-p, --project string project id (default $GCP_PROJECT)
-c, --slackChannel string Slack Channel name (should enable slack notify) (default SLACK_CHANNEL)
-s, --slackNotifyEnable Enable slack notification
-t, --slackToken string SlackAPI token (should enable slack notify) (default $SLACK_API_TOKEN)
--timeout int set timeout seconds (default 60)
```
Following variables are used when you did not designate these flags.
|# |flags |variables |
|---|-----------------------|----------------|
| 1 |project(p) |GCP_PROJECT |
| 2 |slackToken |SLACK_API_TOKEN |
| 3 |slackChannel |SLACK_CHANNEL |
## Example: create target resources
Set label for target instance
```sh
# GCE
gcloud compute instances update \
--project \
--update-labels state-scheduler=true
# Instance Group
gcloud compute instance-templates create ... \
--project \
--labels state-scheduler=true
# Cloud SQL (master must be running)
gcloud beta sql instances patch \
--project \
--update-labels state-scheduler=true
# GKE
gcloud container clusters update \
--project \
--zone \
--update-labels state-scheduler=true,restore-size-=
```
## Deploy to GCP CloudFunction
* install [gcloud](https://cloud.google.com/sdk/gcloud/)
### Required variables
When you want to get slack notification, please set these environment variables.
You can get slack notification if and only if these three variables are set.
|# |variables |Note |
|---|----------------|-----------------------------------|
| 1 |SLACK_ENABLE |Slack notification enable ("true") |
| 2 |SLACK_API_TOKEN |Slack api token |
| 3 |SLACK_CHANNEL |Slack channel name |
### Steps
As an example, start an instance between 9 and 22:00 on weekdays.
```sh
# Deploy Cloud Function: slack notification enable
gcloud functions deploy switchInstanceState --project \
--entry-point SwitchInstanceState --runtime go111 \
--trigger-topic instance-scheduler-event \
--set-env-vars SLACK_ENABLE=false
# Create Cloud Scheduler Job(Stop)
gcloud beta scheduler jobs create pubsub shutdown-workday \
--project \
--schedule '0 22 * * 1-5' \
--topic instance-scheduler-event \
--message-body '{"command":"stop"}' \
--time-zone 'Asia/Tokyo' \
--description 'automatically stop instances'
# Create Cloud Scheduler Job(Start)
gcloud beta scheduler jobs create pubsub restart-workday \
--project \
--schedule '0 9 * * 1-5' \
--topic instance-scheduler-event \
--message-body '{"command":"start"}' \
--time-zone 'Asia/Tokyo' \
--description 'automatically restart instances'
```
## Tips: Debug Function
* publish message to pub/sub
* `gcloud pubsub topics publish stop-instance-event --project --message "{"command":"stop"}"`
* confirm Functions log
* `gcloud functions logs read --project --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](LICENSE) file for details