-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathds_env_sagemaker_studio.yaml
312 lines (290 loc) · 10.8 KB
/
ds_env_sagemaker_studio.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
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
# Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
# The following template is designed to onboard SageMaker Studio Domain in VPCOnly secure environment for data science.
# The template depends on shared SageMaker Studio VPC and DataScience Administration role created by ds_administration.yaml.
# This template creates a KMS CMK, resources required for custom sagemaker image for tensorflow, a default data scientist role,
# and omboards SageMaker Studio Domain with default user settings.
Description: |
CloudFormation template for creating and onboarding SageMaker Studio
Parameters:
SharedServiceStackSetName:
Type: String
Description: Shared Service stack set name, common across data science stacks
StudioDomainName:
Description: Studio domain name
AllowedPattern: '^[a-zA-Z0-9](-*[a-zA-Z0-9]){0,62}'
Type: String
Default: ds-studio-domain
KMSAlias:
Type: String
Default: ds-sagemaker-studio-kms
CustomImageEcrUri:
Type: String
Description: Custom Images URI in ECR of format ACCOUNT_ID.dkr.ecr.REGION.amazonaws.com/ECR_REPOSITORY_NAME:IMAGE_NAME
CustomImageName:
Type: String
Description: Custom Image name
CustomImageDescription:
Type: String
Description: Custom Image description
AppImageConfigName:
Type: String
Description: App image config name for SageMaker custom image
KernelSpecsName:
Type: String
Description: Jupyter kernel name for SageMaker custom image, kernel name must match the name in custom name per to Jupyter kernel specs https://jupyter-client.readthedocs.io/en/stable/kernels.html
Default: python3
KernelSpecsDisplayName:
Type: String
Description: Jupyter kernel display name, this name is displayed in custom images in Studio
Default: Python 3
Outputs:
StudioDomainId:
Description: SageMaker Studio Domain ID
Value: !Ref SageMakerStudioDomain
Export:
Name: !Sub 'ds-sagemaker-studio-${SharedServiceStackSetName}-domain-id'
StudioUrl:
Description: Link to open SageMaker Studio
Value: !GetAtt SageMakerStudioDomain.Url
EFSKmsKeyId:
Description: KMS Key Id for the Studio EFS encryption
Value: !Ref SagemakerStudioKMS
EFSKmsKeyArn:
Description: KMS Key ARN for the Studio EFS encryption
Value: !GetAtt SagemakerStudioKMS.Arn
Export:
Name: !Sub 'ds-sagemaker-studio-kms-cmk-${SharedServiceStackSetName}-arn'
Resources:
SagemakerStudioKMS:
Type: 'AWS::KMS::Key'
Properties:
Description: Generated KMS Key for Sagemaker Studio's EFS encryption
EnableKeyRotation: true
Enabled: true
KeyPolicy:
Version: 2012-10-17
Id: KmsKey-EfsSagemakerStudioKey
Statement:
- Sid: Enable IAM User Permissions
Effect: Allow
Principal:
AWS: !Sub "arn:aws:iam::${AWS::AccountId}:root"
Action: 'kms:*'
Resource: '*'
- Sid: Allow access for Key Administrators
Action:
- 'kms:Create*'
- 'kms:Describe*'
- 'kms:Enable*'
- 'kms:List*'
- 'kms:Put*'
- 'kms:Update*'
- 'kms:Revoke*'
- 'kms:Disable*'
- 'kms:Get*'
- 'kms:Delete*'
- 'kms:TagResource'
- 'kms:UntagResource'
- 'kms:ScheduleKeyDeletion'
- 'kms:CancelKeyDeletion'
Effect: Allow
Principal:
AWS:
Fn::ImportValue:
!Sub "ds-administrator-role-${SharedServiceStackSetName}-arn"
Resource: '*'
- Sid: Allow access for Key Users
Effect: Allow
Principal:
AWS: !Sub "arn:aws:iam::${AWS::AccountId}:root"
Action:
- 'kms:Encrypt'
- 'kms:Decrypt'
- 'kms:CreateGrant'
- 'kms:ReEncrypt'
- 'kms:GenerateDataKey'
- 'kms:DescribeKey'
Resource: '*'
Condition:
StringNotEquals:
'aws:SourceVpce':
Fn::ImportValue:
!Sub "ds-s3-endpoint-${SharedServiceStackSetName}-id"
Tags:
- Key: StudioDomainName
Value: !Ref StudioDomainName
- Key: StackSetName
Value: !Ref SharedServiceStackSetName
KeyAlias:
Type: AWS::KMS::Alias
Properties:
AliasName: !Sub alias/${KMSAlias}
TargetKeyId: !Ref SagemakerStudioKMS
SageMakerCustomImage:
Type: AWS::SageMaker::Image
Properties:
ImageDescription: !Ref CustomImageDescription
ImageDisplayName: !Ref CustomImageDescription
ImageName: !Ref CustomImageName
# Requires SageMakerFullAccess
ImageRoleArn:
Fn::ImportValue:
!Sub 'ds-administrator-role-${SharedServiceStackSetName}-arn'
Tags:
- Key: StudioDomainName
Value: !Ref StudioDomainName
- Key: StackSetName
Value: !Ref SharedServiceStackSetName
SageMakerCustomImageVersion:
Type: AWS::SageMaker::ImageVersion
DependsOn:
- SageMakerCustomImage
Properties:
BaseImage: !Ref CustomImageEcrUri
ImageName: !Ref CustomImageName
SageMakerCustomImageAppConfig:
Type: AWS::SageMaker::AppImageConfig
DependsOn:
- SageMakerCustomImageVersion
Properties:
AppImageConfigName: !Ref AppImageConfigName
KernelGatewayImageConfig:
FileSystemConfig:
DefaultGid: 100
DefaultUid: 1000
MountPath: '/home/jovyan/work'
KernelSpecs:
- DisplayName: !Ref KernelSpecsDisplayName
Name: !Ref KernelSpecsName
Tags:
- Key: StudioDomainName
Value: !Ref StudioDomainName
- Key: StackSetName
Value: !Ref SharedServiceStackSetName
DataScientistDefaultRole:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
AWS: !Sub "arn:aws:iam::${AWS::AccountId}:root"
Action:
- 'sts:AssumeRole'
- Sid: SageMakerTrustRelationship
Effect: Allow
Principal:
Service:
- 'sagemaker.amazonaws.com'
Action:
- 'sts:AssumeRole'
RoleName: !Sub "ds-default-user-role-${SharedServiceStackSetName}"
Policies:
- PolicyName: SageMakerAccessInlinePolicy
PolicyDocument:
Version: 2012-10-17
Statement:
- Sid: DataScientistAdditionalPolicies
Effect: Allow
Action:
- 'sagemaker:UpdateCodeRepository'
- 'sagemaker:DeleteCodeRepository'
- 'sagemaker:CreateCodeRepository'
- 'sagemaker:StartNotebookInstance'
- 'sagemaker:StopNotebookInstance'
- 'sagemaker:CreateApp'
- 'sagemaker:AddTags'
- 'sagemaker:DeleteApp'
- 'sagemaker:GetSagemakerServicecatalogPortfolioStatus'
- 'codecommit:BatchGetRepositories'
- 'codecommit:GitPull'
- 'codecommit:GitPush'
- 'codecommit:CreateBranch'
- 'codecommit:DeleteBranch'
- 'codecommit:GetBranch'
- 'codecommit:ListBranches'
- 'codecommit:CreatePullRequest'
- 'codecommit:CreatePullRequestApproval'
- 'codecommit:GetPullRequest'
- 'codecommit:CreateCommit'
- 'codecommit:GetCommit'
- 'codecommit:GetCommitHistory'
- 'codecommit:GetDifferences'
- 'codecommit:GetReferences'
- 'codecommit:CreateRepository'
- 'codecommit:GetRepository'
- 'codecommit:ListRepositories'
- 'iam:TagRole'
- 'kms:CreateGrant'
- 'kms:DescribeKey'
- 'servicecatalog:ListAcceptedPortfolioShares'
Resource: '*'
- Sid: SageMakerIamPassRole
Effect: Allow
Action:
- 'iam:PassRole'
Resource: '*'
Condition:
StringEquals:
'iam:PassedToService':
- sagemaker.amazonaws.com
ManagedPolicyArns:
- 'arn:aws:iam::aws:policy/AWSServiceCatalogEndUserFullAccess'
- 'arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess'
- 'arn:aws:iam::aws:policy/AWSCloudFormationReadOnlyAccess'
- 'arn:aws:iam::aws:policy/AmazonSSMReadOnlyAccess'
- 'arn:aws:iam::aws:policy/AWSLambda_ReadOnlyAccess'
- 'arn:aws:iam::aws:policy/AWSCodeCommitReadOnly'
- 'arn:aws:iam::aws:policy/AWSCodeArtifactReadOnlyAccess'
- 'arn:aws:iam::aws:policy/AmazonSageMakerFullAccess'
- 'arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly'
Tags:
- Key: StudioDomainName
Value: !Ref StudioDomainName
- Key: StackSetName
Value: !Ref SharedServiceStackSetName
DataScientistDefaultRoleArn:
Type: 'AWS::SSM::Parameter'
Properties:
Name: !Sub "ds-default-user-role-${SharedServiceStackSetName}-arn"
Type: String
Value: !GetAtt DataScientistDefaultRole.Arn
Description: SSM-Parameter - Default DataScientist Role Arn
SageMakerStudioDomain:
Type: AWS::SageMaker::Domain
DependsOn:
- SageMakerCustomImageAppConfig
Properties:
AppNetworkAccessType: VpcOnly
AuthMode: IAM
DefaultUserSettings:
ExecutionRole: !GetAtt DataScientistDefaultRole.Arn
SecurityGroups:
- Fn::ImportValue:
!Sub 'ds-sagemaker-vpc-sg-${SharedServiceStackSetName}'
SharingSettings:
NotebookOutputOption: Disabled
KernelGatewayAppSettings:
CustomImages:
- AppImageConfigName: !Ref AppImageConfigName
ImageName: !Ref CustomImageName
DefaultResourceSpec:
InstanceType: 'ml.t3.medium'
SageMakerImageArn: !Ref SageMakerCustomImage
SageMakerImageVersionArn: !GetAtt SageMakerCustomImageVersion.ImageVersionArn
DomainName: !Ref StudioDomainName
KmsKeyId: !Ref SagemakerStudioKMS
SubnetIds:
- Fn::ImportValue:
!Sub 'ds-subnet1-${SharedServiceStackSetName}'
- Fn::ImportValue:
!Sub 'ds-subnet2-${SharedServiceStackSetName}'
- Fn::ImportValue:
!Sub 'ds-subnet3-${SharedServiceStackSetName}'
VpcId:
Fn::ImportValue: !Sub 'ds-vpc-${SharedServiceStackSetName}'
Tags:
- Key: StackSetName
Value: !Ref SharedServiceStackSetName