-
Notifications
You must be signed in to change notification settings - Fork 7
/
template.yaml
465 lines (436 loc) · 12.1 KB
/
template.yaml
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
AWSTemplateFormatVersion: "2010-09-09"
Transform: "AWS::Serverless-2016-10-31"
Parameters:
environment:
Type: String
Default: development
logLevel:
Type: String
Default: debug
AllowedValues:
- debug
- info
- warn
- error
stage:
Type: String
Default: dev
AllowedValues:
- dev
- stage
- prod
eventLogTable:
Type: String
eventStream:
Type: String
setValidationsTable:
Type: String
projectionUsersTable:
Type: String
projectionRolesTable:
Type: String
Globals:
Function:
Runtime: nodejs12.x
MemorySize: 512
Timeout: 30
Tags:
Application: Serverless API Demo
Environment:
Variables:
environment: !Ref environment
logLevel: !Ref logLevel
stage: !Ref stage
eventSourceLog: !Ref eventLogTable
eventSourceStream: !Ref eventStream
setValidationsTable: !Ref setValidationsTable
projectionUsersTable: !Ref projectionUsersTable
projectionRolesTable: !Ref projectionRolesTable
Resources:
# API Gateway
ServerlessSamApi:
Type: AWS::Serverless::Api
Properties:
StageName: !Ref stage
Cors:
AllowMethods: "'GET,POST,PUT,PATCH,DELETE,OPTIONS'"
AllowHeaders: "'authorization'"
AllowOrigin: "'*'"
OpenApiVersion: '3.0'
###
### /users endpoints
###
# GET /users
ListUsersController:
Type: "AWS::Serverless::Function"
Properties:
Handler: user-controller.listHandler
CodeUri: src/iam/user/controllers
AutoPublishAlias: live
Policies:
- DynamoDBReadPolicy:
TableName: !Ref projectionUsersTable
Events:
UserApi:
Type: Api
Properties:
Path: /users
Method: GET
RestApiId: !Ref ServerlessSamApi
# GET /users/{id}
GetUserController:
Type: "AWS::Serverless::Function"
Properties:
Handler: user-controller.getHandler
CodeUri: src/iam/user/controllers
AutoPublishAlias: live
Policies:
- DynamoDBReadPolicy:
TableName: !Ref projectionUsersTable
Events:
UserApi:
Type: Api
Properties:
Path: /users/{id}
Method: GET
RestApiId: !Ref ServerlessSamApi
# POST /users
CreateUserController:
Type: "AWS::Serverless::Function"
Properties:
Handler: user-controller.postHandler
CodeUri: src/iam/user/controllers
AutoPublishAlias: live
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref eventLogTable
- DynamoDBCrudPolicy:
TableName: !Ref setValidationsTable
Events:
UserApi:
Type: Api
Properties:
Path: /users
Method: POST
RestApiId: !Ref ServerlessSamApi
# PUT /users/{id}
UpdateUserController:
Type: "AWS::Serverless::Function"
Properties:
Handler: user-controller.putHandler
CodeUri: src/iam/user/controllers
AutoPublishAlias: live
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref eventLogTable
- DynamoDBCrudPolicy:
TableName: !Ref setValidationsTable
Events:
UserApi:
Type: Api
Properties:
Path: /users/{id}
Method: PUT
RestApiId: !Ref ServerlessSamApi
# PUT /users/{id}/disable
DisableUserController:
Type: "AWS::Serverless::Function"
Properties:
Handler: user-controller.disableHandler
CodeUri: src/iam/user/controllers
AutoPublishAlias: live
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref eventLogTable
- DynamoDBCrudPolicy:
TableName: !Ref setValidationsTable
Events:
UserApi:
Type: Api
Properties:
Path: /users/{id}/disable
Method: PUT
RestApiId: !Ref ServerlessSamApi
# POST /users/{id}/roles
AddUserRoleController:
Type: "AWS::Serverless::Function"
Properties:
Handler: user-controller.addRoleHandler
CodeUri: src/iam/user/controllers
AutoPublishAlias: live
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref eventLogTable
- DynamoDBCrudPolicy:
TableName: !Ref setValidationsTable
Events:
UserApi:
Type: Api
Properties:
Path: /users/{id}/roles
Method: POST
RestApiId: !Ref ServerlessSamApi
# DELETE /users/{id}/roles/{roleId}
RemoveUserRoleController:
Type: "AWS::Serverless::Function"
Properties:
Handler: user-controller.removeRoleHandler
CodeUri: src/iam/user/controllers
AutoPublishAlias: live
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref eventLogTable
- DynamoDBCrudPolicy:
TableName: !Ref setValidationsTable
Events:
UserApi:
Type: Api
Properties:
Path: /users/{id}/roles/{roleId}
Method: DELETE
RestApiId: !Ref ServerlessSamApi
###
### /roles endpoints
###
# GET /roles
ListRolesController:
Type: "AWS::Serverless::Function"
Properties:
Handler: role-controller.listHandler
CodeUri: src/iam/role/controllers
AutoPublishAlias: live
Policies:
- DynamoDBReadPolicy:
TableName: !Ref projectionRolesTable
Events:
RoleApi:
Type: Api
Properties:
Path: /roles
Method: GET
RestApiId: !Ref ServerlessSamApi
# GET /roles/{id}
GetRoleController:
Type: "AWS::Serverless::Function"
Properties:
Handler: role-controller.getHandler
CodeUri: src/iam/role/controllers
AutoPublishAlias: live
Policies:
- DynamoDBReadPolicy:
TableName: !Ref projectionRolesTable
Events:
RoleApi:
Type: Api
Properties:
Path: /roles/{id}
Method: GET
RestApiId: !Ref ServerlessSamApi
# POST /roles
CreateRoleController:
Type: "AWS::Serverless::Function"
Properties:
Handler: role-controller.postHandler
CodeUri: src/iam/role/controllers
AutoPublishAlias: live
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref eventLogTable
- DynamoDBCrudPolicy:
TableName: !Ref setValidationsTable
Events:
RoleApi:
Type: Api
Properties:
Path: /roles
Method: POST
RestApiId: !Ref ServerlessSamApi
# PUT /roles/{id}/disable
DisableRoleController:
Type: "AWS::Serverless::Function"
Properties:
Handler: role-controller.disableHandler
CodeUri: src/iam/role/controllers
AutoPublishAlias: live
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref eventLogTable
- DynamoDBCrudPolicy:
TableName: !Ref setValidationsTable
Events:
RoleApi:
Type: Api
Properties:
Path: /roles/{id}/disable
Method: PUT
RestApiId: !Ref ServerlessSamApi
# Append only Dynamo table
EventLog:
Type: "AWS::DynamoDB::Table"
Properties:
TableName: !Ref eventLogTable
AttributeDefinitions:
- AttributeName: id
AttributeType: S
- AttributeName: $version
AttributeType: N
KeySchema:
- AttributeName: id
KeyType: HASH
- AttributeName: $version
KeyType: RANGE
ProvisionedThroughput:
ReadCapacityUnits: 5
WriteCapacityUnits: 5
StreamSpecification:
StreamViewType: NEW_IMAGE
# Table for Set Validations
SetValidations:
Type: "AWS::Serverless::SimpleTable"
Properties:
TableName: !Ref setValidationsTable
PrimaryKey:
Name: constraint
Type: String
ProvisionedThroughput:
ReadCapacityUnits: 5
WriteCapacityUnits: 5
# User Projection
ProjectionUsers:
Type: "AWS::Serverless::SimpleTable"
Properties:
TableName: !Ref projectionUsersTable
ProvisionedThroughput:
ReadCapacityUnits: 5
WriteCapacityUnits: 5
# Role Projection
ProjectionRoles:
Type: "AWS::Serverless::SimpleTable"
Properties:
TableName: !Ref projectionRolesTable
ProvisionedThroughput:
ReadCapacityUnits: 5
WriteCapacityUnits: 5
# Stream Dynamo insert events to Kinesis stream
DynamoKinesisAdaptor:
Type: AWS::Serverless::Function
Properties:
Handler: dynamo-kinesis-adaptor.handler
CodeUri: src/event-store/
Policies:
- DynamoDBStreamReadPolicy:
TableName: !Ref eventLogTable
StreamName: !Select [2, !Split ["/", !GetAtt EventLog.StreamArn]]
- KinesisCrudPolicy:
StreamName: !Ref eventStream
AutoPublishAlias: live
Events:
Stream:
Type: DynamoDB
Properties:
Stream: !GetAtt EventLog.StreamArn
BatchSize: 100
StartingPosition: LATEST
# Kinesis Stream with EventSource Lambda broker
EventSourceStream:
Type: AWS::Kinesis::Stream
Properties:
Name: !Ref eventStream
ShardCount: 1
# TODO: Firehose => S3 Backup of stream
StreamConsumer:
Type: "AWS::Kinesis::StreamConsumer"
Properties:
StreamARN: !GetAtt EventSourceStream.Arn
ConsumerName: !Ref EventBroker
EventBroker:
Type: "AWS::Serverless::Function"
Properties:
Handler: event-source-broker.handler
CodeUri: src/event-store/brokers/
AutoPublishAlias: live
Policies:
- KinesisStreamReadPolicy:
StreamName: !Ref eventStream
- SQSSendMessagePolicy:
QueueName: !GetAtt IamUserQueue.QueueName
- SQSSendMessagePolicy:
QueueName: !GetAtt IamRoleQueue.QueueName
- Statement:
- Action:
- sqs:ListQueues
Effect: Allow
Resource: "arn:aws:sqs:*"
Events:
Stream:
Type: Kinesis
Properties:
Stream: !GetAtt StreamConsumer.ConsumerARN
StartingPosition: LATEST
BatchSize: 100
# Create Queue and handler Lambda for each aggregate. These handlers will create the projections.
IamUserQueue:
Type: AWS::SQS::Queue
Properties:
FifoQueue: true
IamRoleQueue:
Type: AWS::SQS::Queue
Properties:
FifoQueue: true
# User queue processor
IamUserQueueProcessor:
Type: AWS::Serverless::Function
Properties:
Handler: queue-broker.handler
CodeUri: src/event-store/brokers/
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref projectionUsersTable
- DynamoDBCrudPolicy:
TableName: !Ref projectionRolesTable
AutoPublishAlias: live
Events:
IamUserQueueEvent:
Type: SQS
Properties:
Queue: !GetAtt IamUserQueue.Arn
BatchSize: 10
# Role queue processor
IamRoleQueueProcessor:
Type: AWS::Serverless::Function
Properties:
Handler: queue-broker.handler
CodeUri: src/event-store/brokers/
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref projectionRolesTable
- DynamoDBCrudPolicy:
TableName: !Ref projectionUsersTable
AutoPublishAlias: live
Events:
IamRolesQueueEvent:
Type: SQS
Properties:
Queue: !GetAtt IamRoleQueue.Arn
BatchSize: 10
Outputs:
ApiUrl:
Description: The target URL of the created API
Value: !Sub "https://${ServerlessSamApi}.execute-api.${AWS::Region}.amazonaws.com/${stage}/"
Export:
Name: !Sub "${stage}-api-url"
EventLogARN:
Description: "Event Log ARN"
Value: !GetAtt EventLog.Arn
EventLogStreamARN:
Description: "Event Log Streamn ARN"
Value: !GetAtt EventLog.StreamArn
EventSourceStreamARN:
Description: "Event Source Stream ARN"
Value: !GetAtt EventSourceStream.Arn
IamUserQueueARN:
Description: "Iam User Queue ARN"
Value: !GetAtt IamUserQueue.Arn
IamRoleQueueARN:
Description: "Iam Role Queue ARN"
Value: !GetAtt IamRoleQueue.Arn