-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserverless.yml
552 lines (539 loc) · 17 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
service:
name: colonies
frameworkVersion: '>=1.44.1 <2.0.0'
plugins:
- serverless-webpack
- serverless-iam-roles-per-function
- serverless-pseudo-parameters
# - serverless-domain-manager
- serverless-plugin-custom-domain
# - serverless-cloudformation-sub-variables
custom:
domain: "colonies-api.andrhamm.com"
serverless-iam-roles-per-function:
defaultInherit: true
# customDomain:
# domainName: colonies-api.andrhamm.com
# stage: ${self:provider.stage}
# basePath: ''
# certificateName: '*.andrhamm.com'
# createRoute53Record: true
# endpointType: 'regional'
webpack:
webpackConfig: ./webpack.config.js
includeModules:
forceExclude:
- aws-sdk
dynamodb_table_name_games: ${self:service.name}-${self:provider.stage}-games-3
dynamodb_index_name_games_id: gameId
dynamodb_index_name_games_opponent: opponent
dynamodb_table_name_users: ${self:service.name}-${self:provider.stage}-users
# dynamodb_index_name_games_room: ${self:custom.dynamodb_table_name_games}-by-room
arn_prefix_lambda_function: arn:aws:lambda:#{AWS::Region}:#{AWS::AccountId}:function:${self:service.name}-${self:provider.stage}
arn_api_gateway_connections:
Fn::Join:
- "/"
- - Fn::Join: [":", ["arn:aws:execute-api", {"Ref":"AWS::Region"}, {"Ref":"AWS::AccountId"}, {"Ref": "WebsocketsApi"}]]
- ${self:provider.stage}
- "*"
- "@connections"
- "*"
cognito_user_pool_arn:
Fn::Join:
- ''
- - 'arn:aws:cognito-idp:'
- ${self:provider.region}
- ':'
- Ref: AWS::AccountId
- ':userpool/'
- Ref: CognitoUserPool
provider:
name: aws
runtime: nodejs8.10
stage: ${opt:stage, 'dev'}
profile: ${opt:profile, 'tremorlab'}
region: us-east-1
logs:
restApi: true
# websocket: true
websocketsApiRouteSelectionExpression: $request.body.action
environment:
# NOTE: this is useful for local invocations and has no affect in deployed environments
AWS_PROFILE: ${self:provider.profile}
API_BASE:
Fn::Join:
- ""
- - Ref: ApiGatewayRestApi
- ".execute-api.${self:provider.region}."
- Ref: AWS::URLSuffix
- "/${self:provider.stage}"
WSS_BASE:
Fn::Join:
- ""
- - Ref: WebsocketsApi
- ".execute-api.${self:provider.region}."
- Ref: AWS::URLSuffix
- "/${self:provider.stage}"
ENCRYPTION_KEY: ${file(./secrets.${self:provider.stage}.yml):encryptionKey}
SIGNING_KEY: ${file(./secrets.${self:provider.stage}.yml):signingKey}
FUNCTION_SHIELD_TOKEN: ${file(./secrets.${self:provider.stage}.yml):functionShieldToken}
DYNAMODB_PARTITION_COUNT_GAMES: 4
DYNAMODB_TABLE_NAME_GAMES: ${self:custom.dynamodb_table_name_games}
DYNAMODB_INDEX_NAME_GAMES_ID: ${self:custom.dynamodb_index_name_games_id}
DYNAMODB_INDEX_NAME_GAMES_OPPONENT: ${self:custom.dynamodb_index_name_games_opponent}
DYNAMODB_TABLE_NAME_USERS: ${self:custom.dynamodb_table_name_users}
COGNITO_USER_POOL_ID: !Ref CognitoUserPool
SES_EMAIL_DOMAIN: ${self:custom.domain}
iamRoleStatements:
- Effect: Allow
Action:
- cognito-idp:ListUsers
- cognito-idp:AdminGetUser
Resource:
- ${self:custom.cognito_user_pool_arn}
package:
individually: true
exclude:
- secrets.*.yml
functions:
getWebsocketConnectToken:
handler: handlers/websockets-auth.getToken
description: Retreive a token that can be used to authenticate the websocket connection
events:
- http:
path: token
method: get
cors: ${file(api/cors.yml)}
authorizer: aws_iam
websocketDefault:
handler: handlers/websockets.routeDefault
events:
- websocket: $default
iamRoleStatements:
- Effect: Allow
Action:
- execute-api:ManageConnections
- execute-api:Invoke
Resource:
- ${self:custom.arn_api_gateway_connections}
- Effect: Allow
Action:
- dynamodb:BatchGetItem
Resource:
- Fn::GetAtt: [ UsersDynamoDbTable, Arn ]
websocketDisconnect:
handler: handlers/websockets.routeDisconnect
events:
- websocket: $disconnect
iamRoleStatements:
- Effect: Allow
Action:
- execute-api:ManageConnections
- execute-api:Invoke
Resource:
- ${self:custom.arn_api_gateway_connections}
- Effect: Allow
Action:
- dynamodb:GetItem
- dynamodb:UpdateItem
- dynamodb:DeleteItem
Resource:
- Fn::GetAtt: [ UsersDynamoDbTable, Arn ]
websocketConnect:
handler: handlers/websockets.routeConnect
events:
- websocket:
route: $connect
authorizer:
name: websocketAuth
identitySource:
- 'route.request.querystring.token'
iamRoleStatements:
- Effect: Allow
Action:
- execute-api:ManageConnections
- execute-api:Invoke
Resource:
- ${self:custom.arn_api_gateway_connections}
- Effect: Allow
Action:
- dynamodb:PutItem
- dynamodb:UpdateItem
Resource:
- Fn::GetAtt: [ UsersDynamoDbTable, Arn ]
websocketAuth:
handler: handlers/websockets-auth.handler
# test:
# handler: handlers/test.post
# events:
# - http:
# path: test
# method: post
# TODO: player username lookup endpoint, predictive search
#
gameCreate:
handler: handlers/game.post
description: Create a new game
events:
- http:
path: games
method: post
cors: ${file(api/cors.yml)}
authorizer: aws_iam
request:
schema:
application/json: ${file(api/game-create.json)}
iamRoleStatements:
- Effect: Allow
Action:
- dynamodb:PutItem
Resource:
- Fn::GetAtt: [ GamesDynamoDbTable, Arn ]
- Effect: Allow
Action:
- dynamodb:UpdateItem
Resource:
- Fn::GetAtt: [ UsersDynamoDbTable, Arn ]
gameRead:
handler: handlers/game.get
description: Retreive a game by ID or encrypted key
events:
- http:
path: games/{idOrEncryptedKey}
method: get
cors: ${file(api/cors.yml)}
authorizer: aws_iam
iamRoleStatements:
- Effect: Allow
Action:
- dynamodb:GetItem
- dynamodb:Query
Resource:
- Fn::GetAtt: [ GamesDynamoDbTable, Arn ]
- Fn::Join:
- ""
- - Fn::GetAtt: [ GamesDynamoDbTable, Arn ]
- "/index/${self:custom.dynamodb_index_name_games_id}"
gameList:
handler: handlers/game.list
description: Retreive all recent games
events:
- http:
path: games
method: get
cors: ${file(api/cors.yml)}
authorizer: aws_iam
iamRoleStatements:
- Effect: Allow
Action:
- dynamodb:Query
- dynamodb:GetItem
Resource:
- Fn::GetAtt: [ GamesDynamoDbTable, Arn ]
- Fn::Join:
- ""
- - Fn::GetAtt: [ GamesDynamoDbTable, Arn ]
- "/index/${self:custom.dynamodb_index_name_games_opponent}"
turnCreate:
handler: handlers/turn.post
description: Submit a new turn for a game
events:
- http:
path: games/{idOrEncryptedKey}/turn
method: post
cors: ${file(api/cors.yml)}
authorizer: aws_iam
request:
schema:
application/json: ${file(api/turn-create.json)}
iamRoleStatements:
- Effect: Allow
Action:
- dynamodb:GetItem
- dynamodb:UpdateItem
Resource:
- Fn::GetAtt: [ GamesDynamoDbTable, Arn ]
wsTurnCompleted:
description: Sends websocket events when a turn has completed
handler: handlers/ws-turn-completed.handler
events:
- stream:
type: dynamodb
arn:
Fn::GetAtt: [ GamesDynamoDbTable, StreamArn ]
batchSize: 100
iamRoleStatements:
- Effect: Allow
Action:
- execute-api:ManageConnections
- execute-api:Invoke
Resource:
- ${self:custom.arn_api_gateway_connections}
- Effect: Allow
Action:
- dynamodb:BatchGetItem
Resource:
- Fn::GetAtt: [ UsersDynamoDbTable, Arn ]
wsChatReceived:
description: Writes chat messages to DynamoDB
handler: handlers/ws-chat-received.handler
events:
- websocket:
route: sendGameChat
request:
schema:
application/json: ${file(api/game-create.json)}
iamRoleStatements:
- Effect: Allow
Action:
- execute-api:ManageConnections
# - execute-api:Invoke
Resource:
- ${self:custom.arn_api_gateway_connections}
- Effect: Allow
Action:
- dynamodb:UpdateItem
- dynamodb:GetItem
Resource:
- Fn::GetAtt: [ GamesDynamoDbTable, Arn ]
- Effect: Allow
Action:
- dynamodb:GetItem
- dynamodb:BatchGetItem
Resource:
- Fn::GetAtt: [ UsersDynamoDbTable, Arn ]
resources:
Resources:
UsersDynamoDbTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: ${self:custom.dynamodb_table_name_users}
BillingMode: PAY_PER_REQUEST
# StreamSpecification:
# StreamViewType: NEW_AND_OLD_IMAGES
AttributeDefinitions:
- AttributeName: partitionKey
AttributeType: S
KeySchema:
# user exmaple: u:b6971e08-5c31-41b0-b061-482b41c98460
# connection exmaple: c:dFiR-fGeoAMCIUw=
- AttributeName: partitionKey
KeyType: HASH
GamesDynamoDbTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: ${self:custom.dynamodb_table_name_games}
BillingMode: PAY_PER_REQUEST
StreamSpecification:
StreamViewType: NEW_AND_OLD_IMAGES
AttributeDefinitions:
- AttributeName: partitionKey
AttributeType: S
- AttributeName: sortKey
AttributeType: S
- AttributeName: opponentPartitionKey
AttributeType: S
- AttributeName: opponentSortKey
AttributeType: S
- AttributeName: id
AttributeType: S
KeySchema:
# `${player1Guid}`
# exmaple: b6971e08-5c31-41b0-b061-482b41c98460
- AttributeName: partitionKey
KeyType: HASH
# `${[player2Guid, gameStartTimeMs].join(':')}`
- AttributeName: sortKey
KeyType: RANGE
GlobalSecondaryIndexes:
- IndexName: ${self:custom.dynamodb_index_name_games_id}
# TODO: Make this the primary table key
KeySchema:
- AttributeName: id
KeyType: HASH
Projection:
ProjectionType: INCLUDE
NonKeyAttributes:
- key
- IndexName: ${self:custom.dynamodb_index_name_games_opponent}
KeySchema:
# `${player2Guid}`
# exmaple: f5df8afe-b6bd-4498-a26c-bb5c1d587cc8
- AttributeName: opponentPartitionKey
KeyType: HASH
# `${[player1Guid, gameStartTimeMs].join(':')}`
- AttributeName: opponentSortKey
KeyType: RANGE
Projection:
ProjectionType: INCLUDE
NonKeyAttributes:
- id
- key
- turn
- firstPlayer
- players
- createdAt
- updatedAt
- completedAt
CognitoUserPool:
Type: AWS::Cognito::UserPool
Properties:
UserPoolName: hiddenColoniesUserPool
MfaConfiguration: OFF
AutoVerifiedAttributes:
- email
Schema:
- Name: name
AttributeDataType: String
Mutable: true
Required: true
- Name: email
AttributeDataType: String
Mutable: false
Required: true
Policies:
PasswordPolicy:
MinimumLength: 6
RequireLowercase: false
RequireNumbers: false
RequireSymbols: false
RequireUppercase: false
CognitoUserPoolClient:
Type: AWS::Cognito::UserPoolClient
Properties:
ClientName: hiddenColoniesUserPoolClient
GenerateSecret: false
UserPoolId:
Ref: CognitoUserPool
CognitoIdentityPool:
Type: AWS::Cognito::IdentityPool
Properties:
IdentityPoolName: hiddenColoniesUserIdentityPool
AllowUnauthenticatedIdentities: false
CognitoIdentityProviders:
- ClientId:
Ref: CognitoUserPoolClient
ProviderName:
Fn::GetAtt: [CognitoUserPool, ProviderName]
CognitoIdentityPoolRoles:
Type: AWS::Cognito::IdentityPoolRoleAttachment
Properties:
IdentityPoolId:
Ref: CognitoIdentityPool
Roles:
authenticated:
Fn::GetAtt: [CognitoAuthRole, Arn]
unauthenticated:
Fn::GetAtt: [CognitoUnauthRole, Arn]
CognitoAuthRole:
Type: AWS::IAM::Role
Properties:
RoleName: appAuthRole
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:*"
- "mobileanalytics:PutEvents"
Resource: "*"
- Effect: "Allow"
Action:
- "execute-api:Invoke"
Resource:
Fn::Join:
- ''
-
- 'arn:aws:execute-api:'
- Ref: AWS::Region
- ':'
- Ref: AWS::AccountId
- ':'
- Ref: ApiGatewayRestApi
- '/*'
CognitoUnauthRole:
Type: AWS::IAM::Role
Properties:
RoleName: appUnauthRole
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": unauthenticated
Policies:
- PolicyName: "CognitoUnauthorizedPolicy"
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Action:
- "cognito-sync:*"
- "cognito-identity:*"
# - "mobileanalytics:PutEvents"
Resource: "*"
# CognitoAuthorizer:
# DependsOn:
# - ApiGatewayRestApi
# Type: AWS::ApiGateway::Authorizer
# Properties:
# AuthorizerResultTtlInSeconds: 300
# IdentitySource: method.request.header.Authorization
# RestApiId:
# Ref: ApiGatewayRestApi
# Type: COGNITO_USER_POOLS
# Name: Authorizer
# ProviderARNs:
# - Fn::Join:
# - ''
# - - 'arn:aws:cognito-idp:'
# - ${self:provider.region}
# - ':'
# - Ref: AWS::AccountId
# - ':userpool/'
# - Ref: CognitoUserPool
GatewayResponseDefault4XX:
Type: 'AWS::ApiGateway::GatewayResponse'
Properties:
ResponseParameters:
gatewayresponse.header.Access-Control-Allow-Origin: "'*'"
gatewayresponse.header.Access-Control-Allow-Headers: "'*'"
ResponseType: DEFAULT_4XX
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
RestApiId:
Ref: 'ApiGatewayRestApi'