-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserverless.yml
203 lines (189 loc) · 6.21 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
service: serverless-color-tracking
frameworkVersion: "3"
# TODO: Add WAF to protect the API Gateway from DDoS attacks
# TODO: check wether API Key provided by clients exist in the database before allowing them to connect to the websocket
# TODO: add "events per second" column associated with each API Key in database
# TODO: use DynamoDB updates - https://catalog.workshops.aws/serverless-app-terraform/en-US/module4-realtime-updates
# this way data broadcasted to clients is ALWAYS up-to-date
# TODO: define "limit" for "events per second" for each API Key in database
# TODO: simulate 1K users sending events to API Gateway and see how it scales
# TODO: Load testing with Hey tool - https://catalog.workshops.aws/serverless-app-terraform/en-US/module2-backend-apis/review-test#load-test
# TODO: validate events schema before processing them - https://docs.powertools.aws.dev/lambda/python/2.16.2/utilities/validation/#validating-custom-formats
# TODO: apply "reserved concurrent executions" to avoid being charged for more than expected on lamda functions
provider:
name: aws
runtime: python3.9
profile: default
region: us-east-1
logRetentionInDays: 7
environment:
# TODO: put into SSM Manager stores (helps for rotation of the values itself) and use AWS KMS to encrypt this value
ADMIN_API_KEY: ${file(./secrets.json):ADMIN_API_KEY}
CONNECTIONS_TABLE: ${self:service}-${sls:stage}-connectionsTable
STATS_TABLE: ${self:service}-${sls:stage}-statsTable
CLICKS_STREAM: ${self:service}-${sls:stage}-clicksStream
HOVERS_STREAM: ${self:service}-${sls:stage}-hoversStream
websocketsApiName: ${self:service}-${sls:stage}-websocketApi
websocketsApiRouteSelectionExpression: $request.body.action
iam:
role:
statements:
- Effect: Allow
Action:
- kinesis:GetRecords
- kinesis:GetShardIterator
- kinesis:DescribeStream
- kinesis:ListStreams
- kinesis:PutRecord
- kinesis:PutRecords
Resource:
- Fn::GetAtt: [kinesisClicksStream, Arn]
- Effect: Allow
Action:
- kinesis:GetRecords
- kinesis:GetShardIterator
- kinesis:DescribeStream
- kinesis:ListStreams
- kinesis:PutRecord
- kinesis:PutRecords
Resource:
- Fn::GetAtt: [kinesisHoversStream, Arn]
- Effect: Allow
Action:
- dynamodb:GetItem
- dynamodb:PutItem
- dynamodb:Scan
Resource: "arn:aws:dynamodb:${aws:region}:*:table/${self:provider.environment.STATS_TABLE}"
- Effect: Allow
Action:
- dynamodb:PutItem
- dynamodb:DeleteItem
- dynamodb:Scan
Resource: "arn:aws:dynamodb:${aws:region}:*:table/${self:provider.environment.CONNECTIONS_TABLE}"
- Effect: Allow
Action:
- "execute-api:ManageConnections"
Resource:
- "arn:aws:execute-api:*:*:**/@connections/*"
plugins:
- serverless-python-requirements
# TODO: try serverless-finch to deploy static website
# TODO: try AWS Amplify to deploy static website. Github repo can be connected to Amplify to deploy automatically any changes.
functions:
createEvent:
handler: lambdas/endpoints/v1/events/producer.handler
events:
- http:
path: /v1/events
method: post
cors: true
clicksConsumer:
handler: lambdas/consumers/clicks.handler
events:
- stream:
type: kinesis
arn:
Fn::GetAtt:
- kinesisClicksStream
- Arn
hoversConsumer:
handler: lambdas/consumers/hovers.handler
events:
- stream:
type: kinesis
arn:
Fn::GetAtt:
- kinesisHoversStream
- Arn
broadcastClicks:
handler: lambdas/websockets/broadcast/clicks.handler
events:
- stream:
type: kinesis
arn:
Fn::GetAtt:
- kinesisClicksStream
- Arn
broadcastHovers:
handler: lambdas/websockets/broadcast/hovers.handler
events:
- stream:
type: kinesis
arn:
Fn::GetAtt:
- kinesisHoversStream
- Arn
broadcastAdminStats:
handler: lambdas/websockets/broadcast/admin/stats.handler
events:
- stream:
type: kinesis
arn:
Fn::GetAtt:
- kinesisClicksStream
- Arn
- stream:
type: kinesis
arn:
Fn::GetAtt:
- kinesisHoversStream
- Arn
getStats:
handler: lambdas/endpoints/v1/stats/get.handler
events:
- http:
path: /v1/stats
method: get
cors: true
getAdminStats:
handler: lambdas/endpoints/admin/stats.handler
events:
- http:
path: /admin/v1/stats
method: get
cors: true
websocketConnections:
handler: lambdas/websockets/manager.handler
events:
- websocket:
route: $connect
- websocket:
route: $disconnect
- websocket:
route: $default
resources:
Resources:
kinesisClicksStream:
Type: AWS::Kinesis::Stream
Properties:
Name: ${self:provider.environment.CLICKS_STREAM}
RetentionPeriodHours: 24
ShardCount: 1
kinesisHoversStream:
Type: AWS::Kinesis::Stream
Properties:
Name: ${self:provider.environment.HOVERS_STREAM}
RetentionPeriodHours: 24
ShardCount: 1
ConnectionsTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: ${self:provider.environment.CONNECTIONS_TABLE}
KeySchema:
- AttributeName: ConnectionId
KeyType: HASH
AttributeDefinitions:
- AttributeName: ConnectionId
AttributeType: S
BillingMode: PAY_PER_REQUEST
StatsTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: ${self:provider.environment.STATS_TABLE}
KeySchema:
- AttributeName: Id
KeyType: HASH
AttributeDefinitions:
- AttributeName: Id
AttributeType: S
BillingMode: PAY_PER_REQUEST