forked from aws-samples/amazon-sagemaker-secure-mlops
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject-model-deploy.yaml
More file actions
470 lines (447 loc) · 18.9 KB
/
Copy pathproject-model-deploy.yaml
File metadata and controls
470 lines (447 loc) · 18.9 KB
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
460
461
462
463
464
465
466
467
468
469
470
AWSTemplateFormatVersion: 2010-09-09
Description: |
MLOps SageMaker Project for multi-account ML model deployment.
This template creates a CI/CD pipeline to deploy a given inference image and pretrained Model to two separate account -- staging and production.
Parameters:
SageMakerProjectName:
Type: String
Description: Name of the project
MinLength: 1
MaxLength: 32
AllowedPattern: ^[a-zA-Z](-*[a-zA-Z0-9])*
SageMakerProjectId:
Type: String
Description: Service generated Id of the project.
ModelPackageGroupName:
Type: String
Description: Model package group name to monitor for model package state changes. Leave 'Auto' to auto generate.
Default: 'Auto'
MultiAccountDeployment:
Type: String
Description: Enable multi-account deployment of the model. The staging and production OU Ids must be set in the environment.
AllowedValues:
- 'YES'
- 'NO'
Default: 'NO'
Conditions:
MLOpsArtifactBucketCondition: !Equals [ 'true', 'true' ]
GenerateModelPackageNameCondition: !Equals [ !Ref ModelPackageGroupName, 'Auto' ]
MultiAccountDeploymentCondition: !Equals [ !Ref MultiAccountDeployment, 'YES' ]
Resources:
# Retrieve the environment variables
GetEnvironmentConfiguration:
Type: Custom::GetEnvironmentConfiguration
Properties:
ServiceToken: !ImportValue 'ds-get-environment-configuration-lambda-arn'
SageMakerProjectName: !Ref SageMakerProjectName
SSMParams:
-
VariableName: 'DataBucketName'
ParameterName: 'data-bucket-name'
-
VariableName: 'ModelBucketName'
ParameterName: 'model-bucket-name'
-
VariableName: 'S3VPCEId'
ParameterName: 's3-vpce-id'
-
VariableName: 'S3KmsKeyId'
ParameterName: 'kms-s3-key-arn'
-
VariableName: 'PipelineExecutionRole'
ParameterName: 'sm-pipeline-execution-role-arn'
-
VariableName: 'OUStagingId'
ParameterName: 'ou-staging-id'
-
VariableName: 'OUProdId'
ParameterName: 'ou-prod-id'
-
VariableName: 'ProdAccountList'
ParameterName: 'production-account-list'
-
VariableName: 'StagingAccountList'
ParameterName: 'staging-account-list'
-
VariableName: 'ModelExecutionRole'
ParameterName: 'sm-model-execution-role-name'
-
VariableName: 'StackSetExecutionRole'
ParameterName: 'stackset-execution-role-name'
-
VariableName: 'StackSetAdministrationRole'
ParameterName: 'stackset-administration-role-arn'
-
VariableName: 'EbsKmsKeyArn'
ParameterName: 'kms-ebs-key-arn'
-
VariableName: 'EnvTypeStagingName'
ParameterName: 'env-type-staging-name'
-
VariableName: 'EnvTypeProdName'
ParameterName: 'env-type-prod-name'
-
VariableName: 'SeedCodeS3BucketName'
ParameterName: 'seed-code-s3bucket-name'
MlOpsArtifactsBucket:
Type: AWS::S3::Bucket
Condition: MLOpsArtifactBucketCondition
DeletionPolicy: Retain
UpdateReplacePolicy: Retain
Properties:
BucketName: !Sub sm-mlops-cp-${SageMakerProjectName}-${SageMakerProjectId} # 12+32+15=59 chars max/ 63 allowed
AccessControl: Private
PublicAccessBlockConfiguration:
BlockPublicAcls: TRUE
BlockPublicPolicy: TRUE
IgnorePublicAcls: TRUE
RestrictPublicBuckets: TRUE
BucketEncryption:
ServerSideEncryptionConfiguration:
- ServerSideEncryptionByDefault:
SSEAlgorithm: 'aws:kms'
KMSMasterKeyID: !GetAtt GetEnvironmentConfiguration.S3KmsKeyId
Tags:
- Key: SageMakerProjectName
Value: !Ref SageMakerProjectName
- Key: SageMakerProjectId
Value: !Ref SageMakerProjectId
- Key: EnvironmentName
Value: !GetAtt GetEnvironmentConfiguration.EnvironmentName
- Key: EnvironmentType
Value: !GetAtt GetEnvironmentConfiguration.EnvironmentType
ModelDeployRegistryEventRule:
Type: AWS::Events::Rule
Properties:
# Max length allowed: 64
Name: !Sub sagemaker-${SageMakerProjectName}-${SageMakerProjectId}-model # max: 10+33+15+5=63 chars
Description: "Rule to trigger a deployment when SageMaker Model registry is updated with a new model package. For example, a new model package is registered with Registry"
EventPattern:
source:
- "aws.sagemaker"
detail-type:
- "SageMaker Model Package State Change"
detail:
ModelPackageGroupName:
- !If
- GenerateModelPackageNameCondition
- !Sub '${SageMakerProjectName}-${SageMakerProjectId}'
- !Ref ModelPackageGroupName
State: "ENABLED"
Targets:
-
Arn:
!Sub 'arn:${AWS::Partition}:codepipeline:${AWS::Region}:${AWS::AccountId}:${ModelDeployPipeline}'
RoleArn:
!Sub 'arn:${AWS::Partition}:iam::${AWS::AccountId}:role/service-role/AmazonSageMakerServiceCatalogProductsUseRole'
Id: !Sub sagemaker-${SageMakerProjectName}-trigger
ModelDeployCodeCommitEventRule:
Type: AWS::Events::Rule
Properties:
# Max length allowed: 64
Name: !Sub sagemaker-${SageMakerProjectName}-${SageMakerProjectId}-code # max: 10+33+15+5=63 chars
Description: "Rule to launch a pipeline run when ModelDeploy CodeCommit repository is updated"
EventPattern:
source:
- "aws.codecommit"
detail-type:
- "CodeCommit Repository State Change"
resources:
- !GetAtt ModelDeployCodeCommitRepository.Arn
detail:
referenceType:
- "branch"
referenceName:
- "main"
State: "ENABLED"
Targets:
-
Arn:
!Sub 'arn:${AWS::Partition}:codepipeline:${AWS::Region}:${AWS::AccountId}:${ModelDeployPipeline}'
RoleArn:
!Sub 'arn:${AWS::Partition}:iam::${AWS::AccountId}:role/service-role/AmazonSageMakerServiceCatalogProductsUseRole'
Id: !Sub codecommit-${SageMakerProjectName}-modelbuild
ModelDeployCodeCommitRepository:
Type: AWS::CodeCommit::Repository
Properties:
# Max allowed length: 100 chars
RepositoryName: !Sub sagemaker-${SageMakerProjectName}-${SageMakerProjectId}-model-deploy # max: 10+33+15+12=70
RepositoryDescription: !Sub SageMaker Endpoint deployment infrastructure as code for the project ${SageMakerProjectName}
Code:
S3:
Bucket: !GetAtt GetEnvironmentConfiguration.SeedCodeS3BucketName
Key: sagemaker-mlops/seed-code/mlops-model-deploy-v1.0.zip
BranchName: main
Tags:
- Key: SageMakerProjectName
Value: !Ref SageMakerProjectName
- Key: SageMakerProjectId
Value: !Ref SageMakerProjectId
- Key: EnvironmentName
Value: !GetAtt GetEnvironmentConfiguration.EnvironmentName
- Key: EnvironmentType
Value: !GetAtt GetEnvironmentConfiguration.EnvironmentType
ModelDeployBuildProject:
Type: AWS::CodeBuild::Project
Properties:
# Max length: 255 chars
Name: !Sub sagemaker-${SageMakerProjectName}-${SageMakerProjectId}-modeldeploy # max: 10+33+15+10=68
Description: Pulls the code from Model Deploy CodeCommit repository, builds the CloudFormation templates with the endpoint and deploys them
ServiceRole: !Sub 'arn:${AWS::Partition}:iam::${AWS::AccountId}:role/service-role/AmazonSageMakerServiceCatalogProductsUseRole'
Artifacts:
Type: CODEPIPELINE
Environment:
Type: LINUX_CONTAINER
ComputeType: BUILD_GENERAL1_SMALL
Image: aws/codebuild/amazonlinux2-x86_64-standard:3.0
EnvironmentVariables:
- Name: SAGEMAKER_PROJECT_NAME
Value: !Ref SageMakerProjectName
- Name: SAGEMAKER_PROJECT_ID
Value: !Ref SageMakerProjectId
- Name: ENV_NAME
Value: !GetAtt GetEnvironmentConfiguration.EnvironmentName
- Name: ENV_TYPE
Value: !GetAtt GetEnvironmentConfiguration.EnvironmentType
- Name: ARTIFACT_BUCKET
Value: !If
- MLOpsArtifactBucketCondition
- !Ref MlOpsArtifactsBucket
- !GetAtt GetEnvironmentConfiguration.DataBucketName
- Name: DATA_BUCKET
Value: !GetAtt GetEnvironmentConfiguration.DataBucketName
- Name: MODEL_BUCKET
Value: !GetAtt GetEnvironmentConfiguration.ModelBucketName
- Name: SOURCE_MODEL_PACKAGE_GROUP_NAME
Value: !If
- GenerateModelPackageNameCondition
- !Sub '${SageMakerProjectName}-${SageMakerProjectId}'
- !Ref ModelPackageGroupName
- Name: AWS_REGION
Value: !Ref AWS::Region
- Name: MULTI_ACCOUNT_DEPLOYMENT
Value: !Ref MultiAccountDeployment
- Name: STAGING_ACCOUNT_LIST
Value: !If
- MultiAccountDeploymentCondition
- !GetAtt GetEnvironmentConfiguration.StagingAccountList
- !Ref 'AWS::AccountId'
- Name: PROD_ACCOUNT_LIST
Value: !If
- MultiAccountDeploymentCondition
- !GetAtt GetEnvironmentConfiguration.ProdAccountList
- !Ref 'AWS::AccountId'
- Name: STAGING_CONFIG_NAME
Value: 'staging-config'
- Name: PROD_CONFIG_NAME
Value: 'prod-config'
- Name: CFN_TEMPLATE_NAME
Value: 'cfn-sm-endpoint'
- Name: SAGEMAKER_EXECUTION_ROLE_STAGING_NAME
Value: !GetAtt GetEnvironmentConfiguration.ModelExecutionRole
- Name: SAGEMAKER_EXECUTION_ROLE_PROD_NAME
Value: !GetAtt GetEnvironmentConfiguration.ModelExecutionRole
- Name: SAGEMAKER_EBS_KMS_KEY_ARN
Value: !GetAtt GetEnvironmentConfiguration.EbsKmsKeyArn
- Name: ENV_TYPE_STAGING_NAME
Value: !GetAtt GetEnvironmentConfiguration.EnvTypeStagingName
- Name: ENV_TYPE_PROD_NAME
Value: !GetAtt GetEnvironmentConfiguration.EnvTypeProdName
Source:
Type: CODEPIPELINE
BuildSpec: buildspec.yml
TimeoutInMinutes: 15
VpcConfig:
SecurityGroupIds: !GetAtt GetEnvironmentConfiguration.SecurityGroups
Subnets: !GetAtt GetEnvironmentConfiguration.SubnetIds
VpcId: !GetAtt GetEnvironmentConfiguration.VpcId
Tags:
- Key: SageMakerProjectName
Value: !Ref SageMakerProjectName
- Key: SageMakerProjectId
Value: !Ref SageMakerProjectId
- Key: EnvironmentName
Value: !GetAtt GetEnvironmentConfiguration.EnvironmentName
- Key: EnvironmentType
Value: !GetAtt GetEnvironmentConfiguration.EnvironmentType
ModelDeployTestProject:
Type: AWS::CodeBuild::Project
Properties:
# Max length: 255 chars
Name: !Sub sagemaker-${SageMakerProjectName}-${SageMakerProjectId}-modeldeploy-test # max: 10+33+15+7=65
Description: Test the deployment endpoint in the staging account
ServiceRole: !Sub 'arn:${AWS::Partition}:iam::${AWS::AccountId}:role/service-role/AmazonSageMakerServiceCatalogProductsUseRole'
Artifacts:
Type: CODEPIPELINE
Environment:
Type: LINUX_CONTAINER
ComputeType: BUILD_GENERAL1_SMALL
Image: "aws/codebuild/amazonlinux2-x86_64-standard:3.0"
EnvironmentVariables:
- Name: AWS_REGION
Value: !Ref "AWS::Region"
- Name: BUILD_CONFIG
Value: 'staging-config'
- Name: TEST_RESULTS
Value: 'test-results'
Source:
Type: CODEPIPELINE
BuildSpec: test/buildspec.yml
TimeoutInMinutes: 30
VpcConfig:
SecurityGroupIds: !GetAtt GetEnvironmentConfiguration.SecurityGroups
Subnets: !GetAtt GetEnvironmentConfiguration.SubnetIds
VpcId: !GetAtt GetEnvironmentConfiguration.VpcId
Tags:
- Key: SageMakerProjectName
Value: !Ref SageMakerProjectName
- Key: SageMakerProjectId
Value: !Ref SageMakerProjectId
- Key: EnvironmentName
Value: !GetAtt GetEnvironmentConfiguration.EnvironmentName
- Key: EnvironmentType
Value: !GetAtt GetEnvironmentConfiguration.EnvironmentType
ModelDeployPipeline:
Type: AWS::CodePipeline::Pipeline
Properties:
# Max length: 100 chars
Name: !Sub sagemaker-${SageMakerProjectName}-${SageMakerProjectId}-modeldeploy # max: 10+33+15+11=69
RoleArn: !Sub 'arn:${AWS::Partition}:iam::${AWS::AccountId}:role/service-role/AmazonSageMakerServiceCatalogProductsUseRole'
ArtifactStore:
Type: S3
Location: !If
- MLOpsArtifactBucketCondition
- !Ref MlOpsArtifactsBucket
- !GetAtt GetEnvironmentConfiguration.DataBucketName
Tags:
- Key: SageMakerProjectName
Value: !Ref SageMakerProjectName
- Key: SageMakerProjectId
Value: !Ref SageMakerProjectId
- Key: EnvironmentName
Value: !GetAtt GetEnvironmentConfiguration.EnvironmentName
- Key: EnvironmentType
Value: !GetAtt GetEnvironmentConfiguration.EnvironmentType
Stages:
- Name: Source
Actions:
- Name: ModelDeploySource
ActionTypeId:
Category: Source
Owner: AWS
Provider: CodeCommit
Version: '1'
Configuration:
PollForSourceChanges: false
RepositoryName: !GetAtt ModelDeployCodeCommitRepository.Name
BranchName: main
OutputArtifacts:
- Name: ModelDeploySourceArtifact
# Stage: Build CFN Template for model deployment
- Name: Build
Actions:
- Name: BuildEndpointDeploymentTemplate
ActionTypeId:
Category: Build
Owner: AWS
Provider: CodeBuild
Version: '1'
InputArtifacts:
- Name: ModelDeploySourceArtifact
OutputArtifacts:
- Name: BuildArtifact
Configuration:
ProjectName: !Ref ModelDeployBuildProject
RunOrder: 1
# Stage: Deploy and test into the staging accounts
- Name: DeployModelStaging
Actions:
# Action 1
- Name: DeployStaging
InputArtifacts:
- Name: BuildArtifact
ActionTypeId:
Category: Deploy
Owner: AWS
Version: '1'
Provider: CloudFormationStackSet
Configuration:
StackSetName: !Sub 'sagemaker-${SageMakerProjectName}-${SageMakerProjectId}-deploy-${GetEnvironmentConfiguration.EnvTypeStagingName}' #10+33+15+14=72 out of 128 max
Description: 'SageMaker endpoint in the staging target OU'
TemplatePath: BuildArtifact::cfn-sm-endpoint.yaml
Parameters: BuildArtifact::staging-config.json
Capabilities: CAPABILITY_NAMED_IAM
PermissionModel: 'SELF_MANAGED'
AdministrationRoleArn: !GetAtt GetEnvironmentConfiguration.StackSetAdministrationRole
ExecutionRoleName: !GetAtt GetEnvironmentConfiguration.StackSetExecutionRole
# For a self-managed model, targets can only be AWS accounts
DeploymentTargets: !If
- MultiAccountDeploymentCondition
- !GetAtt GetEnvironmentConfiguration.StagingAccountList
- !Ref 'AWS::AccountId'
Regions: !Ref 'AWS::Region'
RunOrder: 1
# Action 2
- Name: TestStaging
ActionTypeId:
Category: Build
Owner: AWS
Provider: CodeBuild
Version: '1'
InputArtifacts:
- Name: ModelDeploySourceArtifact
- Name: BuildArtifact
OutputArtifacts:
- Name: TestArtifact
Configuration:
ProjectName: !Ref ModelDeployTestProject
PrimarySource: ModelDeploySourceArtifact
RunOrder: 2
# Action 3
- Name: ApproveStagingDeployment
ActionTypeId:
Category: Approval
Owner: AWS
Version: '1'
Provider: Manual
Configuration:
CustomData: !Sub
- "Model ${ModelPackageGroupName} for the project ${SageMakerProjectName} is deployed to the staging accounts ${AccountList}"
- ModelPackageGroupName: !If
- GenerateModelPackageNameCondition
- !Sub '${SageMakerProjectName}-${SageMakerProjectId}'
- !Ref ModelPackageGroupName
SageMakerProjectName: !Ref SageMakerProjectName
AccountList: !If
- MultiAccountDeploymentCondition
- !GetAtt GetEnvironmentConfiguration.StagingAccountList
- !Ref 'AWS::AccountId'
ExternalEntityLink: !Sub 'https://${AWS::Region}.console.aws.amazon.com/codesuite/codepipeline/pipelines/sagemaker-${SageMakerProjectName}-${SageMakerProjectId}-modeldeploy/view?region=${AWS::Region}'
RunOrder: 3
# Stage: Deploy into the production accounts
- Name: DeployModelProd
Actions:
- Name: DeployProd
InputArtifacts:
- Name: BuildArtifact
ActionTypeId:
Category: Deploy
Owner: AWS
Version: '1'
Provider: CloudFormationStackSet
Configuration:
StackSetName: !Sub 'sagemaker-${SageMakerProjectName}-${SageMakerProjectId}-deploy-${GetEnvironmentConfiguration.EnvTypeProdName}' #10+33+15+14=72 out of 128 max
Description: 'SageMaker endpoint in the production target OU'
Parameters: BuildArtifact::prod-config.json
TemplatePath: BuildArtifact::cfn-sm-endpoint.yaml
Capabilities: CAPABILITY_NAMED_IAM
PermissionModel: 'SELF_MANAGED'
AdministrationRoleArn: !GetAtt GetEnvironmentConfiguration.StackSetAdministrationRole
ExecutionRoleName: !GetAtt GetEnvironmentConfiguration.StackSetExecutionRole
# For a self-managed model, targets can only be AWS accounts
DeploymentTargets: !If
- MultiAccountDeploymentCondition
- !GetAtt GetEnvironmentConfiguration.ProdAccountList
- !Ref 'AWS::AccountId'
Regions: !Ref 'AWS::Region'
RunOrder: 1