-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtemplate.yaml
270 lines (265 loc) · 8.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
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
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
sutd-trivia-bot
# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Parameters:
BotToken:
Type: String
Description: Telegram API bot token
Globals:
Function:
Timeout: 15
Runtime: python3.8
Environment:
Variables:
TABLE_NAME: !Ref GameTable
LOCK_TABLE_NAME: !Ref LockTable
BOT_TOKEN: !Ref BotToken
Layers:
- !Ref CommonLayer
EventInvokeConfig:
MaximumRetryAttempts: 0
Resources:
CommonLayer:
Type: AWS::Serverless::LayerVersion
Properties:
ContentUri: common
CompatibleRuntimes:
- python3.8
Metadata:
BuildMethod: python3.8
LockTable:
Type: AWS::DynamoDB::Table
Properties:
KeySchema:
- AttributeName: "lock_key"
KeyType: "HASH"
- AttributeName: "sort_key"
KeyType: "RANGE"
AttributeDefinitions:
- AttributeName: "lock_key"
AttributeType: "S"
- AttributeName: "sort_key"
AttributeType: "S"
BillingMode: PAY_PER_REQUEST
TimeToLiveSpecification:
AttributeName: expiry_time
Enabled: true
GameTable:
Type: AWS::DynamoDB::Table
Properties:
KeySchema:
- AttributeName: "pk"
KeyType: "HASH"
- AttributeName: "sk"
KeyType: "RANGE"
AttributeDefinitions:
- AttributeName: "pk"
AttributeType: "S"
- AttributeName: "sk"
AttributeType: "S"
- AttributeName: "score"
AttributeType: "N"
- AttributeName: "gsi_current_active_question"
AttributeType: "S"
- AttributeName: "gsi_callback_question_id"
AttributeType: "S"
GlobalSecondaryIndexes:
- IndexName: ScoreBoard
KeySchema:
- AttributeName: "pk"
KeyType: "HASH"
- AttributeName: "score"
KeyType: "RANGE"
Projection:
ProjectionType: ALL
- IndexName: CurrentActiveQuestion
KeySchema:
- AttributeName: "pk"
KeyType: "HASH"
- AttributeName: "gsi_current_active_question"
KeyType: "RANGE"
Projection:
ProjectionType: ALL
- IndexName: CallbacksByQuestionId
KeySchema:
- AttributeName: "pk"
KeyType: "HASH"
- AttributeName: "gsi_callback_question_id"
KeyType: "RANGE"
Projection:
ProjectionType: ALL
BillingMode: PAY_PER_REQUEST
TelegramBotFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: bot
Handler: lambda_entry.lambda_handler
Events:
JoinSession:
Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
Properties:
Path: /
Method: post
RestApiId: !Ref TelegramWebhookAPI
Environment:
Variables:
TABLE_NAME: !Ref GameTable
LOCK_TABLE_NAME: !Ref LockTable
START_GAME_STATE_MACHINE_ARN: !Ref QuizFlowStateMachine
EventInvokeConfig:
MaximumRetryAttempts: 0
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref LockTable
- DynamoDBCrudPolicy:
TableName: !Ref GameTable
- Statement:
- Effect: Allow
Action:
- 'states:StopExecution'
- 'states:StartExecution'
Resource:
- !Sub "arn:${AWS::Partition}:states:${AWS::Region}:${AWS::AccountId}:stateMachine:*"
- !Sub
- "arn:${Partition}:states:${Region}:${Account}:execution:${StateMachineName}:*"
- Partition: !Ref AWS::Partition
Region: !Ref AWS::Region
Account: !Ref AWS::AccountId
StateMachineName: !GetAtt QuizFlowStateMachine.Name
- !Sub
- "arn:${Partition}:states:${Region}:${Account}:execution:${StateMachineName}:*"
- Partition: !Ref AWS::Partition
Region: !Ref AWS::Region
Account: !Ref AWS::AccountId
StateMachineName: !GetAtt QuestionFlowStateMachine.Name
TelegramWebhookAPI:
Type: AWS::Serverless::Api
Properties:
Name: !Sub "${AWS::StackName}-TelegramWebhookAPI"
StageName: prod
EndpointConfiguration:
Type: REGIONAL
### Question Flow Section
QuestionFlowSendQuestionFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: quizzer/question_flow/send_question
Handler: send.lambda_handler
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref GameTable
- DynamoDBCrudPolicy:
TableName: !Ref LockTable
QuestionFlowFailQuestionFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: quizzer/question_flow/fail_question
Handler: fail.lambda_handler
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref GameTable
- DynamoDBCrudPolicy:
TableName: !Ref LockTable
QuestionFlowStateMachine:
Type: AWS::Serverless::StateMachine
Properties:
DefinitionUri: quizzer/question_flow/statemachine.asl.json
DefinitionSubstitutions:
SendQuestionFunctionArn: !GetAtt QuestionFlowSendQuestionFunction.Arn
FailQuestionFunctionArn: !GetAtt QuestionFlowFailQuestionFunction.Arn
Policies:
- LambdaInvokePolicy:
FunctionName: !Ref QuestionFlowSendQuestionFunction
- LambdaInvokePolicy:
FunctionName: !Ref QuestionFlowFailQuestionFunction
### Parent Quiz Flow Section
QuizFlowSampleQuestionsFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: quizzer/quiz_flow/sample_questions
Handler: sample.lambda_handler
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref GameTable
- DynamoDBCrudPolicy:
TableName: !Ref LockTable
QuizFlowChooseQuestionFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: quizzer/quiz_flow/choose_question
Handler: choose.lambda_handler
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref GameTable
- DynamoDBCrudPolicy:
TableName: !Ref LockTable
QuizFlowEndQuizFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: quizzer/quiz_flow/end_quiz
Handler: end.lambda_handler
Environment:
Variables:
START_GAME_STATE_MACHINE_ARN: "To replace later"
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref GameTable
- DynamoDBCrudPolicy:
TableName: !Ref LockTable
QuizFlowIntermissionFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: quizzer/quiz_flow/intermission
Handler: intermission.lambda_handler
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref GameTable
- DynamoDBCrudPolicy:
TableName: !Ref LockTable
QuizFlowStateMachine:
Type: AWS::Serverless::StateMachine
Properties:
DefinitionUri: quizzer/quiz_flow/statemachine.asl.json
DefinitionSubstitutions:
SampleQuestionsFunctionArn: !GetAtt QuizFlowSampleQuestionsFunction.Arn
ChooseQuestionFunctionArn: !GetAtt QuizFlowChooseQuestionFunction.Arn
EndQuizFunctionArn: !GetAtt QuizFlowEndQuizFunction.Arn
IntermissionFunctionArn: !GetAtt QuizFlowIntermissionFunction.Arn
QuestionFlowStateMachineArn: !Ref QuestionFlowStateMachine
Policies:
- LambdaInvokePolicy:
FunctionName: !Ref QuizFlowSampleQuestionsFunction
- LambdaInvokePolicy:
FunctionName: !Ref QuizFlowChooseQuestionFunction
- LambdaInvokePolicy:
FunctionName: !Ref QuizFlowIntermissionFunction
- LambdaInvokePolicy:
FunctionName: !Ref QuizFlowEndQuizFunction
- StepFunctionsExecutionPolicy:
StateMachineName: !Sub
- "${QuestionFlowStateMachineName}"
- QuestionFlowStateMachineName: !GetAtt QuestionFlowStateMachine.Name
- Statement:
- Effect: Allow
Action:
- states:DescribeExecution
- states:StopExecution
Resource:
- "*"
- Effect: Allow
Action:
- events:PutTargets
- events:PutRule
- events:DescribeRule
Resource:
- !Sub arn:${AWS::Partition}:events:${AWS::Region}:${AWS::AccountId}:rule/StepFunctionsGetEventsForStepFunctionsExecutionRule
Outputs:
TelegramWebhookURL:
Description: Set the bot's API webhook to this location
Value: !Sub "https://${TelegramWebhookAPI}.execute-api.${AWS::Region}.amazonaws.com/prod"
GameTableName:
Value: !Ref GameTable
LockTableName:
Value: !Ref LockTable