-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserverless.yml
446 lines (422 loc) · 14.6 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
##
## 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.
##
# This file is the main config file for your service.
#
# For full config options, check the docs:
# docs.serverless.com
#
service: mentor-classifier-service
# pin the service to only deploy with a specific Serverless version
frameworkVersion: '4'
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: 'classifier'
# 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:
offline:
LOG_LEVEL: 'trace'
GRAPHQL_ENDPOINT: 'http://127.0.0.1:3001/graphql'
SBERT_ENDPOINT: 'http://127.0.0.1:5566/v1'
IS_SENTRY_ENABLED: false
dev:
LOG_LEVEL: 'trace'
IS_SENTRY_ENABLED: false
GRAPHQL_ENDPOINT: https://api.devmentorpal.org/graphql/graphql
SBERT_ENDPOINT: 'https://sbert-qa.mentorpal.org/v1'
SECRET_HEADER_VALUE: ${ssm:/mentorpal/newdev/shared/secret_header_value, ''}
SECRET_HEADER_NAME: ${ssm:/mentorpal/newdev/shared/secret_header_name, ''}
API_SECRET: ${ssm:/mentorpal/newdev/shared/api_secret, ''}
JWT_SECRET: ${ssm:/mentorpal/newdev/shared/jwt_secret, ''}
ALERT_SNS_ARN: 'no alarms in dev'
WEBACL_ARN: ${ssm:/devmentorpal/api_firewall_arn, ''}
qa:
LOG_LEVEL: 'debug'
IS_SENTRY_ENABLED: true
GRAPHQL_ENDPOINT: https://api.qamentorpal.org/graphql/graphql
SBERT_ENDPOINT: 'https://sbert-qa.mentorpal.org/v1'
SECRET_HEADER_VALUE: ${ssm:/mentorpal/v2/shared/secret_header_value, ''}
SECRET_HEADER_NAME: ${ssm:/mentorpal/v2/shared/secret_header_name, ''}
API_SECRET: ${ssm:/mentorpal/v2/shared/api_secret, ''}
JWT_SECRET: ${ssm:/mentorpal/v2/shared/jwt_secret, ''}
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
SBERT_ENDPOINT: 'https://sbert.mentorpal.org/v1'
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, ''}
API_SECRET: ${ssm:/mentorpal/cf/shared/api_secret, ''}
JWT_SECRET: ${ssm:/mentorpal/cf/shared/jwt_secret, ''}
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
functions:
- http_train
- http_train_status
- http_training_data
- authorizer_func
provider:
name: aws
region: ${opt:region, 'us-east-1'}
stage: ${opt:stage, 'dev'} # stage is dev unless otherwise specified with --stage flag
runtime: python3.9
stackTags:
ENVIRONMENT: ${self:provider.stage}
PROJECT: ${self:service}-${self:provider.stage}
REPOSITORY: mentor-classifier-service
deploymentBucket:
name: '${self:service}-sls-deploy-${self:provider.stage}'
blockPublicAccess: true
serverSideEncryption: AES256
versioning: false
architecture: x86_64 # since we build on x86 and have native modules
ecr:
# define images that will be built locally and uploaded to ECR
images:
train:
path: ./
file: Dockerfile
# # Enable for deployment on Mac machines
# platform: linux/amd64
# buildOptions:
# [
# "--provenance",
# "false"
# ]
predict:
path: ./
file: Dockerfile.predict
# # Enable for deployment on Mac machines
# platform: linux/amd64
# buildOptions:
# [
# "--provenance",
# "false"
# ]
followup:
path: ./
file: Dockerfile.followup
# # Enable for deployment on Mac machines
# platform: linux/amd64
# buildOptions:
# [
# "--provenance",
# "false"
# ]
endpointType: regional
apiGateway:
minimumCompressionSize: 1024
usagePlan:
throttle:
burstLimit: 100
rateLimit: 10
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: stage-${self:provider.stage}
GRAPHQL_ENDPOINT: ${self:custom.stages.${self:provider.stage}.GRAPHQL_ENDPOINT}
SBERT_ENDPOINT: ${self:custom.stages.${self:provider.stage}.SBERT_ENDPOINT}
API_SECRET: ${self:custom.stages.${self:provider.stage}.API_SECRET}
JWT_SECRET: ${self:custom.stages.${self:provider.stage}.JWT_SECRET}
SECRET_HEADER_VALUE: ${self:custom.stages.${self:provider.stage}.SECRET_HEADER_VALUE}
SECRET_HEADER_NAME: ${self:custom.stages.${self:provider.stage}.SECRET_HEADER_NAME}
IS_SENTRY_ENABLED: ${self:custom.stages.${self:provider.stage}.IS_SENTRY_ENABLED}
SENTRY_DSN_MENTOR_CLASSIFIER: '${ssm:/mentorpal/classifier/sentry_dsn}'
SHARED_ROOT: '/app/shared' # as defined in Dockerfile
MODELS_BUCKET: classifier-models-${self:provider.stage}
JOBS_TABLE_NAME: classifier-jobs-${self:provider.stage}
JOBS_SQS_NAME: classifier-jobs-${self:provider.stage}
REGION: ${self:provider.region}
iam:
role:
statements:
# todo create dedicated roles for each function for increased security
- Effect: "Allow"
Action:
- "s3:PutObject"
- "s3:GetObject"
Resource:
- 'arn:aws:s3:::${self:provider.environment.MODELS_BUCKET}/*'
- Effect: "Allow"
Action:
- "s3:ListBucket"
Resource:
- 'arn:aws:s3:::${self:provider.environment.MODELS_BUCKET}'
- Effect: "Allow"
Action:
- dynamodb:GetItem
- dynamodb:PutItem
- dynamodb:UpdateItem
Resource:
Fn::GetAtt: [JobsTable, Arn]
- Effect: "Allow"
Action:
- sqs:SendMessage
- sqs:GetQueueUrl
Resource:
Fn::GetAtt: [TrainQueue, Arn]
package:
individually: false
patterns:
# exclude everything:
- '!./**'
# and then add back in only the files we need:
- '*.py'
- 'module/**'
functions:
authorizer_func:
handler: authorizer.handler
http_train:
handler: train.handler
memorySize: 1024
timeout: 30
events:
- http:
path: /train
method: post
cors: true
authorizer:
name: authorizer_func
resultTtlInSeconds: 300
identitySource: method.request.header.Authorization
type: token
http_train_status:
handler: status.handler
memorySize: 512
timeout: 10
events:
- http:
path: /train/status/{id}
method: get
cors: true
authorizer:
name: authorizer_func
resultTtlInSeconds: 300
identitySource: method.request.header.Authorization
type: token
request:
parameters:
paths:
id: true
sqs_train:
image:
name: train
command:
- trainjob.handler
memorySize: 8192
timeout: 600
events:
- sqs:
arn:
Fn::GetAtt: [TrainQueue, Arn]
batchSize: 1
http_answer:
image:
name: predict
command:
- predict.handler
memorySize: 2048
timeout: 30
events:
- http:
path: /questions
method: get
cors: true
request:
parameters:
querystrings:
mentor: true
query: true
chatsessionid: true
ping: false #optional
http_followup:
image:
name: followup
command:
- followup.handler
memorySize: 1024
timeout: 30
events:
- http:
path: /followups/category/{category}/{mentor}
method: post
cors: true
authorizer:
name: authorizer_func
resultTtlInSeconds: 300
identitySource: method.request.header.Authorization
type: token
request:
parameters:
paths:
category: true
mentor: true
http_training_data:
handler: trainingdata.handler
memorySize: 2048
timeout: 30
events:
- http:
path: /trainingdata/{mentor}
method: get
cors: true
authorizer:
name: authorizer_func
resultTtlInSeconds: 300
identitySource: method.request.header.Authorization
type: token
request:
parameters:
paths:
mentor: true
resources:
Conditions:
CreateCloudWatchAlarm:
Fn::Or:
- Fn::Equals: ['${self:provider.stage}', 'qa']
- Fn::Equals: ['${self:provider.stage}', 'prod']
Resources:
TrainDLQ:
Type: AWS::SQS::Queue
Properties:
DelaySeconds: 30
QueueName: classifier-jobs-dlq-${self:provider.stage}
MessageRetentionPeriod: 1209600 # max 14 days, default is 4 days
TrainQueue:
Type: AWS::SQS::Queue
Properties:
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-sqs-queues.html
QueueName: ${self:provider.environment.JOBS_SQS_NAME}
VisibilityTimeout: 800 # AWS recommends 6 times lambdas timeout
RedrivePolicy:
deadLetterTargetArn:
Fn::GetAtt: [TrainDLQ, Arn]
maxReceiveCount: 5 # AWS recommends minimum 5
TrainedModelsBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: ${self:provider.environment.MODELS_BUCKET}
JobsTable:
Type: AWS::DynamoDB::Table
DeletionPolicy: Delete
UpdateReplacePolicy: Delete
Properties:
TableName: ${self:provider.environment.JOBS_TABLE_NAME}
BillingMode: PAY_PER_REQUEST
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
TrainDLQAlarm:
Type: AWS::CloudWatch::Alarm
Condition: CreateCloudWatchAlarm
Properties:
ActionsEnabled: true
AlarmName: 'Number of failed train jobs ${self:resources.Resources.TrainDLQ.Properties.QueueName}'
AlarmDescription: 'Number of failed train 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.TrainDLQ.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: 'classifier API Gateway 5xx errors - ${self:provider.stage}'
AlarmDescription: 'classifier 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'
FirewallAssociation:
Type: AWS::WAFv2::WebACLAssociation
Properties:
ResourceArn:
Fn::Sub: 'arn:aws:apigateway:${AWS::Region}::/restapis/${ApiGatewayRestApi}/stages/${self:provider.stage}'
WebACLArn: '${self:custom.stages.${self:provider.stage}.WEBACL_ARN}'