There are 2 parts in order to perform this process
-
rulegen
serverless code to shedule cloudwatch events for autoscaling. -
rulegen-scale
serverless code which gets invoked byrulegen
cloudwatch rules to perform autoscaling on defined autoscaling groups.
-
sheduled events for multiple resource and time, autoscaling will be defined by use in for of json as the syntax defined in rulegen
-
This json will be uploaded to s3 bucket, before uploading bucket-arn and file name has to be defined in
serverless.yml
file as defined in rulegen -
On s3 bucket put event for that file
rulegen
lambda will generate cloudwatch event rules based on shedules and minimum count defined in json file by the user. -
This rules will trigger next lambda rulegen-scale which will perform task of modifing autoscaling minimum count of ec2/fargate and after updating autoscaling count, event rule will be removed as it was been made to run once only.
To install nodejs click here
-
Install serverless on your local device.
-
sudo npm install -G serverless
-
Now inside git repo directory run:
npm i -D serverless-dotenv-plugin
-
-
Create a s3 bucket on your aws account where you will upload your sheduled scaling json file.
-
provide required example variables values at
.env.dev
file as followsENV_AWS_REGION="<YOUR-REGION>" ENV_AWS_PROFILE="<YOUR-PROFILE>" BUCKET_NAME="shedule-event-ec2" BUCKET_FILE="test.json"
-
run
serverless deploy
This now in you aws account in s3 bucket shedule-event-ec2
you can put scaling time shedules in file example/shedule.json
same can be written in yaml as well like: example/shedule.yml
like follows:
---
- service_type: fargate
scaling_group_name: service/cluster-wp/fastapi
shedule_table:
date: 30/11/2021
shedule:
- start: '16:40:00'
end: '16:57:00'
count: 4
- start: '16:55:00'
end: '17:57:00'
count: 6
- service_type: ec2
scaling_group_name: scl
shedule_table:
date: 30/11/2021
shedule:
- start: '16:10:00'
end: '17:37:00'
count: 4
- start: '17:35:00'
end: '18:57:00'
count: 6