-
Notifications
You must be signed in to change notification settings - Fork 0
/
me_sm_endpoint.ts
159 lines (142 loc) · 3.77 KB
/
me_sm_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
/*
* Copyright 2023-2024 Amazon.com, Inc. or its affiliates.
*/
import {
CfnEndpoint,
CfnEndpointConfig,
CfnModel
} from "aws-cdk-lib/aws-sagemaker";
import { Construct } from "constructs";
/**
* Represents the properties required to configure an OSML model endpoint.
*
* @interface MESMEndpointProps
*/
export interface MESMEndpointProps {
/**
* The Amazon Resource Name (ARN) of the role that provides permissions for the endpoint.
*
* @type {string}
*/
roleArn: string;
/**
* The URI of the Amazon Elastic Container Registry (ECR) container image.
*
* @type {string}
*/
ecrContainerUri: string;
/**
* The name of the machine learning model.
*
* @type {string}
*/
modelName: string;
/**
* Whether to include segmentation masks in the model output.
*
* @type {string}
*/
enableSegmentation: boolean;
/**
* The initial number of instances to run for the endpoint.
*
* @type {number}
*/
initialInstanceCount: number;
/**
* The initial weight for the model variant when using traffic splitting.
*
* @type {number}
*/
initialVariantWeight: number;
/**
* The instance type for the endpoint.
*
* @type {string}
*/
instanceType: string;
/**
* The name of the model variant.
*
* @type {string}
*/
variantName: string;
/**
* The security group ID to associate with the endpoint.
*
* @type {string}
*/
securityGroupId: string;
/**
* An array of subnet IDs where the endpoint should be deployed.
*
* @type {string[]}
*/
subnetIds: string[];
/**
* The access mode for the model repository (e.g., "ReadWrite" or "ReadOnly").
*
* @type {string}
*/
repositoryAccessMode: string;
}
/**
* Represents an AWS SageMaker endpoint for a specified model.
*/
export class MESMEndpoint extends Construct {
public model: CfnModel;
public endpointConfig: CfnEndpointConfig;
public endpoint: CfnEndpoint;
/**
* Creates a SageMaker endpoint for the specified model.
*
* @param {Construct} scope - The scope/stack in which to define this construct.
* @param {string} id - The id of this construct within the current scope.
* @param {MESMEndpointProps} props - The properties of this construct.
* @returns MESMEndpoint- The MESMEndpoint construct.
*/
constructor(scope: Construct, id: string, props: MESMEndpointProps) {
super(scope, id);
// Create a SageMaker model
this.model = new CfnModel(this, id, {
executionRoleArn: props.roleArn,
containers: [
{
image: props.ecrContainerUri,
environment: {
MODEL_SELECTION: props.modelName,
ENABLE_SEGMENTATION: props.enableSegmentation ? "true" : "false"
},
imageConfig: {
repositoryAccessMode: props.repositoryAccessMode
}
}
],
vpcConfig: {
subnets: props.subnetIds,
securityGroupIds: [props.securityGroupId]
}
});
// Configure the SageMaker endpoint settings
this.endpointConfig = new CfnEndpointConfig(this, `${id}-EndpointConfig`, {
productionVariants: [
{
initialInstanceCount: props.initialInstanceCount,
initialVariantWeight: props.initialVariantWeight,
instanceType: props.instanceType,
modelName: this.model.attrModelName,
variantName: props.variantName
}
],
tags: [
{ key: "Name", value: props.modelName },
{ key: "Timestamp", value: new Date().toISOString() }
]
});
// Host a SageMaker endpoint on top of the imported model container
this.endpoint = new CfnEndpoint(this, `${id}-Endpoint`, {
endpointConfigName: this.endpointConfig.attrEndpointConfigName,
endpointName: props.modelName
});
}
}