-
Notifications
You must be signed in to change notification settings - Fork 44
/
template.yml
162 lines (147 loc) · 5.75 KB
/
template.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
Description: Toolchain template which provides the resources needed to represent infrastructure as code. The template creates a CI/CD pipeline using GitHub Actions to build a model using a SageMaker Pipeline and deploy the resulting trained ML Model from Model Registry to two stages in CD -- staging and production.
Parameters:
SageMakerProjectName:
Type: String
Description: Name of the project
NoEcho: true
MinLength: 1
MaxLength: 32
AllowedPattern: ^[a-zA-Z](-*[a-zA-Z0-9])*
SageMakerProjectId:
Type: String
NoEcho: true
Description: Service generated ID of the project.
CodeRepositoryName:
Type: String
MaxLength: 1024
Description: Repository name of the Model Building, Training and Deployment in GitHub
GitHubRepositoryOwnerName:
Type: String
MaxLength: 1024
Description: GitHub Repository Owner Name
CodestarConnectionUniqueId:
Type: String
MaxLength: 1024
Description: Codestar connection unique identifier
GitHubTokenSecretName:
Type: String
MaxLength: 1024
Description: Name of GitHub Token in AWS Secret Manager. This is to call deploy github workflow.
GitHubWorkflowNameForDeployment:
Type: String
MaxLength: 1024
Description: GitHub workflow file name which runs the deployment steps.
Metadata:
AWS::CloudFormation::Interface:
ParameterGroups:
-
Label:
default: "Code Repository Info"
Parameters:
- GitHubRepositoryOwnerName
- CodeRepositoryName
- CodestarConnectionUniqueId
- GitHubTokenSecretName
- GitHubWorkflowNameForDeployment
ParameterLabels:
GitHubRepositoryOwnerName:
default: "GitHub Repository Owner Name (username or organization)"
CodeRepositoryName:
default: "GitHub Repository Name"
CodestarConnectionUniqueId:
default: "Codestar connection unique id"
GitHubTokenSecretName:
default: "Name of the secret in the Secrets Manager which stores GitHub token"
GitHubWorkflowNameForDeployment:
default: "GitHub workflow file for deployment. e.g. deploy.yml"
Resources:
MlOpsArtifactsBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Sub sagemaker-project-github-${SageMakerProjectId}-${AWS::Region}
GitHubWorkflowTriggerLambda:
Type: 'AWS::Lambda::Function'
Properties:
Description: To trigger the GitHub Workflow
Handler: lambda_function.lambda_handler
Runtime: python3.9
FunctionName: !Sub sagemaker-${SageMakerProjectId}-github-trigger
Timeout: 900
Role: !GetAtt GitHubWorkflowTriggerLambdaExecutionRole.Arn
Code:
S3Bucket: mlops-sagemaker-github-example
S3Key: lambda-github-workflow-trigger.zip
Layers:
- !Sub arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:layer:python39-github-arm64:1
Architectures:
- arm64
Environment:
Variables:
DeployRepoName: !Sub ${CodeRepositoryName}
GitHubWorkflowNameForDeployment: !Sub ${GitHubWorkflowNameForDeployment}
GitHubTokenSecretName: !Sub ${GitHubTokenSecretName}
Region: !Ref AWS::Region
GitHubWorkflowTriggerLambdaExecutionRole:
Type: AWS::IAM::Role
Properties:
RoleName: !Join ['-', ['SageMakerGithubWorkflowTriggerLambdaExecutionRole', !Select [4, !Split ['-', !Select [2, !Split ['/', !Ref 'AWS::StackId']]]]]]
Description: lambda function to trigger GitHub workflow for deploying sagemaker model
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action: 'sts:AssumeRole'
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
Policies:
- PolicyName: GitHubWorkflowTriggerExecutionPolicy
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- 'secretsmanager:GetSecretValue'
Resource:
- !Sub arn:aws:secretsmanager:${AWS::Region}:${AWS::AccountId}:secret:${GitHubTokenSecretName}*
ModelDeploySageMakerEventRule:
Type: AWS::Events::Rule
Properties:
Name: !Sub sagemaker-${SageMakerProjectName}-${SageMakerProjectId}-event-rule
Description: Rule to trigger a deployment when SageMaker Model is Approved.
EventPattern:
source:
- "aws.sagemaker"
detail-type:
- "SageMaker Model Package State Change"
detail:
ModelPackageGroupName:
- !Sub ${SageMakerProjectName}-${SageMakerProjectId}
ModelApprovalStatus:
- Approved
State: "ENABLED"
Targets:
-
Arn: !GetAtt GitHubWorkflowTriggerLambda.Arn
Id: !Sub sagemaker-${SageMakerProjectName}-trigger
PermissionForEventsToInvokeLambda:
Type: AWS::Lambda::Permission
Properties:
FunctionName: !GetAtt GitHubWorkflowTriggerLambda.Arn
Action: lambda:InvokeFunction
Principal: events.amazonaws.com
SourceArn: !GetAtt ModelDeploySageMakerEventRule.Arn
SagemakerCodeRepository:
Type: 'AWS::SageMaker::CodeRepository'
Properties:
CodeRepositoryName: !Sub ${CodeRepositoryName}-${SageMakerProjectId}
GitConfig:
Branch: main
RepositoryUrl: !Sub https://codestar-connections.${AWS::Region}.amazonaws.com/git-http/${AWS::AccountId}/${AWS::Region}/${CodestarConnectionUniqueId}/${GitHubRepositoryOwnerName}/${CodeRepositoryName}.git
Tags:
- Key: "sagemaker:project-id"
Value: !Sub ${SageMakerProjectId}
- Key: "sagemaker:project-name"
Value: !Sub ${SageMakerProjectName}