forked from aws-solutions/research-service-workbench-on-aws
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsagemakerNotebook.cfn.yaml
More file actions
292 lines (279 loc) · 10.5 KB
/
Copy pathsagemakerNotebook.cfn.yaml
File metadata and controls
292 lines (279 loc) · 10.5 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
AWSTemplateFormatVersion: 2010-09-09
Description: Service-Workbench-on-AWS SageMaker-Jupyter
Parameters:
Namespace:
Type: String
Description: An environment name that will be prefixed to resource names
InstanceType:
Type: String
Description: EC2 instance type to launch
Default: ml.t3.xlarge
VPC:
Description: VPC for Sagemaker Notebook
Type: AWS::EC2::VPC::Id
Subnet:
Description: Subnet for Sagemaker Notebook, from the VPC selected above
Type: AWS::EC2::Subnet::Id
AccessFromCIDRBlock:
Type: String
Description: The CIDR used to access sagemaker notebook
Default: 10.0.0.0/19
S3Mounts:
Type: String
Description: A JSON array of objects with name, bucket and prefix properties used to mount data
IamPolicyDocument:
Type: String
Description: The IAM policy to be associated with the launched workstation
EnvironmentInstanceFiles:
Type: String
Description: >-
An S3 URI (starting with "s3://") that specifies the location of files to be copied to
the environment instance, including any bootstrap scripts
EncryptionKeyArn:
Type: String
Description: The ARN of the KMS encryption Key used to encrypt data in the notebook
AutoStopIdleTimeInMinutes:
Type: Number
Description: Number of idle minutes for auto stop to shutdown the instance (0 to disable auto-stop)
DatasetsBucketArn:
Type: String
Description: Name of the datasets bucket in the main account
MainAccountId:
Type: String
Description: The Main Account ID where application is deployed
MainAccountRegion:
Type: String
Description: The region of application deployment in main account
MainAccountKeyArn:
Type: String
Description: The ARN of main account bucket encryption key
Conditions:
IamPolicyEmpty: !Equals [!Ref IamPolicyDocument, '{}']
Resources:
SecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: SageMaker Notebook Instance
VpcId:
Ref: VPC
SecurityGroupEgress:
- IpProtocol: tcp
FromPort: 443
ToPort: 443
CidrIp: 0.0.0.0/0
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 443
ToPort: 443
CidrIp: !Ref AccessFromCIDRBlock
InstanceRolePermissionBoundary:
Type: AWS::IAM::ManagedPolicy
Properties:
Description: Permission boundary for EC2 instance role
ManagedPolicyName: !Join ['-', [Ref: Namespace, 'ec2-sagemaker-permission-boundary']]
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- 's3:GetObject'
- 's3:AbortMultipartUpload'
- 's3:ListMultipartUploadParts'
- 's3:PutObject'
- 's3:GetObjectAcl'
- 's3:PutObjectAcl'
Resource:
- !Join ['/', [Ref: DatasetsBucketArn, '*']]
- !Join [':', ['arn:aws:s3', Ref: MainAccountRegion, Ref: MainAccountId, 'accesspoint/*']]
- !Join [
':',
['arn:aws:s3', Ref: MainAccountRegion, Ref: MainAccountId, 'accesspoint/*/object/*']
]
- Effect: Allow
Action:
- 's3:GetAccessPoint'
- 's3:ListAccessPoints'
Resource: '*' # These actions require wildcard resources
- Effect: Allow
Action:
- 's3:ListBucket'
Resource:
- !Ref DatasetsBucketArn
- !Join [':', ['arn:aws:s3', Ref: MainAccountRegion, Ref: MainAccountId, 'accesspoint/*']]
- Effect: Allow
Action:
- 's3:ListBucket'
Resource: !Sub
- 'arn:aws:s3:::${S3Bucket}'
- S3Bucket: !Select [2, !Split ['/', !Ref EnvironmentInstanceFiles]]
Condition:
StringLike:
s3:prefix: !Sub
- '${S3Prefix}/*'
- S3Prefix: !Select [3, !Split ['/', !Ref EnvironmentInstanceFiles]]
- Effect: Allow
Action:
- 's3:GetObject'
Resource: !Sub
- 'arn:aws:s3:::${S3Location}/*'
# Remove "s3://" prefix from EnvironmentInstanceFiles
- S3Location: !Select [1, !Split ['s3://', !Ref EnvironmentInstanceFiles]]
- Effect: Allow
Action:
- logs:CreateLogStream
- logs:DescribeLogStreams
- logs:PutLogEvents
- logs:CreateLogGroup
Resource:
- !Sub arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/sagemaker/*
- Effect: Allow
Action:
- 'kms:CreateGrant'
- 'kms:CreateKey'
- 'kms:DescribeKey'
- 'kms:EnableKeyRotation'
- 'kms:Encrypt'
- 'kms:Decrypt'
- 'kms:GenerateDataKey'
- 'kms:Get*'
- 'kms:List*'
Resource:
- !Ref EncryptionKeyArn
- !Ref MainAccountKeyArn
- Effect: Allow
Action:
- 'sts:AssumeRole'
Resource: 'arn:aws:iam::*:role/swb-*'
- Effect: Allow
Action:
- sagemaker:DescribeNotebookInstance
- sagemaker:StopNotebookInstance
Resource: '*'
Metadata:
cfn_nag:
rules_to_suppress:
- id: W13
reason: 'We allow the wildcard resource for sagemaker permissions since we do not know the sagemaker notebook instance identifier at the time of creation of this permisison boundary'
IAMRole:
Type: 'AWS::IAM::Role'
Properties:
RoleName: !Join ['-', [Ref: Namespace, 'sagemaker-notebook-role']]
Path: '/'
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: 'Allow'
Principal:
Service:
- 'sagemaker.amazonaws.com'
Action:
- 'sts:AssumeRole'
Policies:
- !If
- IamPolicyEmpty
- !Ref 'AWS::NoValue'
- PolicyName: !Join ['-', [Ref: Namespace, 's3-studydata-policy']]
PolicyDocument: !Ref IamPolicyDocument
- PolicyName: !Join ['-', [Ref: Namespace, 's3-bootstrap-script-policy']]
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: 'Allow'
Action: 's3:GetObject'
Resource: !Sub
- 'arn:aws:s3:::${S3Location}/*'
# Remove "s3://" prefix from EnvironmentInstanceFiles
- S3Location: !Select [1, !Split ['s3://', !Ref EnvironmentInstanceFiles]]
- Effect: 'Allow'
Action: 's3:ListBucket'
Resource: !Sub
- 'arn:aws:s3:::${S3Bucket}'
- S3Bucket: !Select [2, !Split ['/', !Ref EnvironmentInstanceFiles]]
Condition:
StringLike:
s3:prefix: !Sub
- '${S3Prefix}/*'
- S3Prefix: !Select [3, !Split ['/', !Ref EnvironmentInstanceFiles]]
- Effect: Allow
Action:
- 'kms:CreateGrant'
- 'kms:Get*'
- 'kms:List*'
- 'kms:Decrypt'
- 'kms:DescribeKey'
- 'kms:Encrypt'
- 'kms:GenerateDataKey'
Resource:
- !Ref MainAccountKeyArn
- PolicyName: cw-logs
PolicyDocument:
Statement:
Effect: Allow
Action:
- logs:CreateLogStream
- logs:DescribeLogStreams
- logs:PutLogEvents
- logs:CreateLogGroup
Resource:
- !Sub arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/sagemaker/*
PermissionsBoundary: !Ref InstanceRolePermissionBoundary
# This policy is attached to the role after the instance is created
# so that the instance can be referenced in the resource section
NotebookStopPolicy:
Type: AWS::IAM::Policy
Properties:
PolicyName: NotebookStop
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- sagemaker:DescribeNotebookInstance
- sagemaker:StopNotebookInstance
Resource:
- !Ref BasicNotebookInstance
Roles:
- !Ref IAMRole
# TODO: Consider also passing DefaultCodeRepository to allow persisting notebook data
BasicNotebookInstance:
Type: 'AWS::SageMaker::NotebookInstance'
Properties:
InstanceType: !Ref InstanceType
RoleArn: !GetAtt IAMRole.Arn
SubnetId: !Ref Subnet
SecurityGroupIds:
- !Ref SecurityGroup
DirectInternetAccess: Enabled
LifecycleConfigName: !GetAtt BasicNotebookInstanceLifecycleConfig.NotebookInstanceLifecycleConfigName
KmsKeyId: !Ref EncryptionKeyArn
# Add script for mounting studies and autostop
BasicNotebookInstanceLifecycleConfig:
Type: 'AWS::SageMaker::NotebookInstanceLifecycleConfig'
Properties:
OnStart:
- Content:
Fn::Base64: !Sub |
#!/usr/bin/env bash
# Download and execute bootstrap script
aws s3 cp "${EnvironmentInstanceFiles}/get_bootstrap.sh" "/tmp"
chmod 500 "/tmp/get_bootstrap.sh"
/tmp/get_bootstrap.sh "${EnvironmentInstanceFiles}" '${S3Mounts}'
# Stop Idle Script
if [ "${AutoStopIdleTimeInMinutes}" != "0" ]; then
echo "Fetching the autostop script"
aws s3 cp "${EnvironmentInstanceFiles}/offline-packages/sagemaker/autostop.py" "/usr/local/bin"
chmod a+x /usr/local/bin/autostop.py
IDLE_TIME=`expr ${AutoStopIdleTimeInMinutes} \* 60`
echo "Starting the SageMaker autostop script in cron"
(crontab -l 2>/dev/null; echo "*/1 * * * * /usr/bin/python /usr/local/bin/autostop.py --time $IDLE_TIME --ignore-connections >> /var/log/autostop.log") | crontab -
fi
Outputs:
NotebookInstanceName:
Description: The name of the SageMaker notebook instance.
Value: !GetAtt [BasicNotebookInstance, NotebookInstanceName]
EnvironmentInstanceRoleArn:
Description: IAM role assumed by the SageMaker environment
Value: !GetAtt IAMRole.Arn
NotebookArn:
Description: SageMaker Notebook Arn
Value: !Ref BasicNotebookInstance