-
Notifications
You must be signed in to change notification settings - Fork 0
/
serverless.yml
98 lines (87 loc) · 2.92 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
service: hearts-app
provider:
name: aws
region: ca-central-1
runtime: rust
memorySize: 128
tracing:
lambda: true
environment:
tableName: ${self:custom.tableName}
iamRoleStatements:
- Effect: Allow
Action:
- "execute-api:ManageConnections"
Resource:
# https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-websocket-control-access-iam.html
- "arn:aws:execute-api:#{AWS::Region}:#{AWS::AccountId}:*/${self:custom.stage}/POST/@connections/*"
- Effect: Allow
Action:
- dynamodb:PutItem
- dynamodb:GetItem
- dynamodb:DeleteItem
# Restrict our IAM role permissions to
# the specific table for the stage
Resource:
- "Fn::GetAtt": [ ConnectionsTable, Arn ]
custom:
# Our stage is based on what is passed in when running serverless
# commands. Or fallsback to what we have set in the provider section.
stage: ${opt:stage, self:provider.stage}
# Set the table name here so we can use it while testing locally
tableName: ${self:custom.stage}-connections
rust:
dockerTag: latest
package:
individually: true
plugins:
- serverless-rust
- serverless-pseudo-parameters
functions:
# manage connection and disconnection of clients
connections:
handler: connections
events:
- websocket:
# The associated route is used when a client first connects to your WebSocket API.
route: $connect
- websocket:
# The associated route is used when a client disconnects from your API. This call is made on a best-effort basis.
route: $disconnect
# catch-all fallback handler for messages
default:
handler: default
events:
- websocket:
# Used when the route selection expression produces a value that does not match any of the other route keys in your API routes. This can be used, for example, to implement a generic error handling mechanism.
route: $default
# handle { action: send, ... } messages
send:
handler: send
events:
- websocket:
route: send
# handle { action: hearts, ... } messages
hearts:
handler: hearts
events:
- websocket:
route: hearts
resources:
Resources:
# TODO consider if using the same table for connections and "hearts" games?
# DynamoDB best practices suggest most applications should only
# have a single table. For resources check out the follow links...
# https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/best-practices.html
# Advanced Design Patterns for DynamoDB - https://www.youtube.com/watch?v=HaEPXoXVf2k
ConnectionsTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: ${self:custom.tableName}
BillingMode: PAY_PER_REQUEST
AttributeDefinitions:
- AttributeName: id
AttributeType: S
KeySchema:
- AttributeName: id
KeyType: HASH