Repository navigation
Expand file tree
/
Copy pathstack.yaml
More file actions
611 lines (575 loc) · 17.1 KB
/
Copy pathstack.yaml
File metadata and controls
611 lines (575 loc) · 17.1 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
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
AWSTemplateFormatVersion: 2010-09-09
Description: Setup infrastructure for Rate My Post project
Parameters:
ProjectName:
Type: String
Default: ratemypost
PreBucket:
Type: String
Default: ratemypost-pre
AppBucket:
Type: String
Default: ratemypost-app
Mappings:
TaskMap:
upload:
name: zip-to-s3
triggerUnzip:
name: s3-to-ecs
unzip:
name: unzip-on-s3
parse:
name: parse-xml
featureEngineering:
name: feature-engineering
createFeatures:
name: get-features
triggerPredict:
name: run-prediction
predict:
name: post-predict
DirMap:
unzip:
name: unzip
zip:
name: zip
data:
name: data
glue:
name: glue
jars:
name: jars
lambda:
name: lambda
modelTraining:
name: training
Resources:
Bucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Ref ProjectName
PublicAccessBlockConfiguration:
BlockPublicAcls: true
BlockPublicPolicy: true
IgnorePublicAcls: true
RestrictPublicBuckets: true
NotificationConfiguration:
LambdaConfigurations:
- Event: s3:ObjectCreated:*
Function: !GetAtt TriggerUnzipLambda.Arn
Filter:
S3Key:
Rules:
- Name: prefix
Value: !Sub
- ${path}/
- path: !FindInMap [DirMap, zip, name]
DependsOn:
- TriggerUnzipLambdaPermission
ECSCluster:
Type: AWS::ECS::Cluster
Properties:
CapacityProviders:
- FARGATE
ClusterName: !Ref ProjectName
GlueDB:
Type: AWS::Glue::Database
Properties:
CatalogId: !Ref AWS::AccountId
DatabaseInput:
Name: default
LocationUri: !Sub
- s3://${bucketName}/${directoryName}
- bucketName: !Ref ProjectName
directoryName: !FindInMap [DirMap, data, name]
GlueDBTraining:
Type: AWS::Glue::Database
Properties:
CatalogId: !Ref AWS::AccountId
DatabaseInput:
Name: training
LocationUri: !Sub
- s3://${bucketName}/${directoryName}
- bucketName: !Ref ProjectName
directoryName: !FindInMap [DirMap, modelTraining, name]
ECSRoleS3:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- ecs-tasks.amazonaws.com
Action:
- sts:AssumeRole
ManagedPolicyArns:
- arn:aws:iam::aws:policy/AmazonS3FullAccess
RoleName: ecs-run-task-s3
LambdaRoleS3:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
ManagedPolicyArns:
- arn:aws:iam::aws:policy/CloudWatchLogsFullAccess
- arn:aws:iam::aws:policy/AmazonEC2ContainerServiceFullAccess
- arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess
RoleName: lambda-get-s3-run-ecs
GlueRoleS3:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- glue.amazonaws.com
Action:
- sts:AssumeRole
ManagedPolicyArns:
- arn:aws:iam::aws:policy/AmazonS3FullAccess
- arn:aws:iam::aws:policy/service-role/AWSGlueServiceNotebookRole
- arn:aws:iam::aws:policy/service-role/AWSGlueServiceRole
RoleName: glue-run-job-s3
GlueRoleS3DynamoDB:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- glue.amazonaws.com
Action:
- sts:AssumeRole
ManagedPolicyArns:
- arn:aws:iam::aws:policy/AmazonS3FullAccess
- arn:aws:iam::aws:policy/service-role/AWSGlueServiceNotebookRole
- arn:aws:iam::aws:policy/service-role/AWSGlueServiceRole
- arn:aws:iam::aws:policy/AmazonDynamoDBFullAccess
RoleName: glue-run-job-s3-dynamodb
ECSExecutionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- ecs-tasks.amazonaws.com
Action:
- sts:AssumeRole
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy
RoleName: ecs-execute-task
UploadTaskRepo:
Type: AWS::ECR::Repository
Properties:
RepositoryName: !FindInMap [TaskMap, upload, name]
UnzipTaskRepo:
Type: AWS::ECR::Repository
Properties:
RepositoryName: !FindInMap [TaskMap, unzip, name]
UploadTaskDefinition:
Type: AWS::ECS::TaskDefinition
Properties:
ContainerDefinitions:
- Name: !FindInMap [TaskMap, upload, name]
Image: !Sub
- ${AccountId}.dkr.ecr.${Region}.amazonaws.com/${RepoName}
- AccountId: !Ref AWS::AccountId
Region: !Ref AWS::Region
RepoName: !Ref UploadTaskRepo
Essential: true
LogConfiguration:
LogDriver: awslogs
Options:
awslogs-group: !Sub
- /ecs/${TaskName}
- TaskName: !FindInMap [TaskMap, upload, name]
awslogs-region: !Ref AWS::Region
awslogs-stream-prefix: ecs
Cpu: 2vCPU
ExecutionRoleArn: !Ref ECSExecutionRole
Family: !FindInMap [TaskMap, upload, name]
Memory: 4GB
NetworkMode: awsvpc
RequiresCompatibilities:
- FARGATE
TaskRoleArn: !GetAtt ECSRoleS3.Arn
DependsOn:
- ECSRoleS3
- UploadTaskRepo
- UploadTaskLogGroup
UnzipTaskDefinition:
Type: AWS::ECS::TaskDefinition
Properties:
ContainerDefinitions:
- Name: !FindInMap [TaskMap, unzip, name]
Image: !Sub
- ${AccountId}.dkr.ecr.${Region}.amazonaws.com/${RepoName}
- AccountId: !Ref AWS::AccountId
Region: !Ref AWS::Region
RepoName: !Ref UnzipTaskRepo
Essential: true
LogConfiguration:
LogDriver: awslogs
Options:
awslogs-group: !Sub
- /ecs/${TaskName}
- TaskName: !FindInMap [TaskMap, unzip, name]
awslogs-region: !Ref AWS::Region
awslogs-stream-prefix: ecs
Cpu: 1vCPU
ExecutionRoleArn: !Ref ECSExecutionRole
Family: !FindInMap [TaskMap, unzip, name]
Memory: 2GB
NetworkMode: awsvpc
RequiresCompatibilities:
- FARGATE
TaskRoleArn: !GetAtt ECSRoleS3.Arn
DependsOn:
- ECSRoleS3
- UnzipTaskRepo
- UploadTaskLogGroup
UploadTaskLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub
- /ecs/${TaskName}
- TaskName: !FindInMap [TaskMap, upload, name]
UnzipTaskLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub
- /ecs/${TaskName}
- TaskName: !FindInMap [TaskMap, unzip, name]
TriggerUnzipLambda:
Type: AWS::Lambda::Function
Properties:
Code:
S3Bucket: !Ref PreBucket
S3Key: !Sub
- ${lambda_dir}/${task_name}/lambda_handler.py
- lambda_dir: !FindInMap [DirMap, lambda, name]
task_name: !FindInMap [TaskMap, triggerUnzip, name]
Description: Run ECS task on S3 file
Environment:
Variables:
CLUSTER_NAME: !Ref ECSCluster
TASK_UNZIP: !FindInMap [TaskMap, unzip, name]
UNZIP_DIR: !FindInMap [DirMap, unzip, name]
FunctionName: !FindInMap [TaskMap, triggerUnzip, name]
Handler: lambda_handler.lambda_handler
MemorySize: 128
PackageType: Zip
Role: !GetAtt LambdaRoleS3.Arn
Runtime: python3.7
Timeout: 10
DependsOn:
- LambdaRoleS3
TriggerUnzipLambdaPermission:
Type: AWS::Lambda::Permission
Properties:
Action: lambda:InvokeFunction
FunctionName: !GetAtt TriggerUnzipLambda.Arn
Principal: s3.amazonaws.com
SourceArn: !Sub arn:aws:s3:::${ProjectName}
DependsOn:
- TriggerUnzipLambda
ParseXMLJob:
Type: AWS::Glue::Job
Properties:
Command:
Name: glueetl
PythonVersion: 3
ScriptLocation: !Sub
- s3://${bucket_name}/${glue_dir}/${job_name}/glue_job.py
- bucket_name: !Ref PreBucket
glue_dir: !FindInMap [DirMap, glue, name]
job_name: !FindInMap [TaskMap, parse, name]
DefaultArguments:
--enable-glue-datacatalog: ""
--enable-metrics: ""
--enable-spark-ui: true
--enable-continuous-cloudwatch-log: true
--enable-continuous-log-filter: true
--job-language: python
--extra-jars: !Sub
- s3://${bucket_name}/${jar_dir}/spark-xml_2.11-0.11.0.jar
- bucket_name: !Ref PreBucket
jar_dir: !FindInMap [DirMap, jars, name]
--bucket_name: !Ref ProjectName
--input_dir: !FindInMap [DirMap, unzip, name]
--output_dir: !FindInMap [DirMap, data, name]
--database_name: default
Description: Parse XML data and save to parquet
ExecutionProperty:
MaxConcurrentRuns: 1
GlueVersion: 2.0
MaxRetries: 0
Name: !FindInMap [TaskMap, parse, name]
NumberOfWorkers: 4
Role: !GetAtt GlueRoleS3.Arn
Timeout: 120
WorkerType: G.1X
DependsOn:
- GlueRoleS3
- Bucket
GetFeaturesJob:
Type: AWS::Glue::Job
Properties:
Command:
Name: glueetl
PythonVersion: 3
ScriptLocation: !Sub
- s3://${bucket_name}/${glue_dir}/${job_name}/glue_job.py
- bucket_name: !Ref PreBucket
glue_dir: !FindInMap [DirMap, glue, name]
job_name: !FindInMap [TaskMap, createFeatures, name]
DefaultArguments:
--enable-glue-datacatalog: ""
--enable-metrics: ""
--enable-spark-ui: true
--enable-continuous-cloudwatch-log: true
--enable-continuous-log-filter: true
--job-language: python
--database_name: default
Description: Create DynamoDB with features for prediction
ExecutionProperty:
MaxConcurrentRuns: 1
GlueVersion: 2.0
MaxRetries: 0
Name: !FindInMap [TaskMap, createFeatures, name]
NumberOfWorkers: 4
Role: !GetAtt GlueRoleS3DynamoDB.Arn
Timeout: 120
WorkerType: G.1X
DependsOn:
- GlueRoleS3DynamoDB
- Bucket
FeatureEngineeringJob:
Type: AWS::Glue::Job
Properties:
Command:
Name: glueetl
PythonVersion: 3
ScriptLocation: !Sub
- s3://${bucket_name}/${glue_dir}/${job_name}/glue_job.py
- bucket_name: !Ref PreBucket
glue_dir: !FindInMap [DirMap, glue, name]
job_name: !FindInMap [TaskMap, featureEngineering, name]
DefaultArguments:
--enable-glue-datacatalog: ""
--enable-metrics: ""
--enable-spark-ui: true
--enable-continuous-cloudwatch-log: true
--enable-continuous-log-filter: true
--job-language: python
--extra-jars: !Sub
- s3://${bucket_name}/${jar_dir}/spark-nlp-assembly-2.6.2.jar
- bucket_name: !Ref PreBucket
jar_dir: !FindInMap [DirMap, jars, name]
--additional-python-modules: spark-nlp==2.6.2
--input_db: default
--output_db: training
--model_bucket: !Ref PreBucket
Description: Feature engineering for model training
ExecutionProperty:
MaxConcurrentRuns: 1
GlueVersion: 2.0
MaxRetries: 0
Name: !FindInMap [TaskMap, featureEngineering, name]
NumberOfWorkers: 4
Role: !GetAtt GlueRoleS3.Arn
Timeout: 480
WorkerType: G.1X
DependsOn:
- GlueRoleS3
- Bucket
DataProcessingWorkflow:
Type: AWS::Glue::Workflow
Properties:
Description: Parse XML and create tables
Name: data-processing
ParseXMLTrigger:
Type: AWS::Glue::Trigger
Properties:
Actions:
- JobName: !Ref GetFeaturesJob
- JobName: !Ref FeatureEngineeringJob
StartOnCreation: true
Name: trigger
Predicate:
Conditions:
- JobName: !Ref ParseXMLJob
LogicalOperator: EQUALS
State: SUCCEEDED
Logical: AND
Type: CONDITIONAL
WorkflowName: !Ref DataProcessingWorkflow
DependsOn:
- GetFeaturesJob
- ParseXMLJob
- DataProcessingWorkflow
ParseXMLStart:
Type: AWS::Glue::Trigger
Properties:
Actions:
- JobName: !FindInMap [TaskMap, parse, name]
Name: start-workflow
Type: ON_DEMAND
WorkflowName: !Ref DataProcessingWorkflow
DependsOn:
- DataProcessingWorkflow
SagemakerRole:
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
- Effect: Allow
Principal:
Service:
- sagemaker.amazonaws.com
Action:
- sts:AssumeRole
ManagedPolicyArns:
- arn:aws:iam::aws:policy/AmazonS3FullAccess
- arn:aws:iam::aws:policy/AmazonSageMakerFullAccess
RoleName: sagemaker-role
LambdaRolePrediction:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
ManagedPolicyArns:
- arn:aws:iam::aws:policy/CloudWatchLogsFullAccess
- arn:aws:iam::aws:policy/AmazonDynamoDBReadOnlyAccess
Policies:
- PolicyName: LambdaSagemakerInvoke
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action: "sagemaker:InvokeEndpoint"
Resource: "*"
RoleName: lambda-predict
TriggerPredictLambda:
Type: AWS::Lambda::Function
Properties:
Code:
S3Bucket: !Ref PreBucket
S3Key: !Sub
- ${lambda_dir}/${task_name}.zip
- lambda_dir: !FindInMap [DirMap, lambda, name]
task_name: !FindInMap [TaskMap, triggerPredict, name]
Description: Run prediction from user request
Environment:
Variables:
ENDPOINT_NAME: !FindInMap [TaskMap, predict, name]
NLTK_DATA: nltk_data
FunctionName: !FindInMap [TaskMap, triggerPredict, name]
Handler: lambda_handler.lambda_handler
MemorySize: 256
PackageType: Zip
Role: !GetAtt LambdaRolePrediction.Arn
Runtime: python3.7
Timeout: 30
DependsOn:
- LambdaRolePrediction
TriggerPredictLambdaLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub
- /aws/lambda/${lambdaFunctionName}
- lambdaFunctionName: !Ref TriggerPredictLambda
RetentionInDays: 30
DependsOn:
- TriggerPredictLambda
ModelApi:
Type: AWS::ApiGateway::RestApi
Properties:
EndpointConfiguration:
Types:
- REGIONAL
Name: !Ref ProjectName
ModelApiPost:
Type: AWS::ApiGateway::Method
Properties:
AuthorizationType: NONE
HttpMethod: POST
Integration:
IntegrationHttpMethod: POST
Type: AWS_PROXY
Uri: !Sub
- arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${lambdaArn}/invocations
- lambdaArn: !GetAtt TriggerPredictLambda.Arn
ResourceId: !GetAtt ModelApi.RootResourceId
RestApiId: !Ref ModelApi
DependsOn:
- ModelApi
- TriggerPredictLambda
ModelApiDeployment:
Type: AWS::ApiGateway::Deployment
Properties:
RestApiId: !Ref ModelApi
StageName: prod
DependsOn:
- ModelApiPost
TriggerPredictLambdaPermission:
Type: AWS::Lambda::Permission
Properties:
Action: lambda:InvokeFunction
FunctionName: !GetAtt TriggerPredictLambda.Arn
Principal: apigateway.amazonaws.com
SourceArn: !Sub arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${ModelApi}/prod/POST/
DependsOn:
- ModelApiDeployment
UserTable:
Type: AWS::DynamoDB::Table
Properties:
AttributeDefinitions:
- AttributeName: "id"
AttributeType: "S"
BillingMode: PAY_PER_REQUEST
KeySchema:
- AttributeName: "id"
KeyType: "HASH"
TableName: users
TagTable:
Type: AWS::DynamoDB::Table
Properties:
AttributeDefinitions:
- AttributeName: "id"
AttributeType: "S"
BillingMode: PAY_PER_REQUEST
KeySchema:
- AttributeName: "id"
KeyType: "HASH"
TableName: tags