-
Notifications
You must be signed in to change notification settings - Fork 7
/
sagemaker-studio-user-stack.ts
321 lines (286 loc) · 12.7 KB
/
sagemaker-studio-user-stack.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
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as sagemaker from 'aws-cdk-lib/aws-sagemaker';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import * as apigateway from 'aws-cdk-lib/aws-apigateway';
import * as iam from "aws-cdk-lib/aws-iam";
import * as s3 from 'aws-cdk-lib/aws-s3';
import * as ssm from 'aws-cdk-lib/aws-ssm';
import { NagSuppressions } from 'cdk-nag'
const sageMakerImageArnMapping = {
'us-east-1': "081325390199",
'us-east-2': "429704687514",
'us-west-1': "742091327244",
'us-west-2':"236514542706",
'af-south-1':"559312083959",
'ap-east-1':"493642496378",
'ap-south-1':"394103062818",
'ap-northeast-2':"806072073708",
'ap-southeast-1':"492261229750",
'ap-southeast-2':"452832661640",
'ap-northeast-1':"102112518831",
'ca-central-1':"310906938811",
'eu-central-1':"936697816551",
'eu-west-1':"470317259841",
'eu-west-2':"712779665605",
'eu-west-3':"615547856133",
'eu-north-1':"243637512696",
'eu-south-1':"592751261982",
'sa-east-1': "782484402741",
}
export class SageMakerStudioUserStack extends cdk.Stack {
public readonly sagemakerStudioDomainId: string;
readonly mlflowDeployBucketName = `mlflow-sagemaker-${this.region}-${this.account}`
constructor(
scope: Construct,
id: string,
httpGatewayStackName: string,
restApiGateway: apigateway.RestApi,
domainId: string,
accessLogs: s3.Bucket,
props?: cdk.StackProps
){
super(scope, id, props);
// mlflow deployment S3 bucket
const mlFlowDeployBucket = new s3.Bucket(this, "mlFlowDeployBucket", {
versioned: false,
bucketName: this.mlflowDeployBucketName,
publicReadAccess: false,
blockPublicAccess: s3.BlockPublicAccess.BLOCK_ALL,
removalPolicy: cdk.RemovalPolicy.DESTROY,
autoDeleteObjects: true,
encryption: s3.BucketEncryption.KMS_MANAGED,
enforceSSL: true,
serverAccessLogsBucket: accessLogs,
serverAccessLogsPrefix: 'mlflow-deploy'
})
const mlflowDeployBucketParam = new ssm.StringParameter(this, 'mlflowRestApiId', {
parameterName: 'mlflow-deploy-bucket',
stringValue: this.mlflowDeployBucketName,
});
// Policy to access the parameters from the Notebook for lab setup
const ssmPolicy = new iam.PolicyDocument({
statements: [
new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
resources: [
`arn:aws:ssm:${this.region}:${this.account}:parameter/mlflow-restApiId`,
`arn:aws:ssm:${this.region}:${this.account}:parameter/mlflow-restApiUrl`,
`arn:aws:ssm:${this.region}:${this.account}:parameter/mlflow-deploy-bucket`,
`arn:aws:ssm:${this.region}:${this.account}:parameter/mlflow-uiUrl`
],
actions: [
"ssm:GetParameters",
"ssm:GetParameter",
]
})
]
})
// Policy to have admin access to MLflow
const restApiAdminPolicy = new iam.PolicyDocument({
statements: [
new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
resources: [
`arn:aws:execute-api:${this.region}:${this.account}:${restApiGateway.restApiId}/*/*/*`
],
actions: ["execute-api:Invoke"],
})
],
})
const s3bucketPolicy = new iam.PolicyDocument({
statements: [
new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
resources: [
`arn:aws:s3:::${this.mlflowDeployBucketName}`,
`arn:aws:s3:::mlflow-${this.account}-${this.region}`,
`arn:aws:s3:::${this.mlflowDeployBucketName}/*`,
`arn:aws:s3:::mlflow-${this.account}-${this.region}/*`
],
actions: ["s3:ListBucket","s3:GetObject", "s3:PutObject", "s3:DeleteObject", "s3:PutObjectTagging"],
})
],
})
// Policy to have read-only access to MLflow
const restApiReaderPolicy = new iam.PolicyDocument({
statements: [
new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
resources: [
`arn:aws:execute-api:${this.region}:${this.account}:${restApiGateway.restApiId}/*/GET/*`,
`arn:aws:execute-api:${this.region}:${this.account}:${restApiGateway.restApiId}/*/POST/api/2.0/mlflow/runs/search`,
`arn:aws:execute-api:${this.region}:${this.account}:${restApiGateway.restApiId}/*/POST/api/2.0/mlflow/experiments/search`
],
actions: ["execute-api:Invoke"],
})
],
})
// Model approver
const restApiModelApprover = new iam.PolicyDocument({
statements: [
new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
resources: [
`arn:aws:execute-api:${this.region}:${this.account}:${restApiGateway.restApiId}/*/GET/*`,
`arn:aws:execute-api:${this.region}:${this.account}:${restApiGateway.restApiId}/*/POST/api/2.0/mlflow/runs/search`,
`arn:aws:execute-api:${this.region}:${this.account}:${restApiGateway.restApiId}/*/POST/api/2.0/mlflow/experiments/search`,
`arn:aws:execute-api:${this.region}:${this.account}:${restApiGateway.restApiId}/*/POST/api/2.0/mlflow/model-versions/*`,
`arn:aws:execute-api:${this.region}:${this.account}:${restApiGateway.restApiId}/*/POST/api/2.0/mlflow/registered-models/*`,
],
actions: ["execute-api:Invoke"],
})
],
})
// SageMaker Execution Role for admins
const sagemakerAdminExecutionRole = new iam.Role(this, "sagemaker-mlflow-admin-role", {
assumedBy: new iam.CompositePrincipal(
new iam.ServicePrincipal("sagemaker.amazonaws.com")
),
managedPolicies: [
iam.ManagedPolicy.fromAwsManagedPolicyName("AmazonSageMakerFullAccess")
],
inlinePolicies: {
restApiAdmin: restApiAdminPolicy,
s3Buckets: s3bucketPolicy,
ssmPolicy: ssmPolicy
},
});
// SageMaker Execution Role for readers
const sagemakerReadersExecutionRole = new iam.Role(this, "sagemaker-mlflow-reader-role", {
assumedBy: new iam.ServicePrincipal("sagemaker.amazonaws.com"),
managedPolicies: [
iam.ManagedPolicy.fromAwsManagedPolicyName("AmazonSageMakerFullAccess")
],
inlinePolicies: {
restApiReader: restApiReaderPolicy,
s3Buckets: s3bucketPolicy,
ssmPolicy: ssmPolicy
},
});
// SageMaker Execution Role for readers
const sagemakerModelAproverExecutionRole = new iam.Role(this, "sagemaker-mlflow-model-aprover-role", {
assumedBy: new iam.ServicePrincipal("sagemaker.amazonaws.com"),
managedPolicies: [
iam.ManagedPolicy.fromAwsManagedPolicyName("AmazonSageMakerFullAccess")
],
inlinePolicies: {
restApiReader: restApiModelApprover,
s3Buckets: s3bucketPolicy,
ssmPolicy: ssmPolicy
},
});
if (domainId == "") {
// Create a domain
const defaultVpc = ec2.Vpc.fromLookup(this, 'DefaultVPC', { isDefault: true });
const subnetIds: string[] = [];
defaultVpc.publicSubnets.forEach((subnet, index) => {
subnetIds.push(subnet.subnetId);
});
const cfnStudioDomain = new sagemaker.CfnDomain(this, 'MyStudioDomain', {
authMode: 'IAM',
defaultUserSettings: {
executionRole: sagemakerAdminExecutionRole.roleArn
},
domainName: 'StudioDomainName',
vpcId: defaultVpc.vpcId,
subnetIds: subnetIds,
});
this.sagemakerStudioDomainId = cfnStudioDomain.attrDomainId
}
else {
this.sagemakerStudioDomainId = domainId
}
const cfnAdminProfile = new sagemaker.CfnUserProfile(this, 'MyCfnAdminProfile', {
domainId: this.sagemakerStudioDomainId,
userProfileName: 'mlflow-admin',
userSettings: {
executionRole: sagemakerAdminExecutionRole.roleArn,
}
}
);
const cfnReaderProfile = new sagemaker.CfnUserProfile(this, 'MyCfnReaderProfile', {
domainId: this.sagemakerStudioDomainId,
userProfileName: 'mlflow-reader',
userSettings: {
executionRole: sagemakerReadersExecutionRole.roleArn,
}
}
);
const cfnModelApproverProfile = new sagemaker.CfnUserProfile(this, 'MyCfnModelApproverProfile', {
domainId: this.sagemakerStudioDomainId,
userProfileName: 'mlflow-model-approver',
userSettings: {
executionRole: sagemakerModelAproverExecutionRole.roleArn,
}
}
);
const cfnAdminJupyterApp = new sagemaker.CfnApp(this, 'MyCfnAdminJupyterApp', {
appName: 'default',
appType: 'JupyterServer',
domainId: this.sagemakerStudioDomainId,
userProfileName: cfnAdminProfile.userProfileName
})
const cfnAdminKernelApp = new sagemaker.CfnApp(this, 'MyCfnAdminKernelApp', {
appName: 'instance-mlflow-basepython-2-0-ml-t3-medium',
appType: 'KernelGateway',
domainId: this.sagemakerStudioDomainId,
userProfileName: cfnAdminProfile.userProfileName,
resourceSpec: {
instanceType: 'ml.t3.medium',
sageMakerImageArn: `arn:aws:sagemaker:${this.region}:${sageMakerImageArnMapping[this.region]}:image/sagemaker-base-python-38`,
}
})
cfnAdminJupyterApp.addDependency(cfnAdminProfile)
cfnAdminKernelApp.addDependency(cfnAdminProfile)
const cfnReaderJupyterApp = new sagemaker.CfnApp(this, 'MyCfnReaderJupyterApp', {
appName: 'default',
appType: 'JupyterServer',
domainId: this.sagemakerStudioDomainId,
userProfileName: cfnReaderProfile.userProfileName
})
const cfnReaderKernelApp = new sagemaker.CfnApp(this, 'MyCfnReaderKernelApp', {
appName: 'instance-mlflow-basepython-2-0-ml-t3-medium',
appType: 'KernelGateway',
domainId: this.sagemakerStudioDomainId,
userProfileName: cfnReaderProfile.userProfileName,
resourceSpec: {
instanceType: 'ml.t3.medium',
sageMakerImageArn: `arn:aws:sagemaker:${this.region}:${sageMakerImageArnMapping[this.region]}:image/sagemaker-base-python-38`,
}
})
cfnReaderJupyterApp.addDependency(cfnReaderProfile)
cfnReaderKernelApp.addDependency(cfnReaderProfile)
const cfnModelApproverJupyterApp = new sagemaker.CfnApp(this, 'MyCfnModelApproverJupyterApp', {
appName: 'default',
appType: 'JupyterServer',
domainId: this.sagemakerStudioDomainId,
userProfileName: cfnModelApproverProfile.userProfileName
})
const cfnModelApproverKernelApp = new sagemaker.CfnApp(this, 'MyCfnModelApproverKernelApp', {
appName: 'instance-mlflow-basepython-2-0-ml-t3-medium',
appType: 'KernelGateway',
domainId: this.sagemakerStudioDomainId,
userProfileName: cfnModelApproverProfile.userProfileName,
resourceSpec: {
instanceType: 'ml.t3.medium',
sageMakerImageArn: `arn:aws:sagemaker:${this.region}:${sageMakerImageArnMapping[this.region]}:image/sagemaker-base-python-38`,
}
})
cfnModelApproverJupyterApp.addDependency(cfnModelApproverProfile)
cfnModelApproverKernelApp.addDependency(cfnModelApproverProfile)
const nagIamSuprressionSMExecutionRole = [
{
id: 'AwsSolutions-IAM4',
reason: "Domain users require full access and the managed policy is likely better than '*'"
},
{
id: 'AwsSolutions-IAM5',
reason: 'Group exceptions for API Gateway invoke permissions necessary to demonstrate model approver permission on MLflow',
},
]
NagSuppressions.addResourceSuppressions(sagemakerAdminExecutionRole, nagIamSuprressionSMExecutionRole, true)
NagSuppressions.addResourceSuppressions(sagemakerReadersExecutionRole, nagIamSuprressionSMExecutionRole, true)
NagSuppressions.addResourceSuppressions(sagemakerModelAproverExecutionRole, nagIamSuprressionSMExecutionRole, true)
}
}