-
Notifications
You must be signed in to change notification settings - Fork 0
/
serverless.yml
603 lines (566 loc) · 21.3 KB
/
serverless.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
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
##
## This software is Copyright ©️ 2020 The University of Southern California. All Rights Reserved.
## Permission to use, copy, modify, and distribute this software and its documentation for educational, research and non-profit purposes, without fee, and without a written agreement is hereby granted, provided that the above copyright notice and subject to the full license file found in the root of this software deliverable. Permission to make commercial use of this software may be obtained by contacting: USC Stevens Center for Innovation University of Southern California 1150 S. Olive Street, Suite 2300, Los Angeles, CA 90115, USA Email: [email protected]
##
## The full terms of this copyright and license should always be found in the root directory of this software deliverable as "license.txt" and if these terms are not found with this software, please contact the USC Stevens Center for the full license.
##
# todo: rename to mentorpal-upload-processor
service: mentorpal-upload-sm
# pin to only deploy with a specific Serverless version
frameworkVersion: '3'
plugins:
- serverless-deployment-bucket
- serverless-layers
- serverless-domain-manager
custom:
domain:
dev: api.devmentorpal.org
qa: api.qamentorpal.org
prod: api.mentorpal.org
customDomain:
domainName: ${self:custom.domain.${opt:stage, 'dev'}}
basePath: 'upload' # api root url becomes: api.mentorpal.org/upload
# certificateName: '*.mentorpal.org' didnt work
# certificateName: 'c6107db7-c2ef-4e85-a21f-bee7d1ac008a' didnt work either
stage: ${self:provider.stage}
endpointType: 'regional'
apiType: rest
createRoute53Record: true
securityPolicy: tls_1_2
# we might have multiple services, better to manually invoke `sls create_domain`
autoDomain: false
stages:
dev:
LOG_LEVEL: DEBUG
IS_SENTRY_ENABLED: false
# v2 is hardcoded, we could use fallback stages instead
# hack: qa and prod are in different regions so deploy fails because
# it cannot resolve other env variables from ssm.
# That's why we provide an empty default value '', but this
# in effect prevent sls from catching errors (missing variable in ssm)!
S3_STATIC_ARN: ${ssm:/devmentorpal/s3_static_arn, ''}
GRAPHQL_ENDPOINT: https://api.devmentorpal.org/graphql/graphql
SECRET_HEADER_VALUE: ${ssm:/mentorpal/newdev/shared/secret_header_value, ''}
SECRET_HEADER_NAME: ${ssm:/mentorpal/newdev/shared/secret_header_name, ''}
JWT_SECRET: ${ssm:/mentorpal/newdev/shared/jwt_secret, ''}
STATIC_URL_BASE: ${ssm:/mentorpal/newdev/shared/static_url_base, ''}
ALERT_SNS_ARN: 'no alerts in dev'
WEBACL_ARN: ${ssm:/devmentorpal/api_firewall_arn, ''}
qa:
S3_STATIC_ARN: ${ssm:/qamentorpal/s3_static_arn, ''}
LOG_LEVEL: DEBUG
IS_SENTRY_ENABLED: true
GRAPHQL_ENDPOINT: https://api.qamentorpal.org/graphql/graphql
JWT_SECRET: ${ssm:/mentorpal/v2/shared/jwt_secret, ''}
SECRET_HEADER_VALUE: ${ssm:/mentorpal/v2/shared/secret_header_value, ''}
SECRET_HEADER_NAME: ${ssm:/mentorpal/v2/shared/secret_header_name, ''}
STATIC_URL_BASE: ${ssm:/mentorpal/v2/shared/static_url_base, ''}
ALERT_SNS_ARN: ${ssm:/qamentorpal/shared/sns_alert_topic_arn, ''}
WEBACL_ARN: ${ssm:/qamentorpal/api_firewall_arn, ''}
prod:
LOG_LEVEL: INFO
IS_SENTRY_ENABLED: true
GRAPHQL_ENDPOINT: https://api.mentorpal.org/graphql/graphql
S3_STATIC_ARN: ${ssm:/mentorpal/s3_static_arn, ''}
JWT_SECRET: ${ssm:/mentorpal/cf/shared/jwt_secret, ''}
SECRET_HEADER_VALUE: ${ssm:/mentorpal/cf/shared/secret_header_value, ''}
SECRET_HEADER_NAME: ${ssm:/mentorpal/cf/shared/secret_header_name, ''}
ALERT_SNS_ARN: ${ssm:/mentorpal/shared/sns_alert_topic_arn, ''}
STATIC_URL_BASE: ${ssm:/mentorpal/cf/shared/static_url_base, ''}
WEBACL_ARN: ${ssm:/mentorpal/api_firewall_arn, ''}
# serverless-layers requires a deployment bucket to be created before deploying this stack
serverless-layers:
- dependencies:
layersDeploymentBucket: ${self:provider.deploymentBucket.name}
dependenciesPath: ./requirements.txt
compatibleRuntimes:
- python3.9
# applies to all lambdas
provider:
name: aws
region: ${opt:region, 'us-east-1'}
stage: ${opt:stage, 'dev'} # stage is dev unless otherwise specified with --stage flag
deploymentBucket:
name: '${self:service}-sls-deploy-${self:provider.stage}'
blockPublicAccess: true
serverSideEncryption: AES256
versioning: false
stackTags:
ENVIRONMENT: ${self:provider.stage}
PROJECT: ${self:service}-${self:provider.stage}
REPOSITORY: mentor-upload
runtime: python3.9
architecture: x86_64 # because of the static ffmpeg binaries and python dependencies
endpointType: regional
apiGateway:
minimumCompressionSize: 1024
# https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-payload-encodings.html
binaryMediaTypes:
- 'multipart/form-data'
tracing:
lambda: true
apiGateway: true
logRetentionInDays: 30
logs:
restApi:
# Enables HTTP access logs (default: true)
accessLogging: true
# # Log format to use for access logs
# format: 'requestId: $context.requestId'
# Enable execution logging (default: true)
executionLogging: true
level: INFO # INFO or ERROR
# Log full requests/responses for execution logging (default: true)
fullExecutionData: false
environment:
STAGE: ${self:provider.stage}
PYTHON_ENV: careerfair-${self:provider.stage}
S3_STATIC_ARN: ${self:custom.stages.${self:provider.stage}.S3_STATIC_ARN}
FFMPEG_EXECUTABLE: /opt/ffmpeg/ffmpeg
FFPROBE_EXECUTABLE: /opt/ffmpeg/ffprobe
GRAPHQL_ENDPOINT: ${self:custom.stages.${self:provider.stage}.GRAPHQL_ENDPOINT}
IS_SENTRY_ENABLED: ${self:custom.stages.${self:provider.stage}.IS_SENTRY_ENABLED}
STATIC_URL_BASE: ${self:custom.stages.${self:provider.stage}.STATIC_URL_BASE}
SENTRY_DSN_MENTOR_UPLOAD: '${ssm:/mentorpal/upload/sentry_dsn}'
TRANSCRIBE_INPUT_BUCKET: '${self:service}-transcribe-input-${self:provider.stage}'
TRANSCRIBE_OUTPUT_BUCKET: '${self:service}-transcribe-output-${self:provider.stage}'
SIGNED_UPLOAD_BUCKET: '${self:service}-signed-upload-${self:provider.stage}'
SECRET_HEADER_NAME: ${self:custom.stages.${self:provider.stage}.SECRET_HEADER_NAME}
SECRET_HEADER_VALUE: ${self:custom.stages.${self:provider.stage}.SECRET_HEADER_VALUE}
# AWS_REGION is reserved
REGION: ${self:provider.region}
# iam permissions for all lambda functions
iam:
role:
statements:
- Effect: "Allow"
Action:
- "s3:PutObject"
- "s3:GetObject"
Resource:
- '${self:custom.stages.${self:provider.stage}.S3_STATIC_ARN}/*'
- 'arn:aws:s3:::${self:provider.environment.TRANSCRIBE_INPUT_BUCKET}/*'
- 'arn:aws:s3:::${self:provider.environment.TRANSCRIBE_OUTPUT_BUCKET}/*'
- 'arn:aws:s3:::${self:provider.environment.SIGNED_UPLOAD_BUCKET}/*'
- Effect: "Allow"
Action:
- "s3:ListBucket"
Resource:
- '${self:custom.stages.${self:provider.stage}.S3_STATIC_ARN}'
- 'arn:aws:s3:::${self:provider.environment.TRANSCRIBE_INPUT_BUCKET}'
- 'arn:aws:s3:::${self:provider.environment.TRANSCRIBE_OUTPUT_BUCKET}'
- 'arn:aws:s3:::${self:provider.environment.SIGNED_UPLOAD_BUCKET}'
- Effect: Allow
Action:
- "transcribe:StartTranscriptionJob"
Resource: "*"
- Effect: "Allow"
Action:
- dynamodb:GetItem
- dynamodb:PutItem
- dynamodb:UpdateItem
Resource:
Fn::GetAtt: [JobsTable, Arn]
- Effect: Allow
Action:
- states:StartExecution
- states:SendTaskSuccess
- states:SendTaskFailure
Resource:
- Fn::GetAtt: [AnswerUploadStepFunction, Arn]
package:
# individually: false
patterns:
# exclude everything:
- '!./**'
# and then add back in only the files we need:
- '*.py'
- 'module/**'
layers:
# binaries are shared and this will make lambdas size smaller
binaries:
path: ./binaries
package:
patterns:
- '!./**'
# relative to layer path:
# when attached, these are available under /opt
- ./ffmpeg/ffmpeg
- ./ffmpeg/ffprobe
- './MediaInfo_DLL_21.09_Lambda/lib/**'
name: binaries-layer-${self:provider.stage}
description: Bundles ffmpeg and mediainfo binaries/libs
compatibleArchitectures: # optional, a list of architectures this layer is compatible with
- x86_64
licenseInfo: GPLv3 # optional, a string specifying license information
retain: false # If true, layer versions are not deleted as new ones are created
functions:
authorizer_func:
handler: authorizer.handler
environment:
JWT_SECRET: ${self:custom.stages.${self:provider.stage}.JWT_SECRET}
http_thumbnail:
handler: thumbnail.handler
memorySize: 1024
timeout: 30 # 30sec is max for http requests
events:
- http:
path: /thumbnail
method: post
cors: true
authorizer:
name: authorizer_func
resultTtlInSeconds: 300
identitySource: method.request.header.Authorization
type: token
http_header:
handler: header_upload.handler
memorySize: 1024
timeout: 30 # 30sec is max for http requests
events:
- http:
path: /header
method: post
cors: true
authorizer:
name: authorizer_func
resultTtlInSeconds: 300
identitySource: method.request.header.Authorization
type: token
http_footer:
handler: footer_upload.handler
memorySize: 1024
timeout: 30 # 30sec is max for http requests
events:
- http:
path: /footer
method: post
cors: true
authorizer:
name: authorizer_func
resultTtlInSeconds: 300
identitySource: method.request.header.Authorization
type: token
http_vbg:
handler: vbg_upload.handler
memorySize: 1024
timeout: 30 # 30sec is max for http requests
events:
- http:
path: /vbg
method: post
cors: true
authorizer:
name: authorizer_func
resultTtlInSeconds: 300
identitySource: method.request.header.Authorization
type: token
http_vtt:
handler: vtt_upload.handler
memorySize: 1024
timeout: 30 # 30sec is max for http requests
events:
- http:
path: /vtt
method: post
cors: true
authorizer:
name: authorizer_func
resultTtlInSeconds: 300
identitySource: method.request.header.Authorization
type: token
http_transfer_start:
handler: transfer-start.handler
memorySize: 512
timeout: 10
environment:
JOBS_TABLE_NAME: upload-jobs-${self:provider.stage}
events:
- http:
path: /transfer/mentor
method: post
cors: true
authorizer:
name: authorizer_func
resultTtlInSeconds: 300
identitySource: method.request.header.Authorization
type: token
http_regen_vtt:
handler: regen-vtt.handler
memorySize: 512
timeout: 10
events:
- http:
path: /regen_vtt
method: post
cors: true
authorizer:
name: authorizer_func
resultTtlInSeconds: 300
identitySource: method.request.header.Authorization
type: token
layers:
# binaries gets named "Binaries"+"LambdaLayer":
# see https://www.serverless.com/framework/docs/providers/aws/guide/layers#using-your-layers
- { Ref: BinariesLambdaLayer }
http_transfer_status:
handler: transfer-status.handler
memorySize: 256
timeout: 10
environment:
JOBS_TABLE_NAME: upload-jobs-${self:provider.stage}
events:
- http:
path: /transfer/status/{id}
method: get
cors: true
authorizer:
name: authorizer_func
resultTtlInSeconds: 300
identitySource: method.request.header.Authorization
type: token
request:
parameters:
paths:
id: true
dbstream_transfer_process:
description: Triggered by new records in dynamo, implements mentor transfer
handler: transfer-process.handler
memorySize: 2048
timeout: 900 # max
environment:
JOBS_TABLE_NAME: upload-jobs-${self:provider.stage}
events:
- stream:
type: dynamodb
maximumRetryAttempts: 3
arn:
Fn::GetAtt: [JobsTable, StreamArn]
# to avoid triggers on status update, make sure it's only when a new job is added:
filterPatterns:
- eventName: [INSERT]
http_upload_url:
handler: upload-url.handler
memorySize: 256
timeout: 10
events:
- http:
path: /upload/url
method: get
cors: true
authorizer:
name: authorizer_func
resultTtlInSeconds: 300
identitySource: method.request.header.Authorization
type: token
http_answer_upload:
handler: answer-upload.handler
memorySize: 1024
timeout: 30 # 30sec is max for http requests
environment:
ANSWER_UPLOAD_STEP_FUNCTION_ARN:
Fn::GetAtt: [AnswerUploadStepFunction, Arn]
events:
- http:
path: /upload/answer
method: post
cors: true
authorizer:
name: authorizer_func
resultTtlInSeconds: 300
identitySource: method.request.header.Authorization
type: token
layers:
# binaries gets named "Binaries"+"LambdaLayer":
# see https://www.serverless.com/framework/docs/providers/aws/guide/layers#using-your-layers
- { Ref: BinariesLambdaLayer }
step_trim:
handler: step-trim.handler
memorySize: 4096 # needs cpu to run ffmpeg
timeout: 600 # some videos take 5min or so
layers:
# binaries gets named "Binaries"+"LambdaLayer":
# see https://www.serverless.com/framework/docs/providers/aws/guide/layers#using-your-layers
- { Ref: BinariesLambdaLayer }
step_transcode_web:
handler: step-transcode-web.handler
memorySize: 8192 # 5min video takes 100sec with 2GB, 50sec with 4GB, 30sec with 8GB
timeout: 900 # give max time to make sure it gets transcoded
layers:
- { Ref: BinariesLambdaLayer }
step_transcode_mobile:
handler: step-transcode-mobile.handler
memorySize: 8192 # 5min video takes 100sec with 2GB, 50sec with 4GB, 30sec with 8GB
timeout: 900 # give max time to make sure it gets transcoded
layers:
- { Ref: BinariesLambdaLayer }
step_transcribe_start:
handler: step-transcribe-start.handler
memorySize: 1024 # need to extract audio
timeout: 30
environment:
TRANSCRIBE_INPUT_BUCKET: ${self:provider.environment.TRANSCRIBE_INPUT_BUCKET}
TRANSCRIBE_OUTPUT_BUCKET: ${self:provider.environment.TRANSCRIBE_OUTPUT_BUCKET}
layers:
# binaries gets named "Binaries"+"LambdaLayer":
# see https://www.serverless.com/framework/docs/providers/aws/guide/layers#using-your-layers
- { Ref: BinariesLambdaLayer }
step_transcribe_collect:
handler: step-transcribe-collect.handler
memorySize: 512
timeout: 30
events:
# only one suffix is allowed and we have .json and .vtt
- s3:
bucket: ${self:provider.environment.TRANSCRIBE_OUTPUT_BUCKET}
event: s3:ObjectCreated:*
existing: true # otherwise sls will try to create it twice and fail
rules:
- suffix: '.json'
- s3:
bucket: ${self:provider.environment.TRANSCRIBE_OUTPUT_BUCKET}
event: s3:ObjectCreated:*
existing: true # otherwise sls will try to create it twice and fail
rules:
- suffix: '.vtt'
step_mark_failed:
handler: step-mark-failed.handler
timeout: 30
resources:
Conditions:
CreateCloudWatchAlarm:
Fn::Or:
- Fn::Equals: ['${self:provider.stage}', 'qa']
- Fn::Equals: ['${self:provider.stage}', 'prod']
Resources:
AnswerUploadStepFunction: ${file(./resources/StepFunctions/AnswerUploadStepFunction.yml):StateMachine}
AnswerUploadStepFunctionExecutionRole: ${file(./resources/StepFunctions/AnswerUploadStepFunctionExecutionRole.yml):Role}
AnswerUploadDLQ:
Type: AWS::SQS::Queue
Properties:
DelaySeconds: 30
QueueName: answer-upload-dlq-${self:provider.stage}
MessageRetentionPeriod: 1209600 # max 14 days, default is 4 days
TranscribeInputBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: ${self:provider.environment.TRANSCRIBE_INPUT_BUCKET}
LifecycleConfiguration:
Rules:
- Id: DeleteInputFileAfter7Days
Status: Enabled
ExpirationInDays: 7
TranscribeOutputBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: ${self:provider.environment.TRANSCRIBE_OUTPUT_BUCKET}
LifecycleConfiguration:
Rules:
- Id: DeleteResultAfter7Days
Status: Enabled
ExpirationInDays: 7
AnswerUploadDLQAlarm:
Type: AWS::CloudWatch::Alarm
Condition: CreateCloudWatchAlarm
Properties:
ActionsEnabled: true
AlarmName: 'Answer upload failed jobs ${self:resources.Resources.AnswerUploadDLQ.Properties.QueueName}'
AlarmDescription: 'Number of failed answer upload jobs greater than zero'
Namespace: 'AWS/SQS'
Statistic: 'Sum'
MetricName: 'ApproximateNumberOfMessagesVisible'
ComparisonOperator: 'GreaterThanThreshold'
EvaluationPeriods: 1
Period: 300 # 5 minutes in seconds
Threshold: 0
TreatMissingData: 'notBreaching'
Dimensions:
- Name: QueueName
Value: ${self:resources.Resources.AnswerUploadDLQ.Properties.QueueName}
AlarmActions:
- ${self:custom.stages.${self:provider.stage}.ALERT_SNS_ARN}
OKActions:
- ${self:custom.stages.${self:provider.stage}.ALERT_SNS_ARN}
ApiErrors:
Type: AWS::CloudWatch::Alarm
Condition: CreateCloudWatchAlarm
Properties:
ActionsEnabled: true
AlarmName: 'upload processor API Gateway 5xx errors ${self:provider.stage}'
AlarmDescription: 'upload processor API Gateway 5xx errors ${self:provider.stage}'
Namespace: AWS/ApiGateway
MetricName: 5XXError
Statistic: Sum
Threshold: 0
ComparisonOperator: 'GreaterThanThreshold'
EvaluationPeriods: 1
Period: 60
TreatMissingData: 'notBreaching'
Dimensions:
- Name: ApiName
Value: '${self:provider.stage}-${self:service}'
AlarmActions:
- ${self:custom.stages.${self:provider.stage}.ALERT_SNS_ARN}
Response5xx:
Type: 'AWS::ApiGateway::GatewayResponse'
Properties:
ResponseParameters:
gatewayresponse.header.Access-Control-Allow-Origin: 'method.request.header.origin'
ResponseType: DEFAULT_5XX
RestApiId:
Ref: 'ApiGatewayRestApi'
Response4xx:
Type: 'AWS::ApiGateway::GatewayResponse'
Properties:
ResponseParameters:
gatewayresponse.header.Access-Control-Allow-Origin: 'method.request.header.origin'
ResponseType: DEFAULT_4XX
RestApiId:
Ref: 'ApiGatewayRestApi'
JobsTable:
Type: AWS::DynamoDB::Table
DeletionPolicy: Delete
UpdateReplacePolicy: Delete
Properties:
TableName: upload-jobs-${self:provider.stage}
BillingMode: PAY_PER_REQUEST
StreamSpecification:
StreamViewType: NEW_IMAGE
AttributeDefinitions:
- AttributeName: id
AttributeType: S
KeySchema:
- AttributeName: id
KeyType: HASH
# to cleanup jobs after a while set this attribute as a number
TimeToLiveSpecification:
AttributeName: ttl
Enabled: true
SignedUploadBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: ${self:provider.environment.SIGNED_UPLOAD_BUCKET}
# videos are processed and uploaded to STATIC_S3 so clean them up:
LifecycleConfiguration:
Rules:
- Id: DeleteResultAfter30Days
Status: Enabled
ExpirationInDays: 30
CorsConfiguration:
CorsRules:
- AllowedMethods:
- PUT
- POST
AllowedOrigins:
- '*'
AllowedHeaders:
- '*'
# this fails on first deploy because CloudFormation tries to create
# association but the gateway does not yet exist
FirewallAssociation:
Type: AWS::WAFv2::WebACLAssociation
DependsOn: "ApiGatewayRestApi"
Properties:
ResourceArn:
Fn::Sub: 'arn:aws:apigateway:${AWS::Region}::/restapis/${ApiGatewayRestApi}/stages/${self:provider.stage}'
WebACLArn: '${self:custom.stages.${self:provider.stage}.WEBACL_ARN}'