-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathscalable-target.ts
298 lines (255 loc) · 7.65 KB
/
scalable-target.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import { Construct } from 'constructs';
import { CfnScalableTarget } from './applicationautoscaling.generated';
import { Schedule } from './schedule';
import { BasicStepScalingPolicyProps, StepScalingPolicy } from './step-scaling-policy';
import { BasicTargetTrackingScalingPolicyProps, TargetTrackingScalingPolicy } from './target-tracking-scaling-policy';
import * as iam from '../../aws-iam';
import { IResource, Lazy, Resource, withResolved } from '../../core';
export interface IScalableTarget extends IResource {
/**
* @attribute
*/
readonly scalableTargetId: string;
}
/**
* Properties for a scalable target
*/
export interface ScalableTargetProps {
/**
* The minimum value that Application Auto Scaling can use to scale a target during a scaling activity.
*/
readonly minCapacity: number;
/**
* The maximum value that Application Auto Scaling can use to scale a target during a scaling activity.
*/
readonly maxCapacity: number;
/**
* Role that allows Application Auto Scaling to modify your scalable target.
*
* @default A role is automatically created
*/
readonly role?: iam.IRole;
/**
* The resource identifier to associate with this scalable target.
*
* This string consists of the resource type and unique identifier.
*
* Example value: `service/ecsStack-MyECSCluster-AB12CDE3F4GH/ecsStack-MyECSService-AB12CDE3F4GH`
*
* @see https://docs.aws.amazon.com/autoscaling/application/APIReference/API_RegisterScalableTarget.html
*/
readonly resourceId: string;
/**
* The scalable dimension that's associated with the scalable target.
*
* Specify the service namespace, resource type, and scaling property.
*
* Example value: `ecs:service:DesiredCount`
* @see https://docs.aws.amazon.com/autoscaling/application/APIReference/API_ScalingPolicy.html
*/
readonly scalableDimension: string;
/**
* The namespace of the AWS service that provides the resource or
* custom-resource for a resource provided by your own application or
* service.
*
* For valid AWS service namespace values, see the RegisterScalableTarget
* action in the Application Auto Scaling API Reference.
*
* @see https://docs.aws.amazon.com/autoscaling/application/APIReference/API_RegisterScalableTarget.html
*/
readonly serviceNamespace: ServiceNamespace;
}
/**
* Define a scalable target
*/
export class ScalableTarget extends Resource implements IScalableTarget {
public static fromScalableTargetId(scope: Construct, id: string, scalableTargetId: string): IScalableTarget {
class Import extends Resource implements IScalableTarget {
public readonly scalableTargetId = scalableTargetId;
}
return new Import(scope, id);
}
/**
* ID of the Scalable Target
*
* Example value: `service/ecsStack-MyECSCluster-AB12CDE3F4GH/ecsStack-MyECSService-AB12CDE3F4GH|ecs:service:DesiredCount|ecs`
*
* @attribute
*/
public readonly scalableTargetId: string;
/**
* The role used to give AutoScaling permissions to your resource
*/
public readonly role: iam.IRole;
private readonly actions = new Array<CfnScalableTarget.ScheduledActionProperty>();
constructor(scope: Construct, id: string, props: ScalableTargetProps) {
super(scope, id);
withResolved(props.maxCapacity, max => {
if (max < 0) {
throw new RangeError(`maxCapacity cannot be negative, got: ${props.maxCapacity}`);
}
});
withResolved(props.minCapacity, min => {
if (min < 0) {
throw new RangeError(`minCapacity cannot be negative, got: ${props.minCapacity}`);
}
});
withResolved(props.minCapacity, props.maxCapacity, (min, max) => {
if (max < min) {
throw new RangeError(`minCapacity (${props.minCapacity}) should be lower than maxCapacity (${props.maxCapacity})`);
}
});
this.role = props.role || new iam.Role(this, 'Role', {
assumedBy: new iam.ServicePrincipal('application-autoscaling.amazonaws.com'),
});
const resource = new CfnScalableTarget(this, 'Resource', {
maxCapacity: props.maxCapacity,
minCapacity: props.minCapacity,
resourceId: props.resourceId,
roleArn: this.role.roleArn,
scalableDimension: props.scalableDimension,
scheduledActions: Lazy.any({ produce: () => this.actions }, { omitEmptyArray: true }),
serviceNamespace: props.serviceNamespace,
});
this.scalableTargetId = resource.ref;
}
/**
* Add a policy statement to the role's policy
*/
public addToRolePolicy(statement: iam.PolicyStatement) {
this.role.addToPrincipalPolicy(statement);
}
/**
* Scale out or in based on time
*/
public scaleOnSchedule(id: string, action: ScalingSchedule) {
if (action.minCapacity === undefined && action.maxCapacity === undefined) {
throw new Error(`You must supply at least one of minCapacity or maxCapacity, got ${JSON.stringify(action)}`);
}
// add a warning on synth when minute is not defined in a cron schedule
action.schedule._bind(this);
this.actions.push({
scheduledActionName: id,
schedule: action.schedule.expressionString,
startTime: action.startTime,
endTime: action.endTime,
scalableTargetAction: {
maxCapacity: action.maxCapacity,
minCapacity: action.minCapacity,
},
});
}
/**
* Scale out or in, in response to a metric
*/
public scaleOnMetric(id: string, props: BasicStepScalingPolicyProps) {
return new StepScalingPolicy(this, id, { ...props, scalingTarget: this });
}
/**
* Scale out or in in order to keep a metric around a target value
*/
public scaleToTrackMetric(id: string, props: BasicTargetTrackingScalingPolicyProps) {
return new TargetTrackingScalingPolicy(this, id, { ...props, scalingTarget: this });
}
}
/**
* A scheduled scaling action
*/
export interface ScalingSchedule {
/**
* When to perform this action.
*/
readonly schedule: Schedule;
/**
* When this scheduled action becomes active.
*
* @default The rule is activate immediately
*/
readonly startTime?: Date
/**
* When this scheduled action expires.
*
* @default The rule never expires.
*/
readonly endTime?: Date;
/**
* The new minimum capacity.
*
* During the scheduled time, if the current capacity is below the minimum
* capacity, Application Auto Scaling scales out to the minimum capacity.
*
* At least one of maxCapacity and minCapacity must be supplied.
*
* @default No new minimum capacity
*/
readonly minCapacity?: number;
/**
* The new maximum capacity.
*
* During the scheduled time, the current capacity is above the maximum
* capacity, Application Auto Scaling scales in to the maximum capacity.
*
* At least one of maxCapacity and minCapacity must be supplied.
*
* @default No new maximum capacity
*/
readonly maxCapacity?: number;
}
/**
* The service that supports Application AutoScaling
*/
export enum ServiceNamespace {
/**
* Elastic Container Service
*/
ECS = 'ecs',
/**
* Elastic Map Reduce
*/
ELASTIC_MAP_REDUCE = 'elasticmapreduce',
/**
* Elastic Compute Cloud
*/
EC2 = 'ec2',
/**
* App Stream
*/
APPSTREAM = 'appstream',
/**
* Dynamo DB
*/
DYNAMODB = 'dynamodb',
/**
* Relational Database Service
*/
RDS = 'rds',
/**
* SageMaker
*/
SAGEMAKER = 'sagemaker',
/**
* Custom Resource
*/
CUSTOM_RESOURCE = 'custom-resource',
/**
* Lambda
*/
LAMBDA = 'lambda',
/**
* Comprehend
*/
COMPREHEND = 'comprehend',
/**
* Kafka
*/
KAFKA = 'kafka',
/**
* ElastiCache
*/
ELASTICACHE = 'elasticache',
/**
* Neptune
*/
NEPTUNE = 'neptune',
}