forked from aws/aws-cdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
endpoint.ts
459 lines (405 loc) · 14.8 KB
/
endpoint.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
import { EOL } from 'os';
import * as appscaling from 'aws-cdk-lib/aws-applicationautoscaling';
import * as cloudwatch from 'aws-cdk-lib/aws-cloudwatch';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import * as iam from 'aws-cdk-lib/aws-iam';
import * as cdk from 'aws-cdk-lib/core';
import * as sagemaker from 'aws-cdk-lib/aws-sagemaker';
import { Construct } from 'constructs';
import { EndpointConfig, IEndpointConfig, InstanceProductionVariant } from './endpoint-config';
import { InstanceType } from './instance-type';
import { sameEnv } from './private/util';
import { CfnEndpoint } from 'aws-cdk-lib/aws-sagemaker';
import { ScalableInstanceCount } from './scalable-instance-count';
/*
* Amazon SageMaker automatic scaling doesn't support automatic scaling for burstable instances such
* as T2, because they already allow for increased capacity under increased workloads.
* https://docs.aws.amazon.com/sagemaker/latest/dg/endpoint-auto-scaling-add-console.html
*/
const BURSTABLE_INSTANCE_TYPE_PREFIXES = Object.entries(ec2.InstanceClass)
.filter(([name, _]) => name.startsWith('T'))
.map(([_, prefix]) => `ml.${prefix}.`);
// IEndpoint is stabilized so that it can be used in aws-apigateway SagemakerIntegration
// Exposing it again here so that there is no breakage to aws-sagemaker-alpha
/**
* The Interface for a SageMaker Endpoint resource.
*/
export interface IEndpoint extends sagemaker.IEndpoint {}
/**
* Represents the features common to all production variant types (e.g., instance, serverless) that
* have been associated with an endpoint.
*/
interface IEndpointProductionVariant {
/**
* The name of the production variant.
*/
readonly variantName: string;
/**
* Return the given named metric for Endpoint
*
* @default - sum over 5 minutes
*/
metric(namespace: string, metricName: string, props?: cloudwatch.MetricOptions): cloudwatch.Metric;
}
/**
* Represents an instance production variant that has been associated with an endpoint.
*/
export interface IEndpointInstanceProductionVariant extends IEndpointProductionVariant {
/**
* Metric for the number of invocations
*
* @default - sum over 5 minutes
*/
metricInvocations(props?: cloudwatch.MetricOptions): cloudwatch.Metric;
/**
* Metric for the number of invocations per instance
*
* @default - sum over 5 minutes
*/
metricInvocationsPerInstance(props?: cloudwatch.MetricOptions): cloudwatch.Metric;
/**
* Metric for model latency
*
* @default - average over 5 minutes
*/
metricModelLatency(props?: cloudwatch.MetricOptions): cloudwatch.Metric;
/**
* Metric for overhead latency
*
* @default - average over 5 minutes
*/
metricOverheadLatency(props?: cloudwatch.MetricOptions): cloudwatch.Metric;
/**
* Metric for the number of invocations by HTTP response code
*
* @default - sum over 5 minutes
*/
metricInvocationResponseCode(responseCode: InvocationHttpResponseCode, props?: cloudwatch.MetricOptions): cloudwatch.Metric;
/**
* Metric for disk utilization
*
* @default - average over 5 minutes
*/
metricDiskUtilization(props?: cloudwatch.MetricOptions): cloudwatch.Metric;
/**
* Metric for CPU utilization
*
* @default - average over 5 minutes
*/
metricCpuUtilization(props?: cloudwatch.MetricOptions): cloudwatch.Metric;
/**
* Metric for memory utilization
*
* @default - average over 5 minutes
*/
metricMemoryUtilization(props?: cloudwatch.MetricOptions): cloudwatch.Metric;
/**
* Metric for GPU utilization
*
* @default - average over 5 minutes
*/
metricGpuUtilization(props?: cloudwatch.MetricOptions): cloudwatch.Metric;
/**
* Metric for GPU memory utilization
*
* @default - average over 5 minutes
*/
metricGpuMemoryUtilization(props?: cloudwatch.MetricOptions): cloudwatch.Metric;
/**
* Enable autoscaling for SageMaker Endpoint production variant
*
* @param scalingProps EnableScalingProps
*/
autoScaleInstanceCount(scalingProps: appscaling.EnableScalingProps): ScalableInstanceCount;
}
class EndpointInstanceProductionVariant implements IEndpointInstanceProductionVariant {
public readonly variantName: string;
private readonly endpoint: Endpoint;
private readonly initialInstanceCount: number;
private readonly instanceType: InstanceType;
private scalableInstanceCount?: ScalableInstanceCount;
constructor(endpoint: Endpoint, variant: InstanceProductionVariant) {
this.initialInstanceCount = variant.initialInstanceCount;
this.instanceType = variant.instanceType;
this.variantName = variant.variantName;
this.endpoint = endpoint;
}
public metric(
namespace: string,
metricName: string,
props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return new cloudwatch.Metric({
namespace,
metricName,
dimensions: {
EndpointName: this.endpoint.endpointName,
VariantName: this.variantName,
},
statistic: 'Sum',
...props,
});
}
public metricInvocations(props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return this.metric('AWS/SageMaker', 'Invocations', props);
}
public metricInvocationsPerInstance(props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return this.metric('AWS/SageMaker', 'InvocationsPerInstance', props);
}
public metricModelLatency(props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return this.metric('AWS/SageMaker', 'ModelLatency', {
statistic: 'Average',
...props,
});
}
public metricOverheadLatency(props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return this.metric('AWS/SageMaker', 'OverheadLatency', {
statistic: 'Average',
...props,
});
}
public metricInvocationResponseCode(
responseCode: InvocationHttpResponseCode,
props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return this.metric('AWS/SageMaker', responseCode, props);
}
public metricDiskUtilization(props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return this.metric('/aws/sagemaker/Endpoints', 'DiskUtilization', {
statistic: 'Average',
...props,
});
}
public metricCpuUtilization(props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return this.metric('/aws/sagemaker/Endpoints', 'CPUUtilization', {
statistic: 'Average',
...props,
});
}
public metricMemoryUtilization(props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return this.metric('/aws/sagemaker/Endpoints', 'MemoryUtilization', {
statistic: 'Average',
...props,
});
}
public metricGpuUtilization(props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return this.metric('/aws/sagemaker/Endpoints', 'GPUUtilization', {
statistic: 'Average',
...props,
});
}
public metricGpuMemoryUtilization(props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return this.metric('/aws/sagemaker/Endpoints', 'GPUMemoryUtilization', {
statistic: 'Average',
...props,
});
}
public autoScaleInstanceCount(scalingProps: appscaling.EnableScalingProps): ScalableInstanceCount {
const errors: string[] = [];
if (scalingProps.minCapacity && scalingProps.minCapacity > this.initialInstanceCount) {
errors.push(`minCapacity cannot be greater than initial instance count: ${this.initialInstanceCount}`);
}
if (scalingProps.maxCapacity && scalingProps.maxCapacity < this.initialInstanceCount) {
errors.push(`maxCapacity cannot be less than initial instance count: ${this.initialInstanceCount}`);
}
if (BURSTABLE_INSTANCE_TYPE_PREFIXES.some(prefix => this.instanceType.toString().startsWith(prefix))) {
errors.push(`AutoScaling not supported for burstable instance types like ${this.instanceType}`);
}
if (this.scalableInstanceCount) {
errors.push('AutoScaling of task count already enabled for this service');
}
if (errors.length > 0) {
throw new Error(`Invalid Application Auto Scaling configuration: ${errors.join(EOL)}`);
}
return this.scalableInstanceCount = new ScalableInstanceCount(this.endpoint, 'InstanceCount', {
serviceNamespace: appscaling.ServiceNamespace.SAGEMAKER,
resourceId: `endpoint/${this.endpoint.endpointName}/variant/${this.variantName}`,
dimension: 'sagemaker:variant:DesiredInstanceCount',
role: this.makeScalingRole(),
minCapacity: scalingProps.minCapacity || this.initialInstanceCount,
maxCapacity: scalingProps.maxCapacity || this.initialInstanceCount,
});
}
/**
* Return the service linked role which will automatically be created by Application Auto Scaling
* for scaling purposes.
*
* @see https://docs.aws.amazon.com/autoscaling/application/userguide/application-auto-scaling-service-linked-roles.html
*/
private makeScalingRole(): iam.IRole {
// Use a Service Linked Role.
return iam.Role.fromRoleArn(this.endpoint, 'ScalingRole', cdk.Stack.of(this.endpoint).formatArn({
service: 'iam',
region: '',
resource: 'role/aws-service-role/sagemaker.application-autoscaling.amazonaws.com',
resourceName: 'AWSServiceRoleForApplicationAutoScaling_SageMakerEndpoint',
}));
}
}
/**
* Represents an Endpoint resource defined outside this stack.
*/
export interface EndpointAttributes {
/**
* The ARN of this endpoint.
*/
readonly endpointArn: string;
}
abstract class EndpointBase extends cdk.Resource implements IEndpoint {
/**
* The ARN of the endpoint.
*
* @attribute
*/
public abstract readonly endpointArn: string;
/**
* The name of the endpoint.
*
* @attribute
*/
public abstract readonly endpointName: string;
/**
* Permits an IAM principal to invoke this endpoint
* @param grantee The principal to grant access to
*/
public grantInvoke(grantee: iam.IGrantable) {
return iam.Grant.addToPrincipal({
grantee,
actions: ['sagemaker:InvokeEndpoint'],
resourceArns: [this.endpointArn],
});
}
}
/**
* Construction properties for a SageMaker Endpoint.
*/
export interface EndpointProps {
/**
* Name of the endpoint.
*
* @default - AWS CloudFormation generates a unique physical ID and uses that ID for the
* endpoint's name.
*/
readonly endpointName?: string;
/**
* The endpoint configuration to use for this endpoint.
*/
readonly endpointConfig: IEndpointConfig;
}
/**
* HTTP response codes for Endpoint invocations
*/
export enum InvocationHttpResponseCode {
/**
* 4xx response codes from Endpoint invocations
*/
INVOCATION_4XX_ERRORS = 'Invocation4XXErrors',
/**
* 5xx response codes from Endpoint invocations
*/
INVOCATION_5XX_ERRORS = 'Invocation5XXErrors',
}
/**
* Defines a SageMaker endpoint.
*/
export class Endpoint extends EndpointBase {
/**
* Imports an Endpoint defined either outside the CDK or in a different CDK stack.
* @param scope the Construct scope.
* @param id the resource id.
* @param endpointArn the ARN of the endpoint.
*/
public static fromEndpointArn(scope: Construct, id: string, endpointArn: string): IEndpoint {
return Endpoint.fromEndpointAttributes(scope, id, { endpointArn });
}
/**
* Imports an Endpoint defined either outside the CDK or in a different CDK stack.
* @param scope the Construct scope.
* @param id the resource id.
* @param endpointName the name of the endpoint.
*/
public static fromEndpointName(scope: Construct, id: string, endpointName: string): IEndpoint {
const endpointArn = cdk.Stack.of(scope).formatArn({
service: 'sagemaker',
resource: 'endpoint',
resourceName: endpointName,
});
return Endpoint.fromEndpointAttributes(scope, id, { endpointArn });
}
/**
* Imports an Endpoint defined either outside the CDK or in a different CDK stack.
* @param scope the Construct scope.
* @param id the resource id.
* @param attrs the attributes of the endpoint to import.
*/
public static fromEndpointAttributes(scope: Construct, id: string, attrs: EndpointAttributes): IEndpoint {
const endpointArn = attrs.endpointArn;
const endpointName = cdk.Stack.of(scope).splitArn(endpointArn, cdk.ArnFormat.SLASH_RESOURCE_NAME).resourceName!;
class Import extends EndpointBase {
public readonly endpointArn = endpointArn;
public readonly endpointName = endpointName;
constructor(s: Construct, i: string) {
super(s, i, {
environmentFromArn: endpointArn,
});
}
}
return new Import(scope, id);
}
/**
* The ARN of the endpoint.
*
* @attribute
*/
public readonly endpointArn: string;
/**
* The name of the endpoint.
*
* @attribute
*/
public readonly endpointName: string;
private readonly endpointConfig: IEndpointConfig;
constructor(scope: Construct, id: string, props: EndpointProps) {
super(scope, id, {
physicalName: props.endpointName,
});
this.validateEnvironmentCompatibility(props.endpointConfig);
this.endpointConfig = props.endpointConfig;
// create the endpoint resource
const endpoint = new CfnEndpoint(this, 'Endpoint', {
endpointConfigName: props.endpointConfig.endpointConfigName,
endpointName: this.physicalName,
});
this.endpointName = this.getResourceNameAttribute(endpoint.attrEndpointName);
this.endpointArn = this.getResourceArnAttribute(endpoint.ref, {
service: 'sagemaker',
resource: 'endpoint',
resourceName: this.physicalName,
});
}
private validateEnvironmentCompatibility(endpointConfig: IEndpointConfig): void {
if (!sameEnv(endpointConfig.env.account, this.env.account)) {
throw new Error(`Cannot use endpoint configuration in account ${endpointConfig.env.account} for endpoint in account ${this.env.account}`);
} else if (!sameEnv(endpointConfig.env.region, this.env.region)) {
throw new Error(`Cannot use endpoint configuration in region ${endpointConfig.env.region} for endpoint in region ${this.env.region}`);
}
}
/**
* Get instance production variants associated with endpoint.
*/
public get instanceProductionVariants(): IEndpointInstanceProductionVariant[] {
if (this.endpointConfig instanceof EndpointConfig) {
return this.endpointConfig._instanceProductionVariants.map(v => new EndpointInstanceProductionVariant(this, v));
}
throw new Error('Production variant lookup is not supported for an imported IEndpointConfig');
}
/**
* Find instance production variant based on variant name
* @param name Variant name from production variant
*/
public findInstanceProductionVariant(name: string): IEndpointInstanceProductionVariant {
if (this.endpointConfig instanceof EndpointConfig) {
const variant = this.endpointConfig._findInstanceProductionVariant(name);
return new EndpointInstanceProductionVariant(this, variant);
}
throw new Error('Production variant lookup is not supported for an imported IEndpointConfig');
}
}