-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
Copy pathscalable-target.ts
256 lines (225 loc) · 6.94 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
import iam = require('@aws-cdk/aws-iam');
import cdk = require('@aws-cdk/cdk');
import { CfnScalableTarget } from './applicationautoscaling.generated';
import { BasicStepScalingPolicyProps, StepScalingPolicy } from './step-scaling-policy';
import { BasicTargetTrackingScalingPolicyProps, TargetTrackingScalingPolicy } from './target-tracking-scaling-policy';
/**
* 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 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 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 cdk.Construct {
/**
* ID of the Scalable Target
*
* @example service/ecsStack-MyECSCluster-AB12CDE3F4GH/ecsStack-MyECSService-AB12CDE3F4GH|ecs:service:DesiredCount|ecs
*/
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: cdk.Construct, id: string, props: ScalableTargetProps) {
super(scope, id);
if (props.maxCapacity < 0) {
throw new RangeError(`maxCapacity cannot be negative, got: ${props.maxCapacity}`);
}
if (props.minCapacity < 0) {
throw new RangeError(`minCapacity cannot be negative, got: ${props.minCapacity}`);
}
if (props.maxCapacity < props.minCapacity) {
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: this.actions,
serviceNamespace: props.serviceNamespace
});
this.scalableTargetId = resource.scalableTargetId;
}
/**
* Add a policy statement to the role's policy
*/
public addToRolePolicy(statement: iam.PolicyStatement) {
this.role.addToPolicy(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)}`);
}
this.actions.push({
scheduledActionName: id,
schedule: action.schedule,
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.
*
* Support formats:
* - at(yyyy-mm-ddThh:mm:ss)
* - rate(value unit)
* - cron(fields)
*
* "At" expressions are useful for one-time schedules. Specify the time in
* UTC.
*
* For "rate" expressions, value is a positive integer, and unit is minute,
* minutes, hour, hours, day, or days.
*
* For more information about cron expressions, see https://en.wikipedia.org/wiki/Cron.
*
* @example rate(12 hours)
*/
readonly schedule: string;
/**
* 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
*/
ElasticMapReduce = 'elasticmapreduce',
/**
* Elastic Compute Cloud
*/
Ec2 = 'ec2',
/**
* App Stream
*/
AppStream = 'appstream',
/**
* Dynamo DB
*/
DynamoDb = 'dynamodb',
/**
* Relational Database Service
*/
Rds = 'rds',
/**
* SageMaker
*/
SageMaker = 'sagemaker',
/**
* Custom Resource
*/
CustomResource = 'custom-resource',
}