-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathserverless.yml
335 lines (303 loc) · 8.99 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
frameworkVersion: '>=1.46.1'
service:
name: ${self:provider.application}
plugins:
- serverless-plugin-typescript
- serverless-dotenv-plugin
custom:
dotenv:
include:
- GETSTREAM_KEY
- GETSTREAM_APPID
provider:
application: ${env:APPLICATION, "beenion"}
stage: ${env:STAGE, "dev"}
name: aws
region: ${env:AWS_REGION, us-east-1}
runtime: nodejs12.x
timeout: 30
versionFunctions: false
variableSyntax: "\\${((?!AWS)[ ~:a-zA-Z0-9._'\",\\-\\/\\(\\)]+?)}"
environment:
EVENTSTORE_TABLE: !Ref EventStoreTable
LINKS_TABLE: !Ref LinksTable
GETSTREAM_QUEUE: !Ref GetStreamQueue
COSMOS_QUEUE: !Ref CosmosQueue
# Create this key in SSM with getstream.io secret
GETSTREAM_SECRET: ${ssm:/${self:provider.application}/${self:provider.stage}/getstreamSecret}
iamRoleStatements:
# Dynamo Event Store Permissions
- Effect: Allow
Action:
- dynamodb:Query
- dynamodb:GetItem
- dynamodb:PutItem
Resource:
- !GetAtt EventStoreTable.Arn
# Dynamp LinkTable Permissions
# Dynamo Caches
- Effect: Allow
Action:
- dynamodb:Query
- dynamodb:GetItem
- dynamodb:PutItem
Resource:
- !GetAtt LinksTable.Arn
# SQS Same Account
# Required by dynamo stream handler
- Effect: Allow
Action:
- sqs:*
Resource:
# Sub style prevents circular reference
- !Sub arn:aws:sqs:${AWS::Region}:${AWS::AccountId}:*
- Effect: Allow
Action:
- cognito-idp:ListUsers
- cognito-idp:AdminGetUser
Resource:
# Sub style prevents circular reference
- !Sub arn:aws:cognito-idp:${AWS::Region}:${AWS::AccountId}:userpool/*
functions:
# Invoke via lambda api from sqspoller.ts
ec2ToEventHandlers: #should be renamed to lambdaToEventHandlers
handler: infrastructure/eventHandlers/lambda/lambdaToEventHandlers.handler
dynamoStreamToSQS:
handler: infrastructure/eventHandlers/dynamodbStream/dynamoStreamToSQS.handler
description: 'Streams Dynamo EventStore Events to SQS FIFO queues'
events:
- stream:
type: dynamodb
startingPosition: LATEST
batchSize: 1 # TODO: Lambda Handler only looks at first Record
enabled: true
arn: !GetAtt EventStoreTable.StreamArn
# Query Handlers
getFeed:
handler: ports/http-query/getFeed.handler
environment:
POOL_ID: !Ref CognitoUserPoolBeenion
events:
- http:
cors: true
path: feed
method: get
authorizer:
type: COGNITO_USER_POOLS
authorizerId: !Ref CognitoAuthorizer
getFeedPerUser:
handler: ports/http-query/getFeedPerUser.handler
environment:
POOL_ID: !Ref CognitoUserPoolBeenion
events:
- http:
cors: true
path: userfeed/{username}
method: get
authorizer:
type: COGNITO_USER_POOLS
authorizerId: !Ref CognitoAuthorizer
getLinkRatings:
handler: ports/http-query/getLinkRatings.handler
environment:
POOL_ID: !Ref CognitoUserPoolBeenion
events:
- http:
cors: true
path: linkratings
method: get
authorizer:
type: COGNITO_USER_POOLS
authorizerId: !Ref CognitoAuthorizer
checkLast:
handler: ports/http-query/checkLast.handler
environment:
POOL_ID: !Ref CognitoUserPoolBeenion
events:
- http:
cors: true
path: checklast
method: get
authorizer:
type: COGNITO_USER_POOLS
authorizerId: !Ref CognitoAuthorizer
listUsers:
handler: ports/http-query/listUsers.handler
environment:
POOL_ID: !Ref CognitoUserPoolBeenion
events:
- http:
cors: true
path: listUsers
method: get
authorizer:
type: COGNITO_USER_POOLS
authorizerId: !Ref CognitoAuthorizer
getUserData:
handler: ports/http-query/getUserData.handler
environment:
POOL_ID: !Ref CognitoUserPoolBeenion
events:
- http:
cors: true
path: getUserData
method: get
authorizer:
type: COGNITO_USER_POOLS
authorizerId: !Ref CognitoAuthorizer
# Command Handlers
linkCommand:
handler: ports/http-command/linkCommand.handler
environment:
POOL_ID: !Ref CognitoUserPoolBeenion
events:
- http:
cors: true
path: linkCommand
method: post
authorizer:
type: COGNITO_USER_POOLS
authorizerId: !Ref CognitoAuthorizer
userCommand:
handler: ports/http-command/userCommand.handler
environment:
POOL_ID: !Ref CognitoUserPoolBeenion
events:
- http:
cors: true
path: userCommand
method: post
authorizer:
type: COGNITO_USER_POOLS
authorizerId: !Ref CognitoAuthorizer
postConfirm:
handler: infrastructure/authentication/postConfirm.handler
description: Triggers follow user commandHandler on signup
timeout: 5 # Maximum
events:
- cognitoUserPool:
pool: Beenion
trigger: PostConfirmation
resources:
Resources:
CognitoAuthorizer:
Type: AWS::ApiGateway::Authorizer
Properties:
AuthorizerResultTtlInSeconds: 300
IdentitySource: method.request.header.Authorization
Name: CognitoAuthorizer
RestApiId: !Ref ApiGatewayRestApi
Type: COGNITO_USER_POOLS
ProviderARNs:
- !GetAtt CognitoUserPoolBeenion.Arn
###################################
CognitoUserPoolBeenion:
Type: AWS::Cognito::UserPool
Properties:
UserPoolName: ${self:provider.application}-${self:provider.stage}
AutoVerifiedAttributes:
- email
UsernameAttributes:
- email
Schema:
- AttributeDataType: String
DeveloperOnlyAttribute: false
Mutable: true
Name: email
Required: true
UserPoolClient:
Type: AWS::Cognito::UserPoolClient
Properties:
ExplicitAuthFlows:
- ADMIN_NO_SRP_AUTH
- USER_PASSWORD_AUTH
GenerateSecret: false
UserPoolId: !Ref CognitoUserPoolBeenion
RefreshTokenValidity: 7
GatewayResponseDefault4XX:
Type: 'AWS::ApiGateway::GatewayResponse'
Properties:
ResponseParameters:
gatewayresponse.header.Access-Control-Allow-Origin: "'*'"
gatewayresponse.header.Access-Control-Allow-Headers: "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'"
gatewayresponse.header.Access-Control-Allow-Methods: "'GET,OPTIONS'"
ResponseType: DEFAULT_4XX
RestApiId: !Ref ApiGatewayRestApi
EventStoreTable:
Type: AWS::DynamoDB::Table
Properties:
AttributeDefinitions:
- AttributeName: streamId
AttributeType: S
- AttributeName: version
AttributeType: N
- AttributeName: active
AttributeType: N
- AttributeName: committedAt
AttributeType: N
KeySchema:
- AttributeName: streamId
KeyType: HASH
- AttributeName: version
KeyType: RANGE
GlobalSecondaryIndexes:
- IndexName: active-committedAt-index
KeySchema:
- AttributeName: active
KeyType: HASH
- AttributeName: committedAt
KeyType: RANGE
Projection:
ProjectionType: ALL
BillingMode: PAY_PER_REQUEST
TimeToLiveSpecification:
AttributeName: ttl
Enabled: true
StreamSpecification:
StreamViewType: NEW_IMAGE
LinksTable:
Type: AWS::DynamoDB::Table
Properties:
AttributeDefinitions:
- AttributeName: linkId
AttributeType: S
KeySchema:
- AttributeName: linkId
KeyType: HASH
BillingMode: PAY_PER_REQUEST
TimeToLiveSpecification:
AttributeName: ttl
Enabled: true
CosmosQueue:
Type: AWS::SQS::Queue
Properties:
ContentBasedDeduplication: true
FifoQueue: true
GetStreamQueue:
Type: AWS::SQS::Queue
Properties:
ContentBasedDeduplication: true
FifoQueue: true
QueuePolicy:
Type: AWS::SQS::QueuePolicy
Properties:
Queues:
- !Ref CosmosQueue
- !Ref GetStreamQueue
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
AWS: !Sub ${AWS::AccountId}
Action:
- sqs:SendMessage
Resource:
- !GetAtt CosmosQueue.Arn
- Effect: Allow
Principal:
AWS: !Sub ${AWS::AccountId}
Action:
- sqs:SendMessage
Resource:
- !GetAtt GetStreamQueue.Arn