-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserverless.yml
More file actions
347 lines (324 loc) · 10.7 KB
/
serverless.yml
File metadata and controls
347 lines (324 loc) · 10.7 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
org: mpgxc
app: infra-face-rekognition-sls
service: infra-face-rekognition-sls
configValidationMode: warn
useDotenv: true
plugins:
- serverless-prune-plugin
- serverless-plugin-optimize
- serverless-deployment-bucket
- serverless-offline
provider:
name: aws
runtime: nodejs20.x
stage: ${opt:stage, self:custom.defaultStage}
region: ${env:AWS_REGION_DEFAULT, 'us-east-1'}
stackName: ${self:service}-${self:provider.stage}
deploymentBucket:
name: ${self:service}-${self:provider.region}-deployment-bucket
blockPublicAccess: true
iamRoleStatements:
- Effect: "Allow"
Action:
- "rekognition:DetectFaces"
- "rekognition:IndexFaces"
- "rekognition:ListFaces"
- "rekognition:SearchFaces"
- "rekognition:SearchFacesByImage"
Resource:
- !Sub "arn:aws:rekognition:${AWS::Region}:${AWS::AccountId}:collection/${self:custom.REKOGNITION_COLLECTION}"
- Effect: "Allow"
Action:
- "rekognition:CreateCollection"
- "rekognition:DeleteCollection"
- "rekognition:ListCollections"
Resource:
- !Sub "arn:aws:rekognition:${AWS::Region}:${AWS::AccountId}:collection/*"
- Effect: "Allow"
Action:
- "s3:GetObject"
- "s3:PutObject"
- "s3:DeleteObject"
Resource:
- !Sub "${FaceRecognitionBucket.Arn}/*"
- Effect: "Allow"
Action:
- "dynamodb:PutItem"
- "dynamodb:GetItem"
- "dynamodb:Query"
- "dynamodb:UpdateItem"
- "dynamodb:BatchWriteItem"
- "dynamodb:DeleteItem"
- "dynamodb:TransactWriteItems"
Resource:
- !GetAtt RekognitionBucketAssetsControll.Arn
- !Sub "${RekognitionBucketAssetsControll.Arn}/index/*"
- Effect: "Allow"
Action:
- "s3:PutObject"
Resource:
- !Sub "${FaceRecognitionBucket.Arn}/uploads/incoming/*"
- Effect: "Allow"
Action:
- "sqs:SendMessage"
- "sqs:SendMessageBatch"
Resource:
- !GetAtt FaceRecognitionQueue.Arn
- !GetAtt ImageExtractFaceQueue.Arn
- !GetAtt ImageGenerateThumbnailQueue.Arn
custom:
defaultStage: dev
prune:
automatic: true
number: 2
logRetentionInDays: 7
serverless-offline:
allowCache: true
useChildProcesses: true
httpPort: 4000
REKOGNITION_COLLECTION: ${self:service}-${self:provider.stage}-collection
layers:
sharpLayer:
path: layers/sharp
name: ${self:provider.stage}-sharp
description: A layer for the sharp package
package:
include:
- node_modules/**
compatibleRuntimes:
- nodejs20.x
compatibleArchitectures:
- x86_64
functions:
DetectAndIndexFaces:
handler: build/handlers/picture-index-processing.handler
memorySize: 256
timeout: 120
description: Detects faces and indexes them in the Rekognition collection
maximumRetryAttempts: 0
events:
- sqs:
arn: !GetAtt FaceRecognitionQueue.Arn
batchSize: 20
maximumBatchingWindow: 60 # Espera 60 segundos para juntar mensagens antes de chamar a função lambda
functionResponseType: ReportBatchItemFailures # Retorna um relatório de falhas
environment:
REKOGNITION_COLLECTION: ${self:custom.REKOGNITION_COLLECTION}
IMAGE_PROCESSING_FACE_EXTRACT: !Ref ImageExtractFaceQueue
IMAGE_PROCESSING_THUMBNAIL: !Ref ImageGenerateThumbnailQueue
DYNAMO_TABLE_NAME: !Ref RekognitionBucketAssetsControll
S3_BUCKET_NAME: !Ref FaceRecognitionBucket
ImageExtractFace:
handler: build/handlers/image-extract-face.handler
memorySize: 256
timeout: 240 # 4 minutos
description: Extracts the face from the uploaded image
maximumRetryAttempts: 0
events:
- sqs:
arn: !GetAtt ImageExtractFaceQueue.Arn
batchSize: 1
maximumBatchingWindow: 60
functionResponseType: ReportBatchItemFailures
layers:
- { Ref: SharpLayerLambdaLayer }
environment:
DYNAMO_TABLE_NAME: !Ref RekognitionBucketAssetsControll
S3_BUCKET_NAME: !Ref FaceRecognitionBucket
ImageThumbnailGenerator:
handler: build/handlers/image-thumbnail-generator.handler
memorySize: 256
timeout: 240 # 4 minutos
description: Generates a thumbnail for the uploaded image
maximumRetryAttempts: 0
events:
- sqs:
arn: !GetAtt ImageGenerateThumbnailQueue.Arn
batchSize: 10
maximumBatchingWindow: 60
functionResponseType: ReportBatchItemFailures
layers:
- { Ref: SharpLayerLambdaLayer }
environment:
S3_BUCKET_NAME: !Ref FaceRecognitionBucket
FindSimilarFaces:
handler: build/handlers/picture-search.handler
memorySize: 128
timeout: 10
description: Finds similar faces in the Rekognition collection
maximumRetryAttempts: 0
events:
- httpApi: POST /search
environment:
REKOGNITION_COLLECTION: ${self:custom.REKOGNITION_COLLECTION}
FindSimilarFacesByFaceId:
handler: build/handlers/picture-search.handlerByFaceId
memorySize: 128
timeout: 10
description: Finds similar faces by FaceId in the Rekognition collection
maximumRetryAttempts: 0
events:
- httpApi: POST /search/by-face-id
environment:
REKOGNITION_COLLECTION: ${self:custom.REKOGNITION_COLLECTION}
PictureAlbumManager:
handler: build/handlers/picture-album-manager/picture-album-manager.handler.handler
memorySize: 256
timeout: 30
description: Manages photo albums and Rekognition collections
maximumRetryAttempts: 0
events:
- httpApi:
path: /albums
method: POST
- httpApi:
path: /albums/{externalClientAlbumId}
method: DELETE
- httpApi:
path: /albums/{externalClientAlbumId}
method: GET
- httpApi:
path: /albums/{externalClientAlbumId}/faces
method: GET
- httpApi:
path: /albums/{externalClientAlbumId}/upload-url
method: POST
environment:
DYNAMO_TABLE_NAME: !Ref RekognitionBucketAssetsControll
S3_BUCKET_NAME: !Ref FaceRecognitionBucket
REKOGNITION_COLLECTION: ${self:custom.REKOGNITION_COLLECTION}
FailureNotification:
handler: build/handlers/failure-notification.handler
memorySize: 128
timeout: 10
description: Sends a notification when a message fails to be processed
maximumRetryAttempts: 0
events:
- sqs:
arn: !GetAtt FaceRecognitionQueueDLQ.Arn
batchSize: 1
- sqs:
arn: !GetAtt ImageExtractFaceQueueDLQ.Arn
batchSize: 1
- sqs:
arn: !GetAtt ImageGenerateThumbnailQueueDLQ.Arn
batchSize: 1
environment:
DISCORD_WEBHOOK_URL: ${env:DISCORD_WEBHOOK_URL, ''}
resources:
Resources:
FaceRecognitionBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: ${self:service}-${self:provider.stage}-bucket
AccessControl: Private
BucketEncryption:
ServerSideEncryptionConfiguration:
- ServerSideEncryptionByDefault:
SSEAlgorithm: AES256
# Por algum motivo esta sessão sempre tem de ser deployado por último
NotificationConfiguration:
QueueConfigurations:
- Event: s3:ObjectCreated:*
Queue: !GetAtt FaceRecognitionQueue.Arn
Filter:
S3Key:
Rules:
- Name: prefix
Value: uploads/incoming/
- Name: suffix
Value: .jpg
- Event: s3:ObjectCreated:*
Queue: !GetAtt FaceRecognitionQueue.Arn
Filter:
S3Key:
Rules:
- Name: prefix
Value: uploads/incoming/
- Name: suffix
Value: .jpeg
ImageExtractFaceQueue:
Type: AWS::SQS::Queue
Properties:
QueueName: ${self:service}-${self:provider.stage}-image-extract-face-queue
VisibilityTimeout: 240
ReceiveMessageWaitTimeSeconds: 20
RedrivePolicy:
deadLetterTargetArn: !GetAtt ImageExtractFaceQueueDLQ.Arn
maxReceiveCount: 1
ImageExtractFaceQueueDLQ:
Type: AWS::SQS::Queue
Properties:
QueueName: ${self:service}-${self:provider.stage}-image-extract-face-queue-dlq
VisibilityTimeout: 240
FaceRecognitionQueue:
Type: AWS::SQS::Queue
Properties:
QueueName: ${self:service}-${self:provider.stage}-queue
VisibilityTimeout: 240
ReceiveMessageWaitTimeSeconds: 20
RedrivePolicy:
deadLetterTargetArn: !GetAtt FaceRecognitionQueueDLQ.Arn
maxReceiveCount: 1
FaceRecognitionQueueDLQ:
Type: AWS::SQS::Queue
Properties:
QueueName: ${self:service}-${self:provider.stage}-queue-dlq
VisibilityTimeout: 240
ImageGenerateThumbnailQueue:
Type: AWS::SQS::Queue
Properties:
QueueName: ${self:service}-${self:provider.stage}-generate-thumbnail-queue
VisibilityTimeout: 240
ReceiveMessageWaitTimeSeconds: 20
RedrivePolicy:
deadLetterTargetArn: !GetAtt ImageGenerateThumbnailQueueDLQ.Arn
maxReceiveCount: 1
ImageGenerateThumbnailQueueDLQ:
Type: AWS::SQS::Queue
Properties:
QueueName: ${self:service}-${self:provider.stage}-generate-thumbnail-queue-dlq
VisibilityTimeout: 240
FaceRecognitionQueuePolicy:
Type: AWS::SQS::QueuePolicy
Properties:
PolicyDocument:
Id: SQSQueuePolicy
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal: "*"
Action: sqs:SendMessage
Resource: !GetAtt FaceRecognitionQueue.Arn
Condition:
ArnEquals:
aws:SourceArn: !GetAtt FaceRecognitionBucket.Arn
Queues:
- !Ref FaceRecognitionQueue
FaceRecognitionCollection:
Type: AWS::Rekognition::Collection
Properties:
CollectionId: ${self:service}-${self:provider.stage}-collection
RekognitionBucketAssetsControll:
Type: AWS::DynamoDB::Table
Properties:
TableName: ${self:service}-${self:provider.stage}-rekognition-bucket-assets-controll
BillingMode: PAY_PER_REQUEST
AttributeDefinitions:
- AttributeName: PK
AttributeType: S
- AttributeName: SK
AttributeType: S
KeySchema:
- AttributeName: PK
KeyType: HASH
- AttributeName: SK
KeyType: RANGE
GlobalSecondaryIndexes:
- IndexName: SK-Index
KeySchema:
- AttributeName: SK
KeyType: HASH
Projection:
ProjectionType: ALL
package: ${file(package.yml):package}