-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserverless.yml
616 lines (607 loc) · 21.7 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
604
605
606
607
608
609
610
611
612
613
614
615
616
service: sde
plugins:
- serverless-webpack
- serverless-step-functions
- serverless-stack-output
custom:
stage: ${opt:stage, self:provider.stage}
region: ${opt:region, self:provider.region}
esArn: ${ssm:/${self:service}-${self:custom.stage}-elasticsearch-arn}
esEndpoint: ${ssm:/${self:service}-${self:custom.stage}-elasticsearch-endpoint}
s3BucketArn: ${ssm:/${self:service}-${self:custom.stage}-s3-bucket-arn}
s3BucketName: ${ssm:/${self:service}-${self:custom.stage}-s3-bucket-name}
s3websiteURL: ${ssm:/${self:service}-${self:custom.stage}-s3-bucket-site-url}
subscriptionsTableName: ${self:custom.stage}-${self:service}-subscriptions
webpack:
includeModules: true # enable auto-packing of external modules
output:
handler: build_scripts/output.handler
package:
individually: true
provider:
name: aws
runtime: nodejs10.x
tags:
Project: ${self:service}
Environment: ${self:custom.stage}
stackTags:
Project: ${self:service}
Environment: ${self:custom.stage}
logs:
restApi: true
# you can overwrite defaults here
stage: dev
region: eu-west-1
functions:
pushNotificationOpenConnection:
handler: functions/pushNotificationOpenConnection.handler
description: Notified when client connects to websocket to complete activity
role: pushNotificationOpenConnectionLambdaRole
events:
- websocket:
route: OpenConnection
pushNotificationToClient:
handler: functions/pushNotificationToClient.handler
description: Send report URL to client over websocket
role: pushNotificationToClientLambdaRole
environment:
WEBSOCKET_ENDPOINT_URL:
!Join
- ''
- - 'https://'
- !Ref WebsocketsApi
- '.execute-api.'
- ${self:custom.region}
- '.amazonaws.com/'
- ${self:custom.stage}
downloadRequest:
handler: functions/downloadRequest.handler
description: Triggered when client requests a download
role: downloadRequestLambdaRole
events:
- http:
path: download-request
method: post
cors: true
request:
schema:
application/json: ${file(api_models/DownloadRequestInputModel.json)}
authorizer:
type: COGNITO_USER_POOLS
authorizerId:
Ref: ApiGatewayAuthorizer
environment:
CSV_DOWNLOAD_REQUEST_STEP_FUNCTION_NAME: "${self:service}-${self:custom.stage}-csv-download-request"
CSV_DOWNLOAD_REQUEST_STEP_FUNCTION_ARN:
!Join
- ':'
-
- 'arn:aws:states'
- Ref: 'AWS::Region'
- Ref: 'AWS::AccountId'
- 'stateMachine'
- '${self:service}-${self:custom.stage}-csv-download-request'
OPEN_CONNECTION_ACTIVITY_ARN:
!Join
- ':'
-
- 'arn:aws:states'
- Ref: 'AWS::Region'
- Ref: 'AWS::AccountId'
- 'activity:AwaitOpenConnectionFromClient'
createPostRequest:
handler: functions/createPostRequest.handler
description: Triggered when a client requests to add a new post
role: createPostRequestLambdaRole
events:
- http:
path: posts
method: post
cors: true
request:
schema:
application/json: ${file(api_models/CreatePostRequestInputModel.json)}
authorizer:
type: COGNITO_USER_POOLS
authorizerId:
Ref: ApiGatewayAuthorizer
environment:
CREATE_POST_STEP_FUNCTION_ARN:
!Join
- ':'
-
- 'arn:aws:states'
- Ref: 'AWS::Region'
- Ref: 'AWS::AccountId'
- 'stateMachine'
- '${self:service}-${self:custom.stage}-create-post-request-step-function'
search:
handler: functions/search.handler
description: Performs an ES search and returns the resulting hits
role: searchLambdaRole
events:
- http:
path: search
method: post
cors: true
request:
schema:
application/json: ${file(api_models/SearchInputModel.json)}
authorizer:
type: COGNITO_USER_POOLS
authorizerId:
Ref: ApiGatewayAuthorizer
environment:
ES_SEARCH_API: ${self:custom.esEndpoint}
graphicalReport:
handler: functions/graphicalReport.handler
role: graphicalReportLambdaRole
events:
- http:
path: report/graphical
method: post
cors: true
request:
schema:
application/json: ${file(api_models/ReportInputModel.json)}
authorizer:
type: COGNITO_USER_POOLS
authorizerId:
Ref: ApiGatewayAuthorizer
environment:
ES_SEARCH_API: ${self:custom.esEndpoint}
S3_BUCKET_NAME: ${self:custom.s3BucketName}
hybridReport:
handler: functions/hybridReport.handler
runtime: nodejs8.10
role: hybridReportLambdaRole
events:
- http:
path: report/hybrid
method: post
cors: true
request:
schema:
application/json: ${file(api_models/ReportInputModel.json)}
authorizer:
type: COGNITO_USER_POOLS
authorizerId:
Ref: ApiGatewayAuthorizer
environment:
ES_SEARCH_API: ${self:custom.esEndpoint}
S3_BUCKET_NAME: ${self:custom.s3BucketName}
sendEmail:
handler: functions/sendEmail.handler
description: Sends an email to the specified email address with the report S3 link
role: sendEmailLambdaRole
environment:
EMAIL_SENDER_ADDRESS : "[email protected]"
reportStatus:
handler: functions/reportStatus.handler
description: Returns the execution status of a generate report task
role: reportStatusLambdaRole
events:
- http:
path: report-status
method: post
cors: true
request:
schema:
application/json: ${file(api_models/ReportStatusInputModel.json)}
authorizer:
type: COGNITO_USER_POOLS
authorizerId:
Ref: ApiGatewayAuthorizer
reportGenerator:
handler: functions/reportGenerator.handler
description: Performs an ES search and generates a report in S3
role: reportGeneratorLambdaRole
environment:
ES_SEARCH_API: ${self:custom.esEndpoint}
S3_BUCKET_NAME: ${self:custom.s3BucketName}
createPost:
handler: functions/createPost.handler
description: Save a new post into ES
role: createPostLambdaRole
environment:
ES_SEARCH_API: ${self:custom.esEndpoint}
matchRealTimeDigests:
handler: functions/matchRealTimeDigests.handler
description: Uses ES to match a new post to existing real time subscriptions
role: matchRealTimeDigestsLambdaRole
environment:
ES_SEARCH_API: ${self:custom.esEndpoint}
realTimeDigestEmailIterator:
handler: functions/realTimeDigestEmailIterator.handler
description: Gets the data for the next email to be sent
role: realTimeDigestEmailIteratorLambdaRole
sendDigestEmail:
handler: functions/sendDigestEmail.handler
description: Sends an email to a given user containing posts matching their daily subscriptions
role: sendDigestEmailLambdaRole
environment:
EMAIL_SENDER_ADDRESS: "[email protected]"
USER_POOL_ID:
Ref: CognitoUserPool
WEBSITE_LINK_URL: ${self:custom.s3websiteURL}
createSubscription:
handler: functions/createSubscription.handler
description: Adds a daily subscription for an SDE user
role: createSubscriptionLambdaRole
events:
- http:
path: subscriptions/daily
method: post
cors: true
request:
schema:
application/json: ${file(api_models/CreateSubscriptionInputModel.json)}
authorizer:
type: COGNITO_USER_POOLS
authorizerId:
Ref: ApiGatewayAuthorizer
environment:
SUBSCRIPTIONS_TABLE: ${self:custom.subscriptionsTableName}
deleteSubscription:
handler: functions/deleteSubscription.handler
description: Deletes a specified daily subscription for an SDE user
role: deleteSubscriptionLambdaRole
events:
- http:
path: subscriptions/daily
method: delete
cors: true
request:
schema:
application/json: ${file(api_models/DeleteSubscriptionInputModel.json)}
authorizer:
type: COGNITO_USER_POOLS
authorizerId:
Ref: ApiGatewayAuthorizer
environment:
SUBSCRIPTIONS_TABLE: ${self:custom.subscriptionsTableName}
getUserSubscriptions:
handler: functions/getUserSubscriptions.handler
description: Gets the daily subscriptions for an SDE user
role: getUserSubscriptionsLambdaRole
events:
- http:
path: subscriptions/daily
method: get
cors: true
authorizer:
type: COGNITO_USER_POOLS
authorizerId:
Ref: ApiGatewayAuthorizer
environment:
SUBSCRIPTIONS_TABLE: ${self:custom.subscriptionsTableName}
createRealTimeSubscription:
handler: functions/createRealTimeSubscription.handler
description: Adds a real time subscription for an SDE user
role: createRealTimeSubscriptionLambdaRole
events:
- http:
path: subscriptions/real-time
method: post
cors: true
request:
schema:
application/json: ${file(api_models/CreateRealTimeSubscriptionInputModel.json)}
authorizer:
type: COGNITO_USER_POOLS
authorizerId:
Ref: ApiGatewayAuthorizer
environment:
ES_SEARCH_API: ${self:custom.esEndpoint}
deleteRealTimeSubscription:
handler: functions/deleteRealTimeSubscription.handler
description: Deletes a specified real time subscription for an SDE user
role: deleteRealTimeSubscriptionLambdaRole
events:
- http:
path: subscriptions/real-time
method: delete
cors: true
request:
schema:
application/json: ${file(api_models/DeleteRealTimeSubscriptionInputModel.json)}
authorizer:
type: COGNITO_USER_POOLS
authorizerId:
Ref: ApiGatewayAuthorizer
environment:
ES_SEARCH_API: ${self:custom.esEndpoint}
getUserRealTimeSubscriptions:
handler: functions/getUserRealTimeSubscriptions.handler
description: Gets the real time subscriptions for an SDE user
role: getUserRealTimeSubscriptionsLambdaRole
events:
- http:
path: subscriptions/real-time
method: get
cors: true
authorizer:
type: COGNITO_USER_POOLS
authorizerId:
Ref: ApiGatewayAuthorizer
environment:
ES_SEARCH_API: ${self:custom.esEndpoint}
scheduledDailyDigests:
handler: functions/scheduledDailyDigests.handler
description: Runs daily, invokes the calculateUserDailyDigest function for each user with daily subscriptions
role: scheduledDailyDigestsLambdaRole
events:
- schedule:
name: ${self:service}-${self:custom.stage}-scheduled-daily-digests
description: 'A daily event to trigger the sendDailyDigests function'
rate: cron(0 1 * * ? *)
environment:
SUBSCRIPTIONS_TABLE: ${self:custom.subscriptionsTableName}
USER_DIGEST_LAMBDA_NAME: ${self:service}-${self:custom.stage}-calculateUserDailyDigest
calculateUserDailyDigest:
handler: functions/calculateUserDailyDigest.handler
description: Calculates and collates previous day's new posts which matching the user's daily subscriptions
role: calculateUserDailyDigestLambdaRole
environment:
ES_SEARCH_API: ${self:custom.esEndpoint}
EMAIL_MAX_POSTS: 50
SEND_DIGEST_EMAIL_LAMBDA_NAME: ${self:service}-${self:custom.stage}-sendDigestEmail
stepFunctions:
stateMachines:
csvDownloadRequest:
name: "${self:service}-${self:custom.stage}-csv-download-request"
definition:
Comment: "My test step function"
StartAt: "Choose download type"
States:
"Choose download type":
Type: Choice
Choices:
-
Variable: "$.type"
StringEquals: email
Next: "Email report request"
-
Variable: "$.type"
StringEquals: direct
Next: "Direct download of report"
-
Variable: "$.type"
StringEquals: push
Next: "Push notification report request"
"Email report request":
Type: Pass
Next: "Generate CSV Report for email"
"Generate CSV Report for email":
Type: Task
Resource:
Fn::GetAtt: [reportGenerator, Arn]
ResultPath: "$.reportOutput"
Next: "Email CSV Report link to user"
"Email CSV Report link to user":
Type: Task
Resource: "arn:aws:states:::lambda:invoke"
Parameters:
FunctionName:
Fn::GetAtt: [sendEmail, Arn]
Payload:
email:
"to.$": "$.parameters.emailAddress"
subject: "CSV Email report"
"reportURL.$": "$.reportOutput.reportURL"
End: true
"Direct download of report":
Type: Pass
Next: "Generate CSV Report for direct download"
"Generate CSV Report for direct download":
Type: Task
Resource:
Fn::GetAtt: [reportGenerator, Arn]
ResultPath: "$.reportOutput"
OutputPath: "$.reportOutput"
End: true
"Push notification report request":
Type: Pass
Next: "Push notification parallel"
"Push notification parallel":
Type: Parallel
Next: "Push Report URL To Client"
Branches:
-
StartAt: "Generate CSV Report for push notification"
States:
"Generate CSV Report for push notification":
Type: Task
Resource:
Fn::GetAtt: [reportGenerator, Arn]
ResultPath: "$.reportOutput"
OutputPath: "$.reportOutput"
End: true
-
StartAt: "Await OpenConnection from Client"
States:
"Await OpenConnection from Client":
Type: Task
Resource:
!Join
- ':'
-
- 'arn:aws:states'
- Ref: 'AWS::Region'
- Ref: 'AWS::AccountId'
- 'activity:AwaitOpenConnectionFromClient'
End: true
"Push Report URL To Client":
Type: Task
Resource:
Fn::GetAtt: [pushNotificationToClient, Arn]
End: true
createPostRequestStepFunction:
name: "${self:service}-${self:custom.stage}-create-post-request-step-function"
definition:
Comment: "Create post and send real-time notifications for matching searches"
StartAt: "Create post"
States:
"Create post":
Type: Task
Resource:
Fn::GetAtt: [createPost, Arn]
Next: "Find matching real-time posts"
"Find matching real-time posts":
Type: Task
Resource:
Fn::GetAtt: [matchRealTimeDigests, Arn]
Next: "Get next email to send"
"Get next email to send":
Type: Task
Resource:
Fn::GetAtt: [realTimeDigestEmailIterator, Arn]
Next: "Check all emails sent"
"Check all emails sent":
Type: Choice
Choices:
-
Variable: "$.shouldContinue"
BooleanEquals: true
Next: "Send email"
Default: "Done"
"Send email":
Type: Task
Resource:
Fn::GetAtt: [sendDigestEmail, Arn]
InputPath: "$.data"
ResultPath: null
Next: "Get next email to send"
"Done":
Type: Pass
End: true
activities:
- AwaitOpenConnectionFromClient
resources:
- Resources:
CognitoUserPool:
Type: AWS::Cognito::UserPool # https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-cognito-userpool.html
Properties:
UserPoolName: ${self:custom.stage}-${self:service}-users-pool
UsernameAttributes:
- email
AutoVerifiedAttributes:
- email
CognitoUserPoolClient:
Type: AWS::Cognito::UserPoolClient # https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-cognito-userpoolclient.html
Properties:
ClientName: ${self:custom.stage}-${self:service}-user-pool-client
UserPoolId:
Ref: CognitoUserPool
GenerateSecret: false
CognitoIdentityPool:
Type: AWS::Cognito::IdentityPool # https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-cognito-identitypool.html
Properties:
IdentityPoolName: ${self:custom.stage}SdeIdentityPool
AllowUnauthenticatedIdentities: false
CognitoIdentityProviders:
- ClientId:
Ref: CognitoUserPoolClient
ProviderName:
Fn::GetAtt: [ "CognitoUserPool", "ProviderName" ]
ApiGatewayAuthorizer: # https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigateway-authorizer.html
DependsOn:
- ApiGatewayRestApi
Type: AWS::ApiGateway::Authorizer
Properties:
Name: ${self:custom.stage}-${self:service}-api-authorizer
IdentitySource: method.request.header.Authorization
RestApiId:
Ref: ApiGatewayRestApi
Type: COGNITO_USER_POOLS
ProviderARNs:
- Fn::GetAtt: [CognitoUserPool, Arn]
SubscriptionsTable:
Type: AWS::DynamoDB::Table # https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-dynamodb-table.html
Properties:
TableName: ${self:custom.subscriptionsTableName}
AttributeDefinitions:
- AttributeName: userId
AttributeType: S
KeySchema:
- AttributeName: userId
KeyType: HASH
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
CognitoIdentityPoolRoles:
Type: AWS::Cognito::IdentityPoolRoleAttachment
Properties:
IdentityPoolId:
Ref: CognitoIdentityPool
Roles:
authenticated:
Fn::GetAtt: [CognitoAuthRole, Arn]
CognitoAuthRole:
Type: AWS::IAM::Role
Properties:
Path: /
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: 'Allow'
Principal:
Federated: 'cognito-identity.amazonaws.com'
Action:
- 'sts:AssumeRoleWithWebIdentity'
Condition:
StringEquals:
'cognito-identity.amazonaws.com:aud':
Ref: CognitoIdentityPool
'ForAnyValue:StringLike':
'cognito-identity.amazonaws.com:amr': authenticated
Policies:
- PolicyName: 'CognitoAuthorizedPolicy'
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: 'Allow'
Action:
- 'cognito-sync:*'
- 'cognito-identity:*'
Resource: '*'
GatewayResponseDefault4XX:
Type: 'AWS::ApiGateway::GatewayResponse'
Properties:
ResponseParameters:
gatewayresponse.header.Access-Control-Allow-Origin: "'*'"
gatewayresponse.header.Content-Type: "'application/json'"
ResponseType: DEFAULT_4XX
RestApiId:
Ref: 'ApiGatewayRestApi'
GatewayResponseDefault5XX:
Type: 'AWS::ApiGateway::GatewayResponse'
Properties:
ResponseParameters:
gatewayresponse.header.Access-Control-Allow-Origin: "'*'"
gatewayresponse.header.Content-Type: "'application/json'"
ResponseType: DEFAULT_5XX
RestApiId:
Ref: 'ApiGatewayRestApi'
- ${file(iam-role-resources/pushNotificationOpenConnection.yml)}
- ${file(iam-role-resources/pushNotificationToClient.yml)}
- ${file(iam-role-resources/downloadRequest.yml)}
- ${file(iam-role-resources/createPostRequest.yml)}
- ${file(iam-role-resources/search.yml)}
- ${file(iam-role-resources/graphicalReport.yml)}
- ${file(iam-role-resources/hybridReport.yml)}
- ${file(iam-role-resources/sendEmail.yml)}
- ${file(iam-role-resources/reportStatus.yml)}
- ${file(iam-role-resources/reportGenerator.yml)}
- ${file(iam-role-resources/createPost.yml)}
- ${file(iam-role-resources/matchRealTimeDigests.yml)}
- ${file(iam-role-resources/realTimeDigestEmailIterator.yml)}
- ${file(iam-role-resources/sendDigestEmail.yml)}
- ${file(iam-role-resources/createSubscription.yml)}
- ${file(iam-role-resources/deleteSubscription.yml)}
- ${file(iam-role-resources/getUserSubscriptions.yml)}
- ${file(iam-role-resources/createRealTimeSubscription.yml)}
- ${file(iam-role-resources/deleteRealTimeSubscription.yml)}
- ${file(iam-role-resources/getUserRealTimeSubscriptions.yml)}
- ${file(iam-role-resources/scheduledDailyDigests.yml)}
- ${file(iam-role-resources/calculateUserDailyDigest.yml)}