Automatic startup and shutdown of compute instances in Google Cloud Platform. Define and manage in one place schedules for VMs in multiple projects and reduce cost of your CGP cloud.
Solution's architecture is similar to described in th article: Scheduling compute instances with Cloud Scheduler
Component | Service | Description |
---|---|---|
configuration | Datastore | Scheduler configuration |
instance-scheduler | Cloud Functions | Function starting/stopping Compute Instances |
instance-scheduler-topic | Pub/Sub Topic | Topic for events triggering function |
instance-scheduler-job | Cloud Scheduler | Cron job for function triggering |
- Install gcloud
- Create a GCP project, set up billing
- Enable required APIs using this link or manually:
Instalation is similat to presented in Google Cloud tutorial.
- Prepare environment
export GCP_PROJECT="Project id where scheduler will be installed"
export GCP_REGION="Region where scheduler will be installed"
export ENV_PROJECTS_LIST="You have to enable scheduling for every project separately. Set comma separated projects list with scheduling enabled e.g. project1,project2,project3"
export ENV_TIMEZONE="Scheduler timezone e.g. Europe/Warsaw"
- Create role for scheduler
gcloud iam roles create InstanceSchedulerRole \
--description "Instance Scheduler for automatic start/stop of instances" \
--permissions compute.instances.list,compute.instances.start,compute.instances.stop \
--stage GA --project $GCP_PROJECT
- Create service account for function
gcloud iam service-accounts create instance-scheduler \
--description="Instance scheduler" \
--display-name="instance-scheduler"
- Assign roles to service account
gcloud projects add-iam-policy-binding $GCP_PROJECT \
--member="serviceAccount:instance-scheduler@$GCP_PROJECT.iam.gserviceaccount.com" \
--role="projects/$GCP_PROJECT/roles/InstanceSchedulerRole"
gcloud projects add-iam-policy-binding $GCP_PROJECT \
--member="serviceAccount:instance-scheduler@$GCP_PROJECT.iam.gserviceaccount.com" \
--role="roles/datastore.viewer"
- Create Pub/Sub topic for function triggering
gcloud pubsub topics create instance-scheduler-topic
- Clone this project and and open function folder:
git clone [email protected]:lmaczulajtys/gcp-instance-scheduler.git
cd gcp-instance-scheduler/functions/instance_scheduler
- Create function with pub/sub trigger
gcloud functions deploy instance-scheduler \
--trigger-topic=instance-scheduler-topic \
--region=$GCP_REGION \
--runtime=python38 \
--entry-point=main \
--service-account=instance-scheduler@$GCP_PROJECT.iam.gserviceaccount.com \
--set-env-vars=^:^ENV_PROJECTS_LIST=$ENV_PROJECTS_LIST:ENV_TIMEZONE=$ENV_TIMEZONE \
--max-instances=1
- Create Cloud Scheduler Job
gcloud scheduler jobs create pubsub instance-scheduler-job --schedule="*/5 * * * *" --topic=instance-scheduler-topic --message-body=run
Currently, you have to create configuration manually. CLI is planned.
Scheduling configuration is stored in Datastore in gcp-instance-scheduler
namespace in two entity types:
- Period - information when instance should be started or stopped
- name - period unique name
- description - period description
- beginTime - instance start time hour e.g. 00:00, 08:30, 18:45
- endTime - instance stop time hour e.g. 00:00, 08:30, 18:45
- weekdays - integer array od week days numbers (monday - 0, sunday - 6) where period is active
- Schedule - schedule with one or multiple periods. Instances are assigned to schedules - not periods.
- name - schedule unique name
- description - schedule description
- periods - string array od periods in schedule
To create Period
- Create entity
- Set napespace to
gcp-instance-scheduler
- Set Kind to
Period
- Set Key identifier to
Custom name
- Set another values. Be careful with weekdays!!! Use example below for whole week:
{
"values": [
{
"integerValue": "0"
},
{
"integerValue": "1"
},
{
"integerValue": "2"
},
{
"integerValue": "3"
},
{
"integerValue": "4"
},
{
"integerValue": "5"
},
{
"integerValue": "6"
}
]
}
To create Schedule
- Create entity
- Set napespace to
gcp-instance-scheduler
- Set Kind to
Schedule
- Set Key identifier to
Custom name
- Set another values. Be careful with schedules!!! Use example below:
{
"values": [
{
"stringValue": "business-days"
}
]
}
To assign instance to configured schedule, you only have to add label schedule
to your instance with schedule name as a value. For example: schedule=business-days
or schedule=dev
.
To disable scheduling, remove label schedule
from your instance.
You have to enable scheduling in every project separately.
- Setup
export SCHEDULER_PROJECT="project id with scheduling function"
export NEW_PROJECT="project id where you want to enable scheduling"
- Create role for scheduler in project and assign it to service account from scheduler project.
gcloud iam roles create InstanceSchedulerRole \
--description "Instance Scheduler for automatic start/stop of instances" \
--permissions compute.instances.list,compute.instances.start,compute.instances.stop \
--stage GA --project $NEW_PROJECT
gcloud projects add-iam-policy-binding $GCP_PROJECT \
--member="serviceAccount:instance-scheduler@$SCHEDULER_PROJECT.iam.gserviceaccount.com" \
--role="projects/$NEW_PROJECT/roles/InstanceSchedulerRole"
- Edit instance-scheduler function and extend
PROJECTS_LIST
environment variable. Add comma and new project id e.g. project1,project2,new-project - Click next and deploy function.
This project is licensed under the Apache 2.0 License - see the LICENSE file for details.
Main inspiration: awslabs/aws-instance-scheduler