forked from aws-samples/amazon-sagemaker-secure-mlops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackage-cfn.yaml
174 lines (154 loc) · 6.12 KB
/
package-cfn.yaml
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
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
AWSTemplateFormatVersion: '2010-09-09'
Description: |
Package CloudFormation template into the provided S3 bucket
Parameters:
S3BucketName:
Description: Name of the existing S3 bucket where the CloudFormation templates will be packaged
Type: String
Outputs:
CoreMainStackS3Uri:
Description: S3 URI for core infrastructure stack
Value: !Sub 'https://s3.${AWS::Region}.amazonaws.com/${S3BucketName}/sagemaker-mlops/core-main.yaml'
EnvMainStackS3Uri:
Description: S3 URI for data science environment stack
Value: !Sub 'https://s3.${AWS::Region}.amazonaws.com/${S3BucketName}/sagemaker-mlops/env-main.yaml'
QuickStartStackS3Uri:
Description: S3 URI for quick start stack
Value: !Sub 'https://s3.${AWS::Region}.amazonaws.com/${S3BucketName}/sagemaker-mlops/data-science-environment-quickstart.yaml'
CoreMainStackDeployLink:
Description: Link to open CloudFormation with core infrastructure stack
Value: !Sub 'https://console.aws.amazon.com/cloudformation/home?region=${AWS::Region}#/stacks/new?templateURL=https://s3.${AWS::Region}.amazonaws.com/${S3BucketName}/sagemaker-mlops/core-main.yaml'
QuickStartStackDeployLink:
Description: Link to open CloudFormation with core infrastructure stack
Value: !Sub 'https://console.aws.amazon.com/cloudformation/home?region=${AWS::Region}#/stacks/new?templateURL=https://s3.${AWS::Region}.amazonaws.com/${S3BucketName}/sagemaker-mlops/data-science-environment-quickstart.yaml'
StartBuildCLICommand:
Description: CLI to start CodeBuild build
Value: !Sub 'aws codebuild start-build --project-name ${CfnTemplatePackageProject}'
Resources:
StartBuildLambdaExecutionRole:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- 'sts:AssumeRole'
Path: /
Policies:
- PolicyName: InlinePolicy
PolicyDocument:
Version: '2012-10-17'
Statement:
- Sid: CodeBuildPermission
Effect: Allow
Action:
- codebuild:StartBuild
Resource: !GetAtt CfnTemplatePackageProject.Arn
ManagedPolicyArns:
- 'arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole'
StartBuildLambda:
Type: AWS::Lambda::Function
Properties:
Code:
ZipFile: |
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
import json
import boto3
import cfnresponse
cb = boto3.client("codebuild")
def lambda_handler(event, context):
try:
response_status = cfnresponse.SUCCESS
if 'RequestType' in event and event['RequestType'] == 'Create':
cb.start_build(projectName=event['ResourceProperties']['ProjectName'])
cfnresponse.send(event, context, response_status, {}, '')
except Exception as e:
print(str(e))
cfnresponse.send(event, context, cfnresponse.FAILED, {}, physicalResourceId=event.get('PhysicalResourceId'), reason=str(e))
Description: Start CodeBuild project
Handler: index.lambda_handler
MemorySize: 128
Role: !GetAtt StartBuildLambdaExecutionRole.Arn
Runtime: python3.8
Timeout: 120
StartBuild:
Type: Custom::StartBuild
Properties:
ServiceToken: !GetAtt StartBuildLambda.Arn
ProjectName: !Ref CfnTemplatePackageProject
CodeBuildServiceRole:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service: codebuild.amazonaws.com
Action: 'sts:AssumeRole'
Path: '/service-role/'
Policies:
- PolicyName: CodeBuildServiceRoleInLinePolicy
PolicyDocument:
Version: 2012-10-17
Statement:
-
Action:
- 'logs:CreateLogGroup'
- 'logs:CreateLogStream'
- 'logs:PutLogEvents'
Resource: '*'
Effect: 'Allow'
-
Action:
- 's3:*Object'
- 's3:GetObjectVersion'
- 's3:GetBucketAcl'
- 's3:GetBucketLocation'
- 's3:ListBucket'
- 's3:PutObjectTagging'
- 's3:CreateBucket'
Resource:
- !Sub 'arn:aws:s3:::${S3BucketName}'
- !Sub 'arn:aws:s3:::${S3BucketName}/*'
Effect: 'Allow'
-
Action:
- 'codebuild:CreateReportGroup'
- 'codebuild:CreateReport'
- 'codebuild:UpdateReport'
- 'codebuild:BatchPutTestCases'
- 'codebuild:BatchPutCodeCoverages'
Resource: !Sub 'arn:aws:codebuild:${AWS::Region}:${AWS::AccountId}:report-group/*'
Effect: 'Allow'
CfnTemplatePackageProject:
Type: AWS::CodeBuild::Project
Properties:
Description: !Sub 'Packaging CFN templates into ${S3BucketName}'
ServiceRole: !GetAtt CodeBuildServiceRole.Arn
Artifacts:
Type: NO_ARTIFACTS
Environment:
Type: 'LINUX_CONTAINER'
ComputeType: 'BUILD_GENERAL1_SMALL'
Image: 'aws/codebuild/amazonlinux2-x86_64-standard:3.0'
EnvironmentVariables:
- Name: 'S3_BUCKET_NAME'
Value: !Ref S3BucketName
- Name: 'DEPLOYMENT_REGION'
Value: !Ref AWS::Region
Source:
Type: S3
Location: !Sub '${S3BucketName}/sagemaker-mlops/sagemaker-secure-mlops.zip'
BuildSpec: buildspec-package-cfn.yml
LogsConfig:
CloudWatchLogs:
Status: 'ENABLED'
TimeoutInMinutes: 5