-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathidp-deploy.yaml
447 lines (435 loc) · 17.8 KB
/
idp-deploy.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
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
AWSTemplateFormatVersion: "2010-09-09"
Description: |
(SO9052) - This solution creates an Amazon SageMaker Studio domain along with the necessary resources required for the Intelligent Document Processing Workshop.
Parameters:
UserProfileName:
Type: String
Description: The user profile name for the IDP workshop
Default: 'SageMakerUser'
DomainName:
Type: String
Description: The domain name of the Sagemaker studio instance
Default: 'IDPSagemakerDomain'
Mappings:
JupyterMap:
us-east-1:
jupyterimage: "arn:aws:sagemaker:us-east-1:081325390199:image/jupyter-server-3"
us-east-2:
jupyterimage: "arn:aws:sagemaker:us-east-2:429704687514:image/jupyter-server-3"
us-west-1:
jupyterimage: "arn:aws:sagemaker:us-west-1:742091327244:image/jupyter-server-3"
us-west-2:
jupyterimage: "arn:aws:sagemaker:us-west-2:236514542706:image/jupyter-server-3"
af-south-1:
jupyterimage: "arn:aws:sagemaker:af-south-1:559312083959:image/jupyter-server-3"
ap-east-1:
jupyterimage: "arn:aws:sagemaker:ap-east-1:493642496378:image/jupyter-server-3"
ap-south-1:
jupyterimage: "arn:aws:sagemaker:ap-south-1:394103062818:image/jupyter-server-3"
ap-northeast-2:
jupyterimage: "arn:aws:sagemaker:ap-northeast-2:806072073708:image/jupyter-server-3"
ap-southeast-1:
jupyterimage: "arn:aws:sagemaker:ap-southeast-1:492261229750:image/jupyter-server-3"
ap-southeast-2:
jupyterimage: "arn:aws:sagemaker:ap-southeast-2:452832661640:image/jupyter-server-3"
ap-northeast-1:
jupyterimage: "arn:aws:sagemaker:ap-northeast-1:102112518831:image/jupyter-server-3"
ca-central-1:
jupyterimage: "arn:aws:sagemaker:ca-central-1:310906938811:image/jupyter-server-3"
eu-central-1:
jupyterimage: "arn:aws:sagemaker:eu-central-1:936697816551:image/jupyter-server-3"
eu-west-1:
jupyterimage: "arn:aws:sagemaker:eu-west-1:470317259841:image/jupyter-server-3"
eu-west-2:
jupyterimage: "arn:aws:sagemaker:eu-west-2:712779665605:image/jupyter-server-3"
eu-west-3:
jupyterimage: "arn:aws:sagemaker:eu-west-3:615547856133:image/jupyter-server-3"
eu-north-1:
jupyterimage: "arn:aws:sagemaker:eu-north-1:243637512696:image/jupyter-server-3"
eu-south-1:
jupyterimage: "arn:aws:sagemaker:eu-south-1:592751261982:image/jupyter-server-3"
sa-east-1:
jupyterimage: "arn:aws:sagemaker:sa-east-1:782484402741:image/jupyter-server-3"
RegionMap:
us-east-1:
datascience: "arn:aws:sagemaker:us-east-1:081325390199:image/datascience-1.0"
us-east-2:
datascience: "arn:aws:sagemaker:us-east-2:429704687514:image/datascience-1.0"
us-west-1:
datascience: "arn:aws:sagemaker:us-west-1:742091327244:image/datascience-1.0"
us-west-2:
datascience: "arn:aws:sagemaker:us-west-2:236514542706:image/datascience-1.0"
af-south-1:
datascience: "arn:aws:sagemaker:af-south-1:559312083959:image/datascience-1.0"
ap-east-1:
datascience: "arn:aws:sagemaker:ap-east-1:493642496378:image/datascience-1.0"
ap-south-1:
datascience: "arn:aws:sagemaker:ap-south-1:394103062818:image/datascience-1.0"
ap-northeast-2:
datascience: "arn:aws:sagemaker:ap-northeast-2:806072073708:image/datascience-1.0"
ap-southeast-1:
datascience: "arn:aws:sagemaker:ap-southeast-1:492261229750:image/datascience-1.0"
ap-southeast-2:
datascience: "arn:aws:sagemaker:ap-southeast-2:452832661640:image/datascience-1.0"
ap-northeast-1:
datascience: "arn:aws:sagemaker:ap-northeast-1:102112518831:image/datascience-1.0"
ca-central-1:
datascience: "arn:aws:sagemaker:ca-central-1:310906938811:image/datascience-1.0"
eu-central-1:
datascience: "arn:aws:sagemaker:eu-central-1:936697816551:image/datascience-1.0"
eu-west-1:
datascience: "arn:aws:sagemaker:eu-west-1:470317259841:image/datascience-1.0"
eu-west-2:
datascience: "arn:aws:sagemaker:eu-west-2:712779665605:image/datascience-1.0"
eu-west-3:
datascience: "arn:aws:sagemaker:eu-west-3:615547856133:image/datascience-1.0"
eu-north-1:
datascience: "arn:aws:sagemaker:eu-north-1:243637512696:image/datascience-1.0"
eu-south-1:
datascience: "arn:aws:sagemaker:eu-south-1:488287956546:image/sagemaker-data-wrangler-1.0"
sa-east-1:
datascience: "arn:aws:sagemaker:sa-east-1:782484402741:image/datascience-1.0"
Resources:
LambdaExecutionRole:
Type: "AWS::IAM::Role"
Properties:
Description: 'Lambda Role for Cloudformation'
Policies:
- PolicyName: sagemaker-access
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- sagemaker:UpdateDomain
- sagemaker:UpdateUserProfile
- sagemaker:CreateStudioLifecycleConfig
- sagemaker:DeleteStudioLifecycleConfig
- sagemaker:CreateApp
- sagemaker:DeleteApp
Resource: '*'
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- "sts:AssumeRole"
Path: /
ManagedPolicyArns:
- 'arn:aws:iam::aws:policy/AmazonEC2ReadOnlyAccess'
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
SageMakerExecutionRole:
Type: AWS::IAM::Role
Properties:
Description: 'IDP IAM role for Sagemaker'
Policies:
- PolicyName: cognito-access
PolicyDocument:
Version: 2012-10-17
Statement:
-
Effect: Allow
Action:
- cognito-idp:Describe*
- cognito-idp:List*
- cognito-idp:AdminAddUserToGroup
- cognito-idp:AdminCreateUser
- cognito-idp:AdminDeleteUser
- cognito-idp:AdminDisableUser
- cognito-idp:AdminEnableUser
- cognito-idp:AdminRemoveUserFromGroup
- cognito-idp:CreateGroup
- cognito-idp:CreateUserPool*
- cognito-idp:UpdateUserPool*
Resource: '*'
- PolicyName: s3-access
PolicyDocument:
Version: 2012-10-17
Statement:
-
Effect: Allow
Action:
- s3:GetObject
- s3:PutObject
- s3:DeleteObject
- s3:ListBucket
Resource: arn:aws:s3:::*
- PolicyName: comprehend-passrole
PolicyDocument:
Version: 2012-10-17
Statement:
-
Effect: Allow
Action:
- iam:PassRole
Resource: arn:aws:iam::*:role/*
Condition:
StringLikeIfExists:
'iam:PassedToService': "comprehend.amazonaws.com"
- PolicyName: textract-comprehend-sl-access
PolicyDocument:
Version: 2012-10-17
Statement:
-
Effect: Allow
Action:
- textract:GetDocumentTextDetection
- textract:GetDocumentAnalysis
- textract:AnalyzeDocument
- textract:AnalyzeID
- textract:AnalyzeExpense
- textract:DetectDocumentText
- textract:StartDocumentAnalysis
- textract:StartDocumentTextDetection
- textract:StartLendingAnalysis
- textract:GetLendingAnalysis
- comprehend:DetectEntities
- comprehend:DetectPiiEntities
- comprehend:ContainsPiiEntities
- comprehend:DescribePiiEntitiesDetectionJob
- comprehend:ListPiiEntitiesDetectionJobs
- comprehend:StartPiiEntitiesDetectionJob
- comprehend:StopPiiEntitiesDetectionJob
- comprehend:StartEntitiesDetectionJob
- comprehend:ClassifyDocument
- comprehend:DescribeDocumentClassificationJob
- comprehend:DescribeDocumentClassifier
- comprehend:CreateDocumentClassifier
- comprehend:CreateEntityRecognizer
- comprehend:DescribeEntityRecognizer
- comprehend:CreateEndpoint
- comprehend:DescribeEndpoint
- comprehend:DeleteEndpoint
- comprehend:DeleteDocumentClassifier
- comprehend:DeleteEntityRecognizer
- comprehend:StopTrainingDocumentClassifier
- comprehend:StopTrainingEntityRecognizer
- comprehend:ListEndpoints
- comprehend:ListEntityRecognizers
- comprehend:ListEntityRecognizerSummaries
- comprehend:ListDocumentClassifiers
- comprehend:ListDocumentClassifierSummaries
- comprehend:ImportModel
- comprehend:StartDocumentClassificationJob
- comprehendmedical:DetectEntitiesV2
- comprehendmedical:DetectPHI
- comprehendmedical:InferICD10CM
- comprehendmedical:InferRxNorm
- comprehendmedical:InferSNOMEDCT
- comprehendmedical:StartICD10CMInferenceJob
- comprehendmedical:StartRxNormInferenceJob
- comprehendmedical:StartSNOMEDCTInferenceJob
- comprehendmedical:StartPHIDetectionJob
Resource: '*'
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
-
Effect: Allow
Principal:
Service:
- sagemaker.amazonaws.com
- comprehend.amazonaws.com
Action:
- sts:AssumeRole
ManagedPolicyArns:
- arn:aws:iam::aws:policy/AmazonSageMakerFullAccess
- arn:aws:iam::aws:policy/IAMReadOnlyAccess
- arn:aws:iam::aws:policy/AmazonAugmentedAIIntegratedAPIAccess
DefaultVpcLambda:
Type: AWS::Lambda::Function
Properties:
FunctionName: CFGetDefaultVpcId
Code:
ZipFile: |
import json
import boto3
import cfnresponse
ec2 = boto3.client('ec2')
def lambda_handler(event, context):
if 'RequestType' in event and event['RequestType'] == 'Create':
vpc_id = get_default_vpc_id()
subnets = get_subnets_for_vpc(vpc_id)
if vpc_id and subnets:
cfnresponse.send(event, context, cfnresponse.SUCCESS, {'VpcId': vpc_id , "Subnets" : subnets}, '')
else:
cfnresponse.send(event, context, cfnresponse.FAILED, {'Error': "Missing Default VPC. A Default VPC is required"}, '')
else:
cfnresponse.send(event, context, cfnresponse.SUCCESS, {},'')
def get_default_vpc_id():
vpcs = ec2.describe_vpcs(Filters=[{'Name': 'is-default', 'Values': ['true']}])
vpcs = vpcs['Vpcs']
vpc_id = vpcs[0]['VpcId']
return vpc_id
def get_subnets_for_vpc(vpcId):
response = ec2.describe_subnets(
Filters=[
{
'Name': 'vpc-id',
'Values': [vpcId]
}
]
)
subnet_ids = []
for subnet in response['Subnets']:
subnet_ids.append(subnet['SubnetId'])
return subnet_ids
Description: Return default VPC ID and Subnets
Handler: index.lambda_handler
MemorySize: 512
Role: !GetAtt LambdaExecutionRole.Arn
Runtime: python3.9
Timeout: 5
DefaultVpcFinder:
Type: Custom::ResourceForFindingDefaultVpc
Properties:
ServiceToken: !GetAtt DefaultVpcLambda.Arn
LccLambda:
Type: AWS::Lambda::Function
Properties:
FunctionName: LccForSagemakerStudio
Code:
ZipFile: |
import json
import boto3
import base64
import textwrap
import cfnresponse
sagemaker = boto3.client('sagemaker')
def lambda_handler(event, context):
print(event)
script = textwrap.dedent('''\
#!/bin/bash
set -eux
echo "Cloning IDP repository"
export REPOSITORY_URL="https://github.com/aws-samples/aws-ai-intelligent-document-processing"
git -C /home/sagemaker-user clone $REPOSITORY_URL
echo "Cloning complete"''')
script_byte = script.encode("ascii")
base64_bytes = base64.b64encode(script_byte)
base64_string = base64_bytes.decode("ascii")
if 'RequestType' in event and event['RequestType'] == 'Create':
domain_id = event['ResourceProperties']['DomainID']
user_profile = event['ResourceProperties']['UserProfileName']
try:
resp = sagemaker.create_studio_lifecycle_config(StudioLifecycleConfigName='idp-git-bootstrap',
StudioLifecycleConfigContent=base64_string,
StudioLifecycleConfigAppType='JupyterServer')
lcc_config_arn = resp['StudioLifecycleConfigArn']
jupyter_setting = {'JupyterServerAppSettings': {
'DefaultResourceSpec': {
'LifecycleConfigArn': lcc_config_arn,
'InstanceType': 'system'
},
'LifecycleConfigArns': [ lcc_config_arn ]
}
}
resp_d = sagemaker.update_domain(DomainId=domain_id,
DefaultUserSettings=jupyter_setting)
cfnresponse.send(event, context, cfnresponse.SUCCESS, {}, '')
except Exception as e:
print(e)
cfnresponse.send(event, context, cfnresponse.FAILED, {'Error': 'Unable to create Lifecycle Config'}, '')
elif 'RequestType' in event and event['RequestType'] == 'Delete':
try:
resp = sagemaker.delete_studio_lifecycle_config(StudioLifecycleConfigName='idp-git-bootstrap')
cfnresponse.send(event, context, cfnresponse.SUCCESS, {}, '')
except Exception as e:
print(e)
cfnresponse.send(event, context, cfnresponse.SUCCESS, {'Message': 'Unable to delete Lifecycle Config.. proceeding'}, '')
else:
cfnresponse.send(event, context, cfnresponse.SUCCESS, {}, '')
Description: Creates a Lifecycle Config for the SageMaker Studio JupyterApp to clone the IDP Github repo
Handler: index.lambda_handler
MemorySize: 512
Role: !GetAtt LambdaExecutionRole.Arn
Runtime: python3.9
Timeout: 5
DefaultLcc:
Type: Custom::ResourceForLcc
DependsOn:
- StudioDomain
Properties:
ServiceToken: !GetAtt LccLambda.Arn
DomainID: !Ref StudioDomain
UserProfileName: !Ref UserProfileName
StudioDomain:
Type: AWS::SageMaker::Domain
Properties:
AppNetworkAccessType: PublicInternetOnly
AuthMode: IAM
DefaultUserSettings:
ExecutionRole: !GetAtt SageMakerExecutionRole.Arn
JupyterServerAppSettings:
DefaultResourceSpec:
InstanceType: system
SageMakerImageArn: !FindInMap
- JupyterMap
- !Ref 'AWS::Region'
- jupyterimage
DomainName: !Ref DomainName
SubnetIds: !GetAtt DefaultVpcFinder.Subnets
VpcId: !GetAtt DefaultVpcFinder.VpcId
UserProfile:
Type: AWS::SageMaker::UserProfile
DependsOn: DefaultLcc
Properties:
DomainId: !GetAtt StudioDomain.DomainId
UserProfileName: !Ref UserProfileName
UserSettings:
ExecutionRole: !GetAtt SageMakerExecutionRole.Arn
JupyterApp:
Type: AWS::SageMaker::App
DependsOn: UserProfile
Properties:
AppName: default
AppType: JupyterServer
DomainId: !GetAtt StudioDomain.DomainId
UserProfileName: !Ref UserProfileName
DataScienceApp:
Type: AWS::SageMaker::App
DependsOn: UserProfile
Properties:
AppName: instance-event-engine-datascience-ml-t3-medium
AppType: KernelGateway
DomainId: !GetAtt StudioDomain.DomainId
ResourceSpec:
InstanceType: ml.t3.medium
SageMakerImageArn: !FindInMap
- RegionMap
- !Ref 'AWS::Region'
- datascience
UserProfileName: !Ref UserProfileName
### S3 Bucket For A2I
A2IBucket:
Type: AWS::S3::Bucket
DeletionPolicy: Delete
Properties:
BucketName: !Join
- "-"
- - "idp-a2i"
- !Select
- 0
- !Split
- "-"
- !Select
- 2
- !Split
- "/"
- !Ref "AWS::StackId"
CorsConfiguration:
CorsRules:
-
AllowedOrigins:
- "*"
AllowedMethods:
- GET
BucketEncryption:
ServerSideEncryptionConfiguration:
- ServerSideEncryptionByDefault:
SSEAlgorithm: AES256
VersioningConfiguration:
Status: Enabled