-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathdynamic_autoscaling_watching_CPU.aws
29 lines (21 loc) · 1.9 KB
/
dynamic_autoscaling_watching_CPU.aws
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# Title: Group of instances scaling with CPU consumption
# Tags: infra, autoscaling
# Description: Create an autoscaling group of instances and watch their CPU to dynamically allocate/delete instances when needed.
# Create the instances launch configuration
launchconfig = create launchconfiguration image={instance.image} keypair={instance.keypair} name=scalingLaunchConf type={instance.type}
# Create the scalinggroup
create scalinggroup desired-capacity=2 launchconfiguration=$launchconfig max-size={instance.max-number} min-size={instance.min-number} name=instancesScalingGroup subnets={instance.subnets}
# Create a scaling policy to add instances (scale-in) and a scaling policy to remove instances (scale-out)
adjustmentType = ChangeInCapacity
scalein = create scalingpolicy adjustment-scaling=1 adjustment-type=$adjustmentType name=policy-scaling-in scalinggroup=instancesScalingGroup
scaleout = create scalingpolicy adjustment-scaling=-1 adjustment-type=$adjustmentType name=policy-step-scaling-2 scalinggroup=instancesScalingGroup
# metrics statistic functions
statFunction = Average
alarmThreshold = 75
monitoredMetric = CPUUtilization
# Add a monitoring alarm to enable scalein when CPU load is above 75% during 2 * 5 min
create alarm namespace=AWS/EC2 dimensions=AutoScalingGroupName:instancesScalingGroup evaluation-periods=2 metric=$monitoredMetric name=scaleinAlarm operator=GreaterThanOrEqualToThreshold period=300 statistic-function=$statFunction threshold=$alarmThreshold
attach alarm name=scaleinAlarm action-arn=$scalein
# Add a monitoring alarm to enable scaleout when CPU load is below 75% during 2 * 5 min
create alarm namespace=AWS/EC2 dimensions=AutoScalingGroupName:instancesScalingGroup evaluation-periods=2 metric=$monitoredMetric name=scaleoutAlarm operator=LessThanOrEqualToThreshold period=300 statistic-function=$statFunction threshold=$alarmThreshold
attach alarm name=scaleoutAlarm action-arn=$scaleout