-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathtarget-tracking-scaling-policy.ts
261 lines (241 loc) · 10.3 KB
/
target-tracking-scaling-policy.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import * as cloudwatch from '@aws-cdk/aws-cloudwatch';
import * as cdk from '@aws-cdk/core';
import { Construct } from 'constructs';
import { CfnScalingPolicy } from './applicationautoscaling.generated';
import { IScalableTarget } from './scalable-target';
// keep this import separate from other imports to reduce chance for merge conflicts with v2-main
// eslint-disable-next-line no-duplicate-imports, import/order
import { Construct as CoreConstruct } from '@aws-cdk/core';
/**
* Base interface for target tracking props
*
* Contains the attributes that are common to target tracking policies,
* except the ones relating to the metric and to the scalable target.
*
* This interface is reused by more specific target tracking props objects
* in other services.
*/
export interface BaseTargetTrackingProps {
/**
* A name for the scaling policy
*
* @default - Automatically generated name.
*/
readonly policyName?: string;
/**
* Indicates whether scale in by the target tracking policy is disabled.
*
* If the value is true, scale in is disabled and the target tracking policy
* won't remove capacity from the scalable resource. Otherwise, scale in is
* enabled and the target tracking policy can remove capacity from the
* scalable resource.
*
* @default false
*/
readonly disableScaleIn?: boolean;
/**
* Period after a scale in activity completes before another scale in activity can start.
*
* @default Duration.seconds(300) for the following scalable targets: ECS services,
* Spot Fleet requests, EMR clusters, AppStream 2.0 fleets, Aurora DB clusters,
* Amazon SageMaker endpoint variants, Custom resources. For all other scalable
* targets, the default value is Duration.seconds(0): DynamoDB tables, DynamoDB
* global secondary indexes, Amazon Comprehend document classification endpoints,
* Lambda provisioned concurrency
*/
readonly scaleInCooldown?: cdk.Duration;
/**
* Period after a scale out activity completes before another scale out activity can start.
*
* @default Duration.seconds(300) for the following scalable targets: ECS services,
* Spot Fleet requests, EMR clusters, AppStream 2.0 fleets, Aurora DB clusters,
* Amazon SageMaker endpoint variants, Custom resources. For all other scalable
* targets, the default value is Duration.seconds(0): DynamoDB tables, DynamoDB
* global secondary indexes, Amazon Comprehend document classification endpoints,
* Lambda provisioned concurrency
*/
readonly scaleOutCooldown?: cdk.Duration;
}
/**
* Properties for a Target Tracking policy that include the metric but exclude the target
*/
export interface BasicTargetTrackingScalingPolicyProps extends BaseTargetTrackingProps {
/**
* The target value for the metric.
*/
readonly targetValue: number;
/**
* A predefined metric for application autoscaling
*
* The metric must track utilization. Scaling out will happen if the metric is higher than
* the target value, scaling in will happen in the metric is lower than the target value.
*
* Exactly one of customMetric or predefinedMetric must be specified.
*
* @default - No predefined metrics.
*/
readonly predefinedMetric?: PredefinedMetric;
/**
* Identify the resource associated with the metric type.
*
* Only used for predefined metric ALBRequestCountPerTarget.
*
* Example value: `app/<load-balancer-name>/<load-balancer-id>/targetgroup/<target-group-name>/<target-group-id>`
*
* @default - No resource label.
*/
readonly resourceLabel?: string;
/**
* A custom metric for application autoscaling
*
* The metric must track utilization. Scaling out will happen if the metric is higher than
* the target value, scaling in will happen in the metric is lower than the target value.
*
* Exactly one of customMetric or predefinedMetric must be specified.
*
* @default - No custom metric.
*/
readonly customMetric?: cloudwatch.IMetric;
}
/**
* Properties for a concrete TargetTrackingPolicy
*
* Adds the scalingTarget.
*/
export interface TargetTrackingScalingPolicyProps extends BasicTargetTrackingScalingPolicyProps {
/*
* The scalable target
*/
readonly scalingTarget: IScalableTarget;
}
export class TargetTrackingScalingPolicy extends CoreConstruct {
/**
* ARN of the scaling policy
*/
public readonly scalingPolicyArn: string;
constructor(scope: Construct, id: string, props: TargetTrackingScalingPolicyProps) {
if ((props.customMetric === undefined) === (props.predefinedMetric === undefined)) {
throw new Error('Exactly one of \'customMetric\' or \'predefinedMetric\' must be specified.');
}
if (props.customMetric && !props.customMetric.toMetricConfig().metricStat) {
throw new Error('Only direct metrics are supported for Target Tracking. Use Step Scaling or supply a Metric object.');
}
super(scope, id);
const resource = new CfnScalingPolicy(this, 'Resource', {
policyName: props.policyName || cdk.Names.uniqueId(this),
policyType: 'TargetTrackingScaling',
scalingTargetId: props.scalingTarget.scalableTargetId,
targetTrackingScalingPolicyConfiguration: {
customizedMetricSpecification: renderCustomMetric(props.customMetric),
disableScaleIn: props.disableScaleIn,
predefinedMetricSpecification: props.predefinedMetric !== undefined ? {
predefinedMetricType: props.predefinedMetric,
resourceLabel: props.resourceLabel,
} : undefined,
scaleInCooldown: props.scaleInCooldown && props.scaleInCooldown.toSeconds(),
scaleOutCooldown: props.scaleOutCooldown && props.scaleOutCooldown.toSeconds(),
targetValue: props.targetValue,
},
});
this.scalingPolicyArn = resource.ref;
}
}
function renderCustomMetric(metric?: cloudwatch.IMetric): CfnScalingPolicy.CustomizedMetricSpecificationProperty | undefined {
if (!metric) { return undefined; }
const c = metric.toMetricConfig().metricStat!;
if (c.statistic.startsWith('p')) {
throw new Error(`Cannot use statistic '${c.statistic}' for Target Tracking: only 'Average', 'Minimum', 'Maximum', 'SampleCount', and 'Sum' are supported.`);
}
return {
dimensions: c.dimensions,
metricName: c.metricName,
namespace: c.namespace,
statistic: c.statistic,
unit: c.unitFilter,
};
}
/**
* One of the predefined autoscaling metrics
*/
export enum PredefinedMetric {
/**
* DYNAMODB_READ_CAPACITY_UTILIZATIO
* @see https://docs.aws.amazon.com/autoscaling/application/APIReference/API_PredefinedMetricSpecification.html
*/
DYNAMODB_READ_CAPACITY_UTILIZATION = 'DynamoDBReadCapacityUtilization',
/**
* DYANMODB_WRITE_CAPACITY_UTILIZATION
* @see https://docs.aws.amazon.com/autoscaling/application/APIReference/API_PredefinedMetricSpecification.html
*/
DYANMODB_WRITE_CAPACITY_UTILIZATION = 'DynamoDBWriteCapacityUtilization',
/**
* ALB_REQUEST_COUNT_PER_TARGET
* @see https://docs.aws.amazon.com/autoscaling/application/APIReference/API_PredefinedMetricSpecification.html
*/
ALB_REQUEST_COUNT_PER_TARGET = 'ALBRequestCountPerTarget',
/**
* RDS_READER_AVERAGE_CPU_UTILIZATION
* @see https://docs.aws.amazon.com/autoscaling/application/APIReference/API_PredefinedMetricSpecification.html
*/
RDS_READER_AVERAGE_CPU_UTILIZATION = 'RDSReaderAverageCPUUtilization',
/**
* RDS_READER_AVERAGE_DATABASE_CONNECTIONS
* @see https://docs.aws.amazon.com/autoscaling/application/APIReference/API_PredefinedMetricSpecification.html
*/
RDS_READER_AVERAGE_DATABASE_CONNECTIONS = 'RDSReaderAverageDatabaseConnections',
/**
* EC2_SPOT_FLEET_REQUEST_AVERAGE_CPU_UTILIZATION
* @see https://docs.aws.amazon.com/autoscaling/application/APIReference/API_PredefinedMetricSpecification.html
*/
EC2_SPOT_FLEET_REQUEST_AVERAGE_CPU_UTILIZATION = 'EC2SpotFleetRequestAverageCPUUtilization',
/**
* EC2_SPOT_FLEET_REQUEST_AVERAGE_NETWORK_IN
* @see https://docs.aws.amazon.com/autoscaling/application/APIReference/API_PredefinedMetricSpecification.html
*/
EC2_SPOT_FLEET_REQUEST_AVERAGE_NETWORK_IN = 'EC2SpotFleetRequestAverageNetworkIn',
/**
* EC2_SPOT_FLEET_REQUEST_AVERAGE_NETWORK_OUT
* @see https://docs.aws.amazon.com/autoscaling/application/APIReference/API_PredefinedMetricSpecification.html
*/
EC2_SPOT_FLEET_REQUEST_AVERAGE_NETWORK_OUT = 'EC2SpotFleetRequestAverageNetworkOut',
/**
* SAGEMAKER_VARIANT_INVOCATIONS_PER_INSTANCE
* @see https://docs.aws.amazon.com/autoscaling/application/APIReference/API_PredefinedMetricSpecification.html
*/
SAGEMAKER_VARIANT_INVOCATIONS_PER_INSTANCE = 'SageMakerVariantInvocationsPerInstance',
/**
* ECS_SERVICE_AVERAGE_CPU_UTILIZATION
* @see https://docs.aws.amazon.com/autoscaling/application/APIReference/API_PredefinedMetricSpecification.html
*/
ECS_SERVICE_AVERAGE_CPU_UTILIZATION = 'ECSServiceAverageCPUUtilization',
/**
* ECS_SERVICE_AVERAGE_MEMORY_UTILIZATION
* @see https://docs.aws.amazon.com/autoscaling/application/APIReference/API_PredefinedMetricSpecification.html
*/
ECS_SERVICE_AVERAGE_MEMORY_UTILIZATION = 'ECSServiceAverageMemoryUtilization',
/**
* LAMBDA_PROVISIONED_CONCURRENCY_UTILIZATION
* @see https://docs.aws.amazon.com/lambda/latest/dg/monitoring-metrics.html#monitoring-metrics-concurrency
*/
LAMBDA_PROVISIONED_CONCURRENCY_UTILIZATION = 'LambdaProvisionedConcurrencyUtilization',
/**
* KAFKA_BROKER_STORAGE_UTILIZATION
* @see https://docs.aws.amazon.com/autoscaling/application/APIReference/API_PredefinedMetricSpecification.html
*/
KAFKA_BROKER_STORAGE_UTILIZATION = 'KafkaBrokerStorageUtilization',
/**
* ELASTIC_CACHE_PRIMARY_ENGINE_CPU_UTILIZATION
* @see https://docs.aws.amazon.com/autoscaling/application/APIReference/API_PredefinedMetricSpecification.html
*/
ELASTICACHE_PRIMARY_ENGINE_CPU_UTILIZATION = 'ElastiCachePrimaryEngineCPUUtilization',
/**
* ELASTIC_CACHE_REPLICA_ENGINE_CPU_UTILIZATION
* @see https://docs.aws.amazon.com/autoscaling/application/APIReference/API_PredefinedMetricSpecification.html
*/
ELASTICACHE_REPLICA_ENGINE_CPU_UTILIZATION = 'ElastiCacheReplicaEngineCPUUtilization',
/**
* ELASTIC_CACHE_REPLICA_ENGINE_CPU_UTILIZATION
* @see https://docs.aws.amazon.com/autoscaling/application/APIReference/API_PredefinedMetricSpecification.html
*/
ELASTICACHE_DATABASE_MEMORY_USAGE_COUNTED_FOR_EVICT_PERCENTAGE = 'ElastiCacheDatabaseMemoryUsageCountedForEvictPercentage',
}