forked from aws-samples/sagemaker-mlops-with-tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CF-MLOps-TensorFlow-Pipeline.yml
649 lines (630 loc) · 19.6 KB
/
CF-MLOps-TensorFlow-Pipeline.yml
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
AWSTemplateFormatVersion: "2010-09-09"
Description: MLOps-TensorFlow-CICD-ipeline
Parameters:
UniqueID:
Type: String
Default: yourinitials
Description: Enter your initials in lower case as a unique identifier for components created
AllowedPattern: "^([a-z]|(d(?!d{0,2}.d{1,3}.d{1,3}.d{1,3})))([a-zd]|(.(?!(.|-)))|(-(?!.))){1,61}[a-zd.]$"
RepositoryBranch:
Type: String
Default: master
Description: Branch to trigger CodePipeline on commit. For this lab, you can leave this as master.
LambdaSeedBucket:
Type: String
Default: mlops-tf-lambda-code-yourinitials-uniqueid
Description: S3 Bucket containing your Lambda Functions
Resources:
CodePipelineArtifactBucket:
Type: AWS::S3::Bucket
Properties:
AccessControl: BucketOwnerFullControl
VersioningConfiguration:
Status: Enabled
BucketName:
Fn::Join:
- ""
- - "mlops-tf-codepipeline-artifacts-"
- !Ref UniqueID
- "-"
- !Select
- 0
- !Split
- "-"
- !Select
- 2
- !Split
- "/"
- !Ref "AWS::StackId"
# ------------------------------------------------------------------------------
# Create S3 bucket to store model artifacts created by after successfully
# completed training of the model.
# ------------------------------------------------------------------------------
ModelArtifactBucket:
Type: AWS::S3::Bucket
Properties:
AccessControl: BucketOwnerFullControl
VersioningConfiguration:
Status: Enabled
BucketName:
Fn::Join:
- ""
- - "mlops-tf-model-artifacts-"
- !Ref UniqueID
- "-"
- !Select
- 0
- !Split
- "-"
- !Select
- 2
- !Split
- "/"
- !Ref "AWS::StackId"
S3DataBucket:
Type: AWS::S3::Bucket
Properties:
AccessControl: BucketOwnerFullControl
VersioningConfiguration:
Status: Enabled
BucketName:
Fn::Join:
- ""
- - "mlops-tf-data-"
- !Ref UniqueID
- "-"
- !Select
- 0
- !Split
- "-"
- !Select
- 2
- !Split
- "/"
- !Ref "AWS::StackId"
CodeCommitRepo:
Type: AWS::CodeCommit::Repository
Properties:
RepositoryName:
Fn::Join:
- ""
- - "mlops-codecommit-tf-"
- !Ref UniqueID
- "-"
- !Select
- 0
- !Split
- "-"
- !Select
- 2
- !Split
- "/"
- !Ref "AWS::StackId"
RepositoryDescription: "MLOps tf Code Commit Repository"
Code:
S3:
Bucket: !Ref LambdaSeedBucket
Key: seedcode.zip
BranchName: !Ref RepositoryBranch
CodePipeline:
Type: AWS::CodePipeline::Pipeline
Properties:
RoleArn: !GetAtt [CodePipelineRole, Arn]
ArtifactStore:
Location:
Ref:
CodePipelineArtifactBucket
Type: S3
Stages:
-
Name: Source
Actions:
-
Name: TrainScript
ActionTypeId:
Category: Source
Owner: AWS
Version: 1
Provider: CodeCommit
OutputArtifacts:
- Name: CodeArtifact
Configuration:
BranchName:
Ref: RepositoryBranch
RepositoryName: !GetAtt [CodeCommitRepo, Name]
RunOrder: 1
-
Name: Build
Actions:
-
Name: BuildTrainScript
ActionTypeId:
Category: Build
Owner: AWS
Version: 1
Provider: CodeBuild
OutputArtifacts:
- Name: TrainPyArtifact
InputArtifacts:
- Name: CodeArtifact
Configuration:
ProjectName: !Ref TrainCodeBuild
RunOrder: 1
-
Name: TrainModel
Actions:
-
Name: PrepareData
ActionTypeId:
Category: Invoke
Owner: AWS
Version: 1
Provider: Lambda
OutputArtifacts:
- Name: prepareInfo
InputArtifacts:
- Name: CodeArtifact
Configuration:
FunctionName:
Fn::Join:
- ""
- - "MLOps-tf-PrepareData-"
- !Ref UniqueID
UserParameters: ' { "stage": "Training"}} '
RunOrder: 1
-
Name: StartTraining
ActionTypeId:
Category: Invoke
Owner: AWS
Version: 1
Provider: Lambda
OutputArtifacts:
- Name: jobEventInfo
InputArtifacts:
- Name: CodeArtifact
Configuration:
FunctionName:
Fn::Join:
- ""
- - "MLOps-tf-TrainModel-"
- !Ref UniqueID
UserParameters: ' {"traincompute": "ml.p3.2xlarge" , "traininstancevolumesize": 10, "traininstancecount": 1} '
RunOrder: 2
-
Name: GetTrainingStatus
ActionTypeId:
Category: Invoke
Owner: AWS
Version: 1
Provider: Lambda
OutputArtifacts:
- Name: CompletedTraining
InputArtifacts:
- Name: jobEventInfo
Configuration:
FunctionName:
Fn::Join:
- ""
- - "MLOps-tf-GetStatus-"
- !Ref UniqueID
UserParameters: ' { "stage": "Training"} '
RunOrder: 3
-
Name: DeployModel-Dev
Actions:
-
Name: DeployToDevEnv
ActionTypeId:
Category: Invoke
Owner: AWS
Version: 1
Provider: Lambda
OutputArtifacts:
- Name: HostingInfo
InputArtifacts:
- Name: CompletedTraining
Configuration:
FunctionName:
Fn::Join:
- ""
- - "MLOps-tf-DeployModel-"
- !Ref UniqueID
UserParameters: '{ "InitialInstanceCount": 1, "InitialVariantWeight": 1, "InstanceType": "ml.p2.xlarge", "EndpointConfigName": "Dev" }'
RunOrder: 1
-
Name: GetDeployStatus
ActionTypeId:
Category: Invoke
Owner: AWS
Version: 1
Provider: Lambda
OutputArtifacts:
- Name: CompletedDevDeploy
InputArtifacts:
- Name: HostingInfo
Configuration:
FunctionName:
Fn::Join:
- ""
- - "MLOps-tf-GetStatus-"
- !Ref UniqueID
UserParameters: ' { "stage": "Deployment"} '
RunOrder: 2
-
Name: DevTest
ActionTypeId:
Category: Invoke
Owner: AWS
Version: 1
Provider: Lambda
OutputArtifacts:
- Name: DevTestOut
InputArtifacts:
- Name: HostingInfo
Configuration:
FunctionName:
Fn::Join:
- ""
- - "MLOps-tf-EvaluateModel-"
- !Ref UniqueID
UserParameters: ' { "env": "Dev" } '
RunOrder: 3
-
Name: DeployModel-Test
Actions:
-
Name: DeployToTestEnv
ActionTypeId:
Category: Invoke
Owner: AWS
Version: 1
Provider: Lambda
OutputArtifacts:
- Name: HostingInfoTest
InputArtifacts:
- Name: CompletedTraining
Configuration:
FunctionName:
Fn::Join:
- ""
- - "MLOps-tf-DeployModel-"
- !Ref UniqueID
UserParameters: '{ "InitialInstanceCount": 1, "InitialVariantWeight": 1, "InstanceType": "ml.p2.xlarge", "EndpointConfigName": "Test" }'
RunOrder: 1
-
Name: GetDeployStatus
ActionTypeId:
Category: Invoke
Owner: AWS
Version: 1
Provider: Lambda
OutputArtifacts:
- Name: CompletedTestDeploy
InputArtifacts:
- Name: HostingInfoTest
Configuration:
FunctionName:
Fn::Join:
- ""
- - "MLOps-tf-GetStatus-"
- !Ref UniqueID
UserParameters: ' { "stage": "Deployment"} '
RunOrder: 2
-
Name: FullTest
ActionTypeId:
Category: Invoke
Owner: AWS
Version: 1
Provider: Lambda
OutputArtifacts:
- Name: FullTestOut
InputArtifacts:
- Name: HostingInfoTest
Configuration:
FunctionName:
Fn::Join:
- ""
- - "MLOps-tf-EvaluateModel-"
- !Ref UniqueID
UserParameters: '{ "env": "Test" } '
RunOrder: 3
-
Name: DeployModel-Prod
Actions:
-
Name: DeployToProdEnv
ActionTypeId:
Category: Invoke
Owner: AWS
Version: 1
Provider: Lambda
OutputArtifacts:
- Name: HostingInfoProd
InputArtifacts:
- Name: CompletedTraining
Configuration:
FunctionName:
Fn::Join:
- ""
- - "MLOps-tf-DeployModel-"
- !Ref UniqueID
UserParameters: '{ "InitialInstanceCount": 1, "InitialVariantWeight": 1, "InstanceType": "ml.p2.xlarge", "EndpointConfigName": "Prod" }'
RunOrder: 1
-
Name: GetDeployStatus
ActionTypeId:
Category: Invoke
Owner: AWS
Version: 1
Provider: Lambda
OutputArtifacts:
- Name: CompletedProdDeploy
InputArtifacts:
- Name: HostingInfoProd
Configuration:
FunctionName:
Fn::Join:
- ""
- - "MLOps-tf-GetStatus-"
- !Ref UniqueID
UserParameters: ' { "stage": "Deployment"} '
RunOrder: 2
CodePipelineRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
Effect: Allow
Principal:
Service: codepipeline.amazonaws.com
Action: sts:AssumeRole
Path: /
ManagedPolicyArns:
- arn:aws:iam::aws:policy/AdministratorAccess
RoleName:
Fn::Join:
- ""
- - "MLOps-tf-CodeBuild-ServiceRole-"
- !Ref UniqueID
TrainCodeBuild:
Type: AWS::CodeBuild::Project
DependsOn: CodeBuildRole
Properties:
Artifacts:
Type: CODEPIPELINE
Environment:
ComputeType: BUILD_GENERAL1_SMALL
Image: aws/codebuild/amazonlinux2-x86_64-standard:3.0
Type: LINUX_CONTAINER
EnvironmentVariables:
- Name: AWS_DEFAULT_REGION
Value: !Ref AWS::Region
- Name: AWS_ACCOUNT_ID
Value: !Ref AWS::AccountId
- Name: Train_Code_Bucket
Value: !Ref S3DataBucket
Source:
Type: CODEPIPELINE
BuildSpec: code/buildspec.yml
TimeoutInMinutes: 10
Name: !Sub ${AWS::StackName}TrainCodeBuild
ServiceRole: !Ref CodeBuildRole
TrainLambdaFunction:
Type: "AWS::Lambda::Function"
Properties:
Handler: "MLOps-tf-TrainModel.lambda_handler"
Role: !GetAtt [LambdaExecutionRole, Arn]
Environment:
Variables:
SageMakerExecutionRole: !GetAtt [SageMakerRole, Arn]
SSEKMSKeyIdIn: !Ref KMSMasterKeyID
ModelArtifactBucket: !Ref ModelArtifactBucket
S3DataBucket: !Ref S3DataBucket
AlgoECR: "763104351884.dkr.ecr.us-east-1.amazonaws.com/tensorflow-training:2.5.0-gpu-py37-cu112-ubuntu18.04"
FunctionName:
Fn::Join:
- ""
- - "MLOps-tf-TrainModel-"
- !Ref UniqueID
Code:
S3Bucket: !Ref LambdaSeedBucket
S3Key: "MLOps-tf-TrainModel.py.zip"
Runtime: "python3.7"
Timeout: 900
MemorySize: 10240
DeployLambdaFunction:
Type: "AWS::Lambda::Function"
Properties:
Handler: "MLOps-tf-DeployModel.lambda_handler"
Role: !GetAtt [LambdaExecutionRole, Arn]
Environment:
Variables:
SageMakerExecutionRole: !GetAtt [SageMakerRole, Arn]
SSEKMSKeyIdIn: !Ref KMSMasterKeyID
S3DataBucket: !Ref S3DataBucket
InferenceIMG: "763104351884.dkr.ecr.us-east-1.amazonaws.com/tensorflow-inference:2.5.1-gpu-py37-cu112-ubuntu18.04"
FunctionName:
Fn::Join:
- ""
- - "MLOps-tf-DeployModel-"
- !Ref UniqueID
Code:
S3Bucket: !Ref LambdaSeedBucket
S3Key: "MLOps-tf-DeployModel.py.zip"
Runtime: "python3.7"
Timeout: 900
GetStatusLambdaFunction:
Type: "AWS::Lambda::Function"
Properties:
Handler: "MLOps-tf-GetStatus.lambda_handler"
Role: !GetAtt [LambdaExecutionRole, Arn]
Environment:
Variables:
SageMakerExecutionRole: !GetAtt [SageMakerRole, Arn]
SSEKMSKeyIdIn: !Ref KMSMasterKeyID
FunctionName:
Fn::Join:
- ""
- - "MLOps-tf-GetStatus-"
- !Ref UniqueID
Code:
S3Bucket: !Ref LambdaSeedBucket
S3Key: "MLOps-tf-GetStatus.py.zip"
Runtime: "python3.7"
Timeout: 900
EvaluateModelLambdaFunction:
Type: "AWS::Lambda::Function"
Properties:
Handler: "MLOps-tf-EvaluateModel.lambda_handler"
Role: !GetAtt [LambdaExecutionRole, Arn]
Environment:
Variables:
SageMakerExecutionRole: !GetAtt [SageMakerRole, Arn]
SSEKMSKeyIdIn: !Ref KMSMasterKeyID
S3DataBucket: !Ref S3DataBucket
FunctionName:
Fn::Join:
- ""
- - "MLOps-tf-EvaluateModel-"
- !Ref UniqueID
Code:
S3Bucket: !Ref LambdaSeedBucket
S3Key: "MLOps-tf-EvaluateModel.py.zip"
Runtime: "python3.7"
Timeout: 900
MemorySize: 5307
Layers:
- arn:aws:lambda:us-east-1:668099181075:layer:AWSLambda-Python37-SciPy1x:37
PandasLayer:
Type: AWS::Lambda::LayerVersion
Properties:
LayerName:
Fn::Join:
- ""
- - "MLOps-tf-pandas-layer-"
- !Ref UniqueID
Description: "Pandas, Numpy, and file processing packages for Python3.7"
Content:
S3Bucket: !Ref LambdaSeedBucket
S3Key: "pandas_layer.zip"
CompatibleRuntimes:
- python3.7
LicenseInfo: "MIT"
DataPreparationLambdaFunction:
Type: "AWS::Lambda::Function"
Properties:
Handler: "MLOps-tf-PrepareData.lambda_handler"
Role: !GetAtt [LambdaExecutionRole, Arn]
Environment:
Variables:
SageMakerExecutionRole: !GetAtt [SageMakerRole, Arn]
SSEKMSKeyIdIn: !Ref KMSMasterKeyID
S3DataBucket: !Ref S3DataBucket
FunctionName:
Fn::Join:
- ""
- - "MLOps-tf-PrepareData-"
- !Ref UniqueID
Code:
S3Bucket: !Ref LambdaSeedBucket
S3Key: "MLOps-tf-PrepareData.py.zip"
Runtime: "python3.7"
Timeout: 900
MemorySize: 5307
Layers:
- !Ref PandasLayer
CodeBuildRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
Effect: Allow
Principal:
Service: codebuild.amazonaws.com
Action: sts:AssumeRole
ManagedPolicyArns:
- arn:aws:iam::aws:policy/AdministratorAccess
RoleName:
Fn::Join:
- ""
- - "MLOps-CodeBuild-ServiceRole-"
- !Ref UniqueID
LambdaExecutionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: sts:AssumeRole
ManagedPolicyArns:
- arn:aws:iam::aws:policy/AmazonSageMakerFullAccess
- arn:aws:iam::aws:policy/AmazonS3FullAccess
- arn:aws:iam::aws:policy/CloudWatchFullAccess
- arn:aws:iam::aws:policy/AWSCodePipelineFullAccess
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
RoleName:
Fn::Join:
- ""
- - "MLOps-tf-Lambda-ServiceRole-"
- !Ref UniqueID
SageMakerRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
Effect: Allow
Principal:
Service: sagemaker.amazonaws.com
Action: sts:AssumeRole
ManagedPolicyArns:
- arn:aws:iam::aws:policy/AmazonSageMakerFullAccess
- arn:aws:iam::aws:policy/AmazonS3FullAccess
- arn:aws:iam::aws:policy/CloudWatchFullAccess
- arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryFullAccess
RoleName:
Fn::Join:
- ""
- - "MLOps-tf-SageMaker-ServiceRole"
- !Ref UniqueID
KMSMasterKeyID:
Type: AWS::KMS::Key
Properties:
Description: "An example CMK"
KeyPolicy:
Version: "2012-10-17"
Id: "key-default-1"
Statement:
- Sid: "Enable IAM User Permissions"
Effect: "Allow"
Principal:
AWS:
Fn::Join:
- ""
- - "arn:aws:iam::"
- !Ref AWS::AccountId
- ":root"
Action: "kms:*"
Resource: "*"
Outputs:
S3DataBucket:
Description: The ID of the S3 Bucket for model training and test data
Value: !Ref S3DataBucket
ModelArtifactBucket:
Description: S3 Bucket for storing model ArtifactStore
Value: !Ref ModelArtifactBucket
CodePipeline:
Description: Pipeline created inside the CodePipeline service-role
Value: !Ref CodePipeline
CodeCommitRepo:
Description: The ID of the CodeCommit Repository
Value: !Ref CodeCommitRepo