-
Notifications
You must be signed in to change notification settings - Fork 0
/
serverless.yml
273 lines (263 loc) · 8.79 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
service: deep-util-services
frameworkVersion: "=3"
provider:
name: aws
runtime: python3.8
stage: ${opt:stage, "local"}
region: us-east-1
versionFunctions: false
memorySize: 512 # in MB
timeout: 30 # in seconds
environment:
SERVICE: ${self:service}
STAGE: ${self:provider.stage}
SOURCE_TABLE_NAME: ${self:custom.sourceTableName}
ASYNC_JOB_TABLE_NAME: ${self:custom.asyncJobTableName}
JWT_SECRET_OR_PUBLIC_KEY: ${file(./secrets/${self:provider.stage}.json):JWT_SECRET_OR_PUBLIC_KEY}
MEDIA_BUCKET_NAME: ${file(./secrets/${self:provider.stage}.json):MEDIA_BUCKET_NAME}
MEDIA_BUCKET_ROOT: ${file(./secrets/${self:provider.stage}.json):MEDIA_BUCKET_ROOT}
SENTRY_DNS: ${file(./secrets/${self:provider.stage}.json):SENTRY_DNS}
NLTK_DATA: /opt/nltk_data
iamRoleStatements:
- Effect: Allow
Action:
- s3:ListBucket
- s3:HeadObject
- s3:GetObject
- s3:PutObject
Resource:
- "arn:aws:s3:::${self:provider.environment.MEDIA_BUCKET_NAME}/*"
- Effect: Allow
Action:
- dynamodb:Query
- dynamodb:Scan
- dynamodb:GetItem
- dynamodb:BatchGetItem
- dynamodb:BatchWriteItem
- dynamodb:PutItem
- dynamodb:UpdateItem
- dynamodb:DeleteItem
- dynamodb:DescribeTable
Resource:
- "arn:aws:dynamodb:${opt:region, self:provider.region}:*:table/${self:provider.environment.SOURCE_TABLE_NAME}"
- "arn:aws:dynamodb:${opt:region, self:provider.region}:*:table/${self:provider.environment.ASYNC_JOB_TABLE_NAME}"
- Effect: Allow
Action:
# - lambda:InvokeFunction
# - lambda:InvokeAsync
- states:StartExecution
# TODO: NOT WORKING Resource: ${self:resources.Outputs.SourceExtractStateMachine.Value}
Resource: "*"
custom:
stepFunctionConfig:
beta:
MaxConcurrency: 50
local:
MaxConcurrency: 50
MaxConcurrency: ${self:custom.stepFunctionConfig.${self:provider.stage}.MaxConcurrency, 1}
dynamodbConfig:
beta:
BillingMode: PAY_PER_REQUEST
dev:
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
nightly:
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
local:
BillingMode: PAY_PER_REQUEST
# ProvisionedThroughput:
# ReadCapacityUnits: 1
# WriteCapacityUnits: 1
BillingMode: ${self:custom.dynamodbConfig.${self:provider.stage}.BillingMode, "PROVISIONED"}
ProvisionedThroughput: ${self:custom.dynamodbConfig.${self:provider.stage}.ProvisionedThroughput, null}
dynamodb:
stages:
- ${self:provider.stage}
sourceTableName: "${self:service}-${self:provider.stage}-sources"
asyncJobTableName: "${self:service}-${self:provider.stage}-async-jobs"
authorizer: authorizerFunc
cors:
origins: ${file(./secrets/${self:provider.stage}.json):CORS_DOMAINS}
pythonRequirements:
dockerizePip: true
noDeploy:
# Dependencies of omitted packages must explicitly be omitted too
- pytest
- parameterized
domains:
beta: services.thedeep.io
dev: services-alpha.thedeep.io
nightly: services-nightly.thedeep.io
local: services-local.thedeep.io
customDomain:
domainName: ${self:custom.domains.${self:provider.stage}}
stage: ${self:provider.stage}
createRoute53Record: false
certificateArn: ${file(./secrets/${self:provider.stage}.json):CERTIFICATE_ARN}
package:
exclude:
- .git/**
- .gitignore
- .DS_Store
- npm-debug.log
- .serverless/**
- .serverless_plugins/**
- .python-venv/**
- .pytest_cache/**
functions:
webInfoExtract:
handler: src.functions.source_extract.handler.web_info_extract
# TODO: Seperate requirements (only handle common modules like utils as vendor)
# module: src/functions/source_extract
events:
- http:
path: web-info-extract
method: get
authorizer: ${self:custom.authorizer}
cors: ${self:custom.cors}
sourceExtract:
handler: src.functions.source_extract.handler.main
environment:
SOURCE_EXTRACT_STATE_MACHINE_ARN: ${self:resources.Outputs.SourceExtractStateMachine.Value}
events:
- http:
path: source-extract
method: post
authorizer: ${self:custom.authorizer}
cors: ${self:custom.cors}
handleEntries:
handler: src.ngrams.handler.handle_entries
events:
- http:
path: ngram-process
method: post
authorizer: ${self:custom.authorizer}
cors: ${self:custom.cors}
layers:
- {Ref: NltkLambdaLayer}
sourceExtractJobSuccess:
handler: src.functions.source_extract.handler.source_extract_job_success
sourceExtractJobFailure:
handler: src.functions.source_extract.handler.source_extract_job_failure
sourceExtractJobMappedTask:
handler: src.functions.source_extract.handler.source_extract_job_mapped_task
timeout: 120
# TODO: Deploy without python dependencies
authorizerFunc:
runtime: nodejs12.x
handler: src/authorizer/handler.auth
stepFunctions:
stateMachines:
SourceExtractStateMachine:
name: "sourceExtractStateMachine-${self:provider.stage}"
definition:
StartAt: SourceExtractJobMappedTask
States:
# TODO: Handle errors
SourceExtractJobMappedTask:
Type: Map
MaxConcurrency: ${self:custom.stepFunctionConfig.MaxConcurrency}
ItemsPath: $.sources
ResultPath: $.sources
Catch:
- ErrorEquals:
- States.ALL
ResultPath: $.error
Next: SourceExtractJobFailure
Next: SourceExtractJobSuccess
Iterator:
StartAt: SourceExtractJobMapTask
States:
SourceExtractJobMapTask:
Type: Task
Resource:
Fn::GetAtt: [sourceExtractJobMappedTask, Arn]
ResultPath: $.status
End: true
SourceExtractJobSuccess:
Type: Task
Resource:
Fn::GetAtt: [sourceExtractJobSuccess, Arn]
End: true
End: true
SourceExtractJobFailure:
Type: Task
Resource:
Fn::GetAtt: [sourceExtractJobFailure, Arn]
End: true
End: true
resources:
Resources:
AsyncJobTable:
Type: "AWS::DynamoDB::Table"
Properties:
# Make sure to update PynamoDB models
AttributeDefinitions:
- AttributeName: uuid
AttributeType: S
KeySchema:
- AttributeName: uuid
KeyType: HASH
BillingMode: ${self:custom.dynamodbConfig.BillingMode}
ProvisionedThroughput: ${self:custom.dynamodbConfig.ProvisionedThroughput, null}
TableName: ${self:custom.asyncJobTableName}
TimeToLiveSpecification:
AttributeName: ttl
Enabled: true
SourceTable:
Type: "AWS::DynamoDB::Table"
Properties:
# Make sure to update PynamoDB models
AttributeDefinitions:
- AttributeName: key
AttributeType: S
KeySchema:
- AttributeName: key
KeyType: HASH
BillingMode: ${self:custom.dynamodbConfig.BillingMode}
ProvisionedThroughput: ${self:custom.dynamodbConfig.ProvisionedThroughput, null}
TableName: ${self:custom.sourceTableName}
GatewayResponseDefault4XX:
Type: "AWS::ApiGateway::GatewayResponse"
Properties:
ResponseParameters:
gatewayresponse.header.Access-Control-Allow-Origin: "'*'"
gatewayresponse.header.Access-Control-Allow-Headers: "'*'"
ResponseType: DEFAULT_4XX
ResponseTemplates:
application/json: ${file(./src/authorizer/error-template.txt)}
RestApiId:
Ref: "ApiGatewayRestApi"
GatewayResponseDefault5XX:
Type: "AWS::ApiGateway::GatewayResponse"
Properties:
ResponseParameters:
gatewayresponse.header.Access-Control-Allow-Origin: "'*'"
gatewayresponse.header.Access-Control-Allow-Headers: "'*'"
ResponseType: DEFAULT_5XX
ResponseTemplates:
application/json: ${file(./src/authorizer/error-template.txt)}
RestApiId:
Ref: "ApiGatewayRestApi"
Outputs:
SourceExtractStateMachine:
Description: The ARN of the source extraction state machine
Value:
Ref: "SourceExtractStateMachineDash${self:provider.stage}"
layers:
nltk:
path: src/ngrams/nltk
name: NltkData
description: Nltk data
compatibleRuntimes:
- python3.8
plugins:
- serverless-domain-manager
- serverless-python-requirements
- serverless-plugin-include-dependencies
- serverless-step-functions
- serverless-dynamodb-local
- serverless-offline # always last