-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.yaml
235 lines (218 loc) · 6.98 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
AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Description: partial batch response sample
Globals:
Function:
Timeout: 5
Runtime: python3.11
Tracing: Active
Environment:
Variables:
POWERTOOLS_SERVICE_NAME: scoreboard
LOG_LEVEL: INFO
POWERTOOLS_METRICS_NAMESPACE: PowertoolsScoreboard
Layers:
- !Sub arn:aws:lambda:${AWS::Region}:017000801446:layer:AWSLambdaPowertoolsPythonV2:40 # v2.22.0
Resources:
# RESOURCES FOR PRODUCER: EventBridge -> Lambda -> Kinesis
ProducerFunction:
Type: AWS::Serverless::Function
Properties:
Handler: app.lambda_handler
CodeUri: producer
FunctionName: scoreboard_producer
Policies:
# Lambda Destinations require additional permissions
# to send failure records to DLQ from Kinesis/DynamoDB
- Version: "2012-10-17"
Statement:
Effect: "Allow"
Action:
- kinesis:PutRecords
Resource: !GetAtt ScoreBoardStream.Arn
ScoreBoardStream:
Type: AWS::Kinesis::Stream
Properties:
Name: kds_scoreboard
ShardCount: 1
StreamEncryption:
EncryptionType: KMS
KeyId: alias/aws/kinesis
EventRuleScheduledProducer:
Type: AWS::Events::Rule
Properties:
Name: scheduled-producer-scoreboard
ScheduleExpression: rate(1 minute)
State: ENABLED
Targets:
- Arn: !GetAtt ProducerFunction.Arn
Id: !Ref ProducerFunction
ProducerPermissionScheduler:
Type: AWS::Lambda::Permission
Properties:
FunctionName: !Ref ProducerFunction
Action: lambda:InvokeFunction
Principal: events.amazonaws.com
SourceArn: !GetAtt EventRuleScheduledProducer.Arn
# RESOURCES FOR STREAM PROCESSOR: Kinesis -> Lambda -> DynamoDB
StreamProcessorFunction:
Type: AWS::Serverless::Function
Properties:
Handler: app.lambda_handler
CodeUri: stream_processor
FunctionName: scoreboard_stream_processor
Timeout: 30
Policies:
# Lambda Destinations require additional permissions
# to send failure records to DLQ from Kinesis/DynamoDB
- Version: "2012-10-17"
Statement:
Effect: "Allow"
Action:
- sqs:GetQueueAttributes
- sqs:GetQueueUrl
- sqs:SendMessage
Resource: !GetAtt DLQStreamProcessor.Arn
- DynamoDBCrudPolicy:
TableName: !Ref ScoreboardRawData
Events:
KinesisStream:
Type: Kinesis
Properties:
Stream: !GetAtt ScoreBoardStream.Arn
BatchSize: 100
MaximumBatchingWindowInSeconds: 2
StartingPosition: LATEST
MaximumRetryAttempts: 2
DestinationConfig:
OnFailure:
Destination: !GetAtt DLQStreamProcessor.Arn
FunctionResponseTypes:
- ReportBatchItemFailures
StreamProcessorFunctionPermissionKinesisInvoke:
Type: AWS::Lambda::Permission
Properties:
Action: lambda:InvokeFunction
FunctionName: !GetAtt StreamProcessorFunction.Arn
Principal: "kinesis.amazonaws.com"
SourceArn: !GetAtt ScoreBoardStream.Arn
DLQStreamProcessor:
Type: AWS::SQS::Queue
ScoreboardRawData:
Type: AWS::DynamoDB::Table
Properties:
TableName: ScoreboardRawData
AttributeDefinitions:
- AttributeName: player_name
AttributeType: S
- AttributeName: timestamp
AttributeType: S
KeySchema:
- AttributeName: player_name
KeyType: HASH
- AttributeName: timestamp
KeyType: RANGE
ProvisionedThroughput:
ReadCapacityUnits: 5
WriteCapacityUnits: 5
TimeToLiveSpecification:
AttributeName: expiration
Enabled: true
StreamSpecification:
StreamViewType: NEW_IMAGE
# RESOURCES FOR SUMMARIZE: DynamoDB -> Lambda -> DynamoDB
SummarizeDataFunction:
Type: AWS::Serverless::Function
Properties:
Handler: app.lambda_handler
CodeUri: summarize
FunctionName: scoreboard_summarize
Timeout: 30
Policies:
- Version: "2012-10-17"
Statement:
Effect: "Allow"
Action:
- sqs:GetQueueAttributes
- sqs:GetQueueUrl
- sqs:SendMessage
Resource: !GetAtt DLQSummarize.Arn
- DynamoDBCrudPolicy:
TableName: !Ref ScoreboardSummedUpData
Events:
DynamoDBStreamEvent:
Type: DynamoDB
Properties:
Stream: !GetAtt ScoreboardRawData.StreamArn
StartingPosition: LATEST
BatchSize: 10
MaximumBatchingWindowInSeconds: 2
MaximumRetryAttempts: 2
DestinationConfig:
OnFailure:
Destination: !GetAtt DLQSummarize.Arn
FunctionResponseTypes:
- ReportBatchItemFailures
ScoreboardSummedUpData:
Type: AWS::DynamoDB::Table
Properties:
TableName: ScoreboardSummedUpData
AttributeDefinitions:
- AttributeName: player_name
AttributeType: S
KeySchema:
- AttributeName: player_name
KeyType: HASH
ProvisionedThroughput:
ReadCapacityUnits: 5
WriteCapacityUnits: 5
DLQSummarize:
Type: AWS::SQS::Queue
# RESOURCES FOR RESTAPI: API Gateway -> Lambda -> DynamoDB
RestApiScoreboard:
Type: AWS::Serverless::Api
Properties:
StageName: prod
ApiKeySourceType: HEADER
Cors: # see CORS section
AllowOrigin: "'*'" # NEVER in Prod ;)
AllowHeaders: "'Content-Type,Authorization,X-Amz-Date,X-Api-Key'"
MaxAge: "'300'"
QueryFunction:
Type: AWS::Serverless::Function
Properties:
Handler: app.lambda_handler
CodeUri: score_api
FunctionName: scoreboard_query
Timeout: 5
Policies:
- DynamoDBReadPolicy:
TableName: !Ref ScoreboardSummedUpData
Events:
GetResource:
Type: Api
Properties:
RestApiId: !Ref RestApiScoreboard
Path: /score
Method: GET
RequestParameters:
- method.request.querystring.order:
Required: true
- method.request.querystring.filter:
Required: false
ResourcesMapParameter:
Type: AWS::SSM::Parameter
Properties:
Name: "/service/scorecard/test/config"
Type: String
Description: Service Map with common resources used for integ/e2e testing
Value: !Sub |
{
"KinesisStream": "kds_scoreboard",
"SummarizedTable": "ScoreboardSummedUpData",
"ScoreEndpoint": "https://${RestApiScoreboard}.execute-api.${AWS::Region}.amazonaws.com/prod/score"
}
Outputs:
ApiEndpoint:
Description: API endpoint URL for the deployed application
Value: !Sub "https://${RestApiScoreboard}.execute-api.${AWS::Region}.amazonaws.com/prod/score"