-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathhail-jupyter.yml
376 lines (339 loc) · 12 KB
/
hail-jupyter.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
AWSTemplateFormatVersion: "2010-09-09"
Description: "SageMaker Jupyter Instance for use with Hail on EMR"
Parameters:
pAllowSsmShell:
AllowedValues:
- "false"
- "true"
Default: "false"
Description: "Required - Allow Systems Manager (SSM) shell access to EMR nodes. Must also be set to true on EMR CloudFormation stacks."
Type: "String"
pIngressCidr:
AllowedPattern: "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/(\\d{1,2})"
Description: "Required - CIDR is allowed full security group ingress. Examples - VPC CIDR, VPN CIDR, etc."
Type: "String"
pInstanceType:
AllowedValues:
- "ml.t2.medium"
- "ml.t2.large"
- "ml.t2.xlarge"
- "ml.t2.2xlarge"
- "ml.t3.medium"
- "ml.t3.large"
- "ml.t3.xlarge"
- "ml.t3.2large"
Default: "ml.t3.medium"
Description: "Required"
Type: "String"
pName:
Description: "Required - Used as the name of the notebook instance and S3 backup location. Recommend username. Example: aperry"
Type: "String"
pS3bucketJupyter:
Description: "Required - This parameter is set by the S3 stack. No changes should be necessary. S3 bucket with Jupyter scripts, common notebooks, and home directory backups."
Type: "AWS::SSM::Parameter::Value<String>"
Default: "/hail/s3/sagemaker"
pS3bucketHail:
Description: "Required - This parameter is set by the S3 stack. No changes should be necessary. Notebook instance will have full read."
Type: "AWS::SSM::Parameter::Value<String>"
Default: "/hail/s3/hail"
pSubnetId:
Description: "Required - Target subnet for Notebook server. Private subnet is strongly suggested as browser access will still be available via the AWS console."
Type: "AWS::EC2::Subnet::Id"
pTagEnvironment:
AllowedValues:
- "production"
- "staging"
- "sandbox"
- "test"
- "development"
- "qa"
- "dr"
Default: "development"
Description: "Optional - Environment type for default resource tagging."
Type: "String"
pEfsFsapId:
Default: ""
Description: "Optional - EFS file system acccess point. If provided, IAM access will be granted to mount the FSAP. E.g. fsap-0b3ec479999999999"
Type: "String"
pTagOwner:
Default: ""
Description: "Optional - Owner of the resources. Person/Department, etc."
Type: "String"
pVolumeSize:
Default: "20"
Description: "Required - Size (GB)."
MaxValue: "500"
MinValue: "5"
Type: "Number"
pVpcId:
Description: "Required - Notebook is deployed in this VPC."
Type: "AWS::EC2::VPC::Id"
Metadata:
AWS::CloudFormation::Interface:
ParameterGroups:
- Label:
default: "Network Details"
Parameters:
- "pVpcId"
- "pSubnetId"
- "pIngressCidr"
- Label:
default: "Instance Details"
Parameters:
- "pName"
- "pInstanceType"
- "pVolumeSize"
- "pAllowSsmShell"
- Label:
default: "S3 Resources"
Parameters:
- "pS3bucketHail"
- "pS3bucketJupyter"
- Label:
default: "EFS Settings"
Parameters:
- "pEfsFsapId"
- Label:
default: "Tagging"
Parameters:
- "pTagEnvironment"
- "pTagOwner"
ParameterLabels:
pAllowSsmShell:
default: "Allow SSM Shell Access to EMR Nodes"
pVpcId:
default: "VPC ID"
pSubnetId:
default: "Subnet ID"
pIngressCidr:
default: "Ingress CIDR"
pEfsFsapId:
default: "File System Access Point ID"
pName:
default: "Instance Name"
pInstanceType:
default: "Instance Type"
pVolumeSize:
default: "Volume Size"
pS3bucketHail:
default: "SSM Parameter for Hail Bucket"
pS3bucketJupyter:
default: "SSM Parameter for SageMaker Bucket"
pTagOwner:
default: "Owner"
pTagEnvironment:
default: "Environment"
Conditions:
CondAllowSsmShell: !Equals [!Ref pAllowSsmShell, "true"]
CondEfsFsapId: !Not [ !Equals [ !Ref pEfsFsapId, "" ] ]
Resources:
instance:
Type: "AWS::SageMaker::NotebookInstance"
Properties:
DirectInternetAccess: "Disabled"
InstanceType: !Ref pInstanceType
RoleArn: !GetAtt role.Arn
LifecycleConfigName: !GetAtt lifecycleConfiguration.NotebookInstanceLifecycleConfigName
NotebookInstanceName: !Ref pName
RootAccess: Enabled
SubnetId: !Ref pSubnetId
VolumeSizeInGB: !Ref pVolumeSize
SecurityGroupIds:
- !Ref sg
Tags:
- Key: "Name"
Value: !Ref pName
- Key: "environment"
Value: !Ref pTagEnvironment
- Key: "owner"
Value: !Ref pTagOwner
lifecycleConfiguration:
Type: "AWS::SageMaker::NotebookInstanceLifecycleConfig"
Properties:
NotebookInstanceLifecycleConfigName: !Sub "${pName}"
OnCreate:
- Content:
Fn::Base64: !Sub |
#!/bin/bash
set -ex
BUCKET="${pS3bucketJupyter}"
EC2_USER_BIN="/home/ec2-user/SageMaker/bin/"
sudo -u ec2-user -i <<EOF
set -ex
/usr/bin/aws s3 cp s3://"$BUCKET"/scripts/ "$EC2_USER_BIN" --recursive
find "$EC2_USER_BIN" -type f -exec chmod -v 700 {} \;
EOF
OnStart:
- Content:
Fn::Base64: !Sub |
#!/bin/bash
set -ex
BUCKET="${pS3bucketJupyter}"
DIRECTORY="/home/ec2-user/SageMaker/"
# Copy previously backed up user notebooks
/usr/bin/aws s3 cp s3://$BUCKET/${pName}/ $DIRECTORY --recursive
# Reset bin permissions
find "$DIRECTORY/bin/" -type f -exec chmod -v 700 {} \;
# Copy common organization notebooks. This will overwrite common notebooks previously saved to the home directory
/usr/bin/aws s3 cp s3://$BUCKET/common-notebooks/ $DIRECTORY/common-notebooks/ --recursive
# Use the system AWS, since ec2-user may change conda environments
CMD="/usr/bin/aws s3 sync --exclude 'bin/*' --exclude '.sparkmagic/*' --exclude 'efs/*' --exclude '*.ipynb_checkpoints/*' $DIRECTORY s3://$BUCKET/${pName}/ > /home/ec2-user/s3-backup.log 2>&1"
chown "ec2-user":"ec2-user" $DIRECTORY --recursive
# Backup the notebook details to S3 on 5 minute intervals
cronjob="*/5 * * * * $CMD"
echo "$cronjob" | crontab -u "ec2-user" -
# Conda/Pip additions
sudo -u ec2-user -i <<EOF
source /home/ec2-user/anaconda3/bin/activate JupyterSystemEnv
# Required for "%manage_spark"
jupyter labextension install @jupyter-widgets/jupyterlab-manager
source /home/ec2-user/anaconda3/bin/deactivate
# Prevent iopub throttling
sed -i.bak 's/^#c.NotebookApp.iopub_data_rate_limit.*$/c.NotebookApp.iopub_data_rate_limit = 1000000/' ~/.jupyter/jupyter_notebook_config.py
# Session Manager for SSM connections to EMR nodes, if desired.
sudo yum -y install https://s3.amazonaws.com/session-manager-downloads/plugin/latest/linux_64bit/session-manager-plugin.rpm
EOF
# Execute user's specific startup operations, if present
CUSTOM_USER_STARTUP_SCRIPT="$DIRECTORY/custom-user-startup.sh"
if [ -f "$CUSTOM_USER_STARTUP_SCRIPT" ]; then source "$CUSTOM_USER_STARTUP_SCRIPT"; fi
role:
Type: "AWS::IAM::Role"
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Principal:
Service:
- "sagemaker.amazonaws.com"
Action:
- "sts:AssumeRole"
Path: "/"
Policies:
- PolicyName: "s3FullUserPathControl"
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Action: "s3:*"
Resource:
- !Sub "arn:aws:s3:::${pS3bucketJupyter}/${pName}/*"
- Effect: "Allow"
Action:
- "s3:ListObject"
- "s3:ListBucket"
Resource: !Sub "arn:aws:s3:::${pS3bucketJupyter}"
- PolicyName: "s3ListPaths"
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Action: "s3:ListBucket"
Resource:
- !Sub "arn:aws:s3:::${pS3bucketJupyter}"
Condition:
StringLike:
s3:prefix:
- !Sub "${pName}/"
- "scripts/"
- "common-notebooks/"
- Effect: "Allow"
Action:
- "s3:GetObject"
Resource:
- !Sub "arn:aws:s3:::${pS3bucketJupyter}/scripts/*"
- !Sub "arn:aws:s3:::${pS3bucketJupyter}/common-notebooks/*"
- PolicyName: "s3hailFullRead"
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Action:
- "s3:Get*"
- "s3:List*"
Resource:
- !Sub "arn:aws:s3:::${pS3bucketHail}/*"
- !Sub "arn:aws:s3:::${pS3bucketHail}"
- PolicyName: "cloudwatchWrite"
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Action:
- "logs:CreateLogStream"
- "logs:CreateLogGroup"
- "logs:PutLogEvents"
Resource: "*"
- PolicyName: "emrListClusters"
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Action:
- "elasticmapreduce:ListInstances"
- "elasticmapreduce:ListBootstrapActions"
- "elasticmapreduce:ListClusters"
- "elasticmapreduce:DescribeCluster"
- "elasticmapreduce:ListInstanceGroups"
Resource: "*"
RoleName: !Sub "sagemaker-jupyter-${pName}"
policyAllowSsmShell:
Type: "AWS::IAM::Policy"
Condition: "CondAllowSsmShell"
Properties:
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Action:
- "ssm:StartSession"
Resource: "arn:aws:ec2:*:*:instance/*"
Condition:
StringLike:
"ssm:resourceTag/allow-emr-ssm":
- "true"
PolicyName: "ssm-session-by-tag"
Roles:
- !Ref role
policyEfsFsapFullAccess:
Condition: "CondEfsFsapId"
Type: "AWS::IAM::Policy"
Properties:
PolicyDocument:
Version: "2012-10-17"
Statement:
- Sid: "efsFsapFull"
Action:
- "elasticfilesystem:Client*"
Effect: "Allow"
Resource: "*"
Condition:
StringEquals:
"elasticfilesystem:AccessPointArn": !Sub "arn:aws:elasticfilesystem:${AWS::Region}:${AWS::AccountId}:access-point/${pEfsFsapId}"
PolicyName: "efs-fsap-full-access"
Roles:
- !Ref role
sg:
Type: "AWS::EC2::SecurityGroup"
Properties:
VpcId: !Ref pVpcId
GroupDescription: "Security group for Sagemaker Notebook to connect to Hail cluster."
SecurityGroupIngress:
- IpProtocol: "-1"
FromPort: -1
ToPort: -1
CidrIp: !Ref pIngressCidr
Description: "Full Access"
SecurityGroupEgress:
- IpProtocol: "-1"
FromPort: -1
ToPort: -1
CidrIp: "0.0.0.0/0"
Description: "all"
Tags:
- Key: "Name"
Value: !Sub "sagemaker-jupyter-${pName}"
- Key: "environment"
Value: !Ref pTagEnvironment
- Key: "owner"
Value: !Ref pTagOwner