/
scalable-task-count.ts
153 lines (136 loc) · 4.76 KB
/
scalable-task-count.ts
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { Construct } from 'constructs';
import * as appscaling from '../../../aws-applicationautoscaling';
import * as cloudwatch from '../../../aws-cloudwatch';
import * as elbv2 from '../../../aws-elasticloadbalancingv2';
/**
* The properties of a scalable attribute representing task count.
*/
export interface ScalableTaskCountProps extends appscaling.BaseScalableAttributeProps {
}
/**
* The scalable attribute representing task count.
*/
export class ScalableTaskCount extends appscaling.BaseScalableAttribute {
/**
* Constructs a new instance of the ScalableTaskCount class.
*/
constructor(scope: Construct, id: string, props: ScalableTaskCountProps) {
super(scope, id, props);
}
/**
* Scales in or out based on a specified scheduled time.
*/
public scaleOnSchedule(id: string, props: appscaling.ScalingSchedule) {
return super.doScaleOnSchedule(id, props);
}
/**
* Scales in or out based on a specified metric value.
*/
public scaleOnMetric(id: string, props: appscaling.BasicStepScalingPolicyProps) {
return super.doScaleOnMetric(id, props);
}
/**
* Scales in or out to achieve a target CPU utilization.
*/
public scaleOnCpuUtilization(id: string, props: CpuUtilizationScalingProps) {
return super.doScaleToTrackMetric(id, {
predefinedMetric: appscaling.PredefinedMetric.ECS_SERVICE_AVERAGE_CPU_UTILIZATION,
policyName: props.policyName,
disableScaleIn: props.disableScaleIn,
targetValue: props.targetUtilizationPercent,
scaleInCooldown: props.scaleInCooldown,
scaleOutCooldown: props.scaleOutCooldown,
});
}
/**
* Scales in or out to achieve a target memory utilization.
*/
public scaleOnMemoryUtilization(id: string, props: MemoryUtilizationScalingProps) {
return super.doScaleToTrackMetric(id, {
predefinedMetric: appscaling.PredefinedMetric.ECS_SERVICE_AVERAGE_MEMORY_UTILIZATION,
targetValue: props.targetUtilizationPercent,
policyName: props.policyName,
disableScaleIn: props.disableScaleIn,
scaleInCooldown: props.scaleInCooldown,
scaleOutCooldown: props.scaleOutCooldown,
});
}
/**
* Scales in or out to achieve a target Application Load Balancer request count per target.
*/
public scaleOnRequestCount(id: string, props: RequestCountScalingProps) {
const resourceLabel = props.targetGroup.firstLoadBalancerFullName +
'/' + props.targetGroup.targetGroupFullName;
return super.doScaleToTrackMetric(id, {
predefinedMetric: appscaling.PredefinedMetric.ALB_REQUEST_COUNT_PER_TARGET,
resourceLabel,
targetValue: props.requestsPerTarget,
policyName: props.policyName,
disableScaleIn: props.disableScaleIn,
scaleInCooldown: props.scaleInCooldown,
scaleOutCooldown: props.scaleOutCooldown,
});
}
/**
* Scales in or out to achieve a target on a custom metric.
*/
public scaleToTrackCustomMetric(id: string, props: TrackCustomMetricProps) {
return super.doScaleToTrackMetric(id, {
customMetric: props.metric,
targetValue: props.targetValue,
policyName: props.policyName,
disableScaleIn: props.disableScaleIn,
scaleInCooldown: props.scaleInCooldown,
scaleOutCooldown: props.scaleOutCooldown,
});
}
}
/**
* The properties for enabling scaling based on CPU utilization.
*/
export interface CpuUtilizationScalingProps extends appscaling.BaseTargetTrackingProps {
/**
* The target value for CPU utilization across all tasks in the service.
*/
readonly targetUtilizationPercent: number;
}
/**
* The properties for enabling scaling based on memory utilization.
*/
export interface MemoryUtilizationScalingProps extends appscaling.BaseTargetTrackingProps {
/**
* The target value for memory utilization across all tasks in the service.
*/
readonly targetUtilizationPercent: number;
}
/**
* The properties for enabling scaling based on Application Load Balancer (ALB) request counts.
*/
export interface RequestCountScalingProps extends appscaling.BaseTargetTrackingProps {
/**
* The number of ALB requests per target.
*/
readonly requestsPerTarget: number;
/**
* The ALB target group name.
*/
readonly targetGroup: elbv2.ApplicationTargetGroup;
}
/**
* The properties for enabling target tracking scaling based on a custom CloudWatch metric.
*/
export interface TrackCustomMetricProps extends appscaling.BaseTargetTrackingProps {
/**
* The custom CloudWatch metric to track.
*
* The metric must represent utilization; that is, you will always get the following behavior:
*
* - metric > targetValue => scale out
* - metric < targetValue => scale in
*/
readonly metric: cloudwatch.IMetric;
/**
* The target value for the custom CloudWatch metric.
*/
readonly targetValue: number;
}