-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.yaml
459 lines (432 loc) · 12.7 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
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
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: SAM Template for lazy-home
Globals:
Function:
Timeout: 10
Handler: app.lambda_handler
Runtime: python3.9
Architectures:
- x86_64
Layers:
- arn:aws:lambda:ap-northeast-1:770693421928:layer:Klayers-p38-pycryptodome:2
- !Ref LazyHomeLayer
Environment:
Variables:
CHANNEL_ACCESS_TOKEN: !Ref ChannelAccessToken
ADMIN_LINE_USER_ID: !Ref AdminLineUserId
SWITCH_BOT_OPEN_TOKEN: !Ref SwitchBotOpenToken
SWITCH_BOT_CLIENT_SECRET: !Ref SwitchBotClientSecret
SESAME_API_KEY: !Ref SesameApiKey
SESAME_DEVICE_UUID: !Ref SesameDeviceUuid
SESAME_DEVICE_SECRET: !Ref SesameDeviceSecret
Tags:
Project: lazy-home
Parameters:
ChannelAccessToken:
Type: String
Description: line bot channel access token
AdminLineUserId:
Type: String
Description: notification destination line user id
SwitchBotOpenToken:
Type: String
SwitchBotClientSecret:
Type: String
TwitterConsumerSecret:
Type: String
Description: twitter consumer secret
SesameApiKey:
Type: String
SesameDeviceUuid:
Type: String
SesameDeviceSecret:
Type: String
Resources:
LazyHomeLayer:
Type: AWS::Serverless::LayerVersion
Properties:
ContentUri: layer/
CompatibleRuntimes:
- python3.9
Metadata:
BuildMethod: python3.9
IftttDispatcher:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/dispatcher/ifttt/
Events:
Controller:
Type: Api
Properties:
Path: /dispatcher/ifttt
Method: post
FunctionName: IftttDispatcher
Role: !GetAtt LambdaFunctionRole.Arn
IftttDispatcherLogGroup:
Type: AWS::Logs::LogGroup
DependsOn:
- IftttDispatcher
Properties:
LogGroupName: !Sub /aws/lambda/${IftttDispatcher}
RetentionInDays: 3
Tags:
- Key: Project
Value: lazy-home
LineDispatcher:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/dispatcher/line/
Events:
Controller:
Type: Api
Properties:
Path: /dispatcher/line
Method: post
FunctionName: LineDispatcher
LineDispatcherLogGroup:
Type: AWS::Logs::LogGroup
DependsOn:
- LineDispatcher
Properties:
LogGroupName: !Sub /aws/lambda/${LineDispatcher}
RetentionInDays: 3
Tags:
- Key: Project
Value: lazy-home
SwitchBotDispatcher:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/dispatcher/switch_bot/
Events:
Controller:
Type: Api
Properties:
Path: /dispatcher/switch_bot
Method: post
FunctionName: SwitchBotDispatcher
Role: !GetAtt LambdaFunctionRole.Arn
SwitchBotDispatcherLogGroup:
Type: AWS::Logs::LogGroup
DependsOn:
- SwitchBotDispatcher
Properties:
LogGroupName: !Sub /aws/lambda/${SwitchBotDispatcher}
RetentionInDays: 3
Tags:
- Key: Project
Value: lazy-home
TwitterDispatcher:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/dispatcher/twitter/
Events:
Controller:
Type: Api
Properties:
Path: /dispatcher/twitter
Method: post
Environment:
Variables:
OPEN_CURTAINS_STATE_MACHINE_ARN: !Ref OpenCurtainsStateMachine
FunctionName: TwitterDispatcher
Role: !GetAtt LambdaFunctionRole.Arn
TwitterDispatcherLogGroup:
Type: AWS::Logs::LogGroup
DependsOn:
- TwitterDispatcher
Properties:
LogGroupName: !Sub /aws/lambda/${TwitterDispatcher}
RetentionInDays: 3
Tags:
- Key: Project
Value: lazy-home
TwitterChallengeResponseCheck:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/dispatcher/twitter/crc
Events:
Controller:
Type: Api
Properties:
Path: /dispatcher/twitter
Method: get
Environment:
Variables:
TWITTER_CONSUMER_SECRET: !Ref TwitterConsumerSecret
FunctionName: TwitterChallengeResponseCheck
TwitterChallengeResponseCheckLogGroup:
Type: AWS::Logs::LogGroup
DependsOn:
- TwitterChallengeResponseCheck
Properties:
LogGroupName: !Sub /aws/lambda/${TwitterChallengeResponseCheck}
RetentionInDays: 1
Tags:
- Key: Project
Value: lazy-home
LockSesameFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/routine/auto_lock/lock_sesame/
FunctionName: LockSesame
Role: !GetAtt LambdaFunctionRole.Arn
LockSesameFuncLogGroup:
Type: AWS::Logs::LogGroup
DependsOn:
- LockSesameFunction
Properties:
LogGroupName: !Sub /aws/lambda/${LockSesameFunction}
RetentionInDays: 3
Tags:
- Key: Project
Value: lazy-home
UnlockSesameFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/routine/auto_lock/unlock_sesame/
FunctionName: UnlockSesame
Role: !GetAtt LambdaFunctionRole.Arn
UnlockSesameFuncLogGroup:
Type: AWS::Logs::LogGroup
DependsOn:
- UnlockSesameFunction
Properties:
LogGroupName: !Sub /aws/lambda/${UnlockSesameFunction}
RetentionInDays: 3
Tags:
- Key: Project
Value: lazy-home
NotifyDoorOpeningFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/routine/auto_lock/notify_door_opening/
FunctionName: NotifyDoorOpeningFunction
Role: !GetAtt LambdaFunctionRole.Arn
NotifyDoorOpeningLogGroup:
Type: AWS::Logs::LogGroup
DependsOn:
- NotifyDoorOpeningFunction
Properties:
LogGroupName: !Sub /aws/lambda/${NotifyDoorOpeningFunction}
RetentionInDays: 3
Tags:
- Key: Project
Value: lazy-home
OpenCurtainsFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/routine/morning/open_curtains/
FunctionName: OpenCurtains
Role: !GetAtt LambdaFunctionRole.Arn
OpenCurtainsFuncLogGroup:
Type: AWS::Logs::LogGroup
DependsOn:
- OpenCurtainsFunction
Properties:
LogGroupName: !Sub /aws/lambda/${OpenCurtainsFunction}
RetentionInDays: 3
Tags:
- Key: Project
Value: lazy-home
LambdaFunctionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
Path: /
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
- arn:aws:iam::aws:policy/AWSStepFunctionsFullAccess
- arn:aws:iam::aws:policy/AWSIoTEventsFullAccess
Tags:
- Key: Project
Value: lazy-home
OpenCurtainsStateMachine:
Type: AWS::Serverless::StateMachine
Properties:
Name: OpenCurtainsStateMachine
Type: STANDARD
DefinitionUri: statemachine/open_curtains.asl.yaml
DefinitionSubstitutions:
OpenCurtainsFunctionArn: !GetAtt OpenCurtainsFunction.Arn
Role: !GetAtt StateMachineRole.Arn
Logging:
Level: ALL
IncludeExecutionData: True
Destinations:
- CloudWatchLogsLogGroup:
LogGroupArn: !GetAtt OpenCurtainsStateMachineLogGroup.Arn
Tags:
Project: lazy-home
OpenCurtainsStateMachineLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName : /aws/states/OpenCurtainsStateMachine
RetentionInDays: 3
Tags:
- Key: Project
Value: lazy-home
StateMachineRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service:
- states.amazonaws.com
Action:
- sts:AssumeRole
Description: >-
Permissions required to execute step functions
Path: /
ManagedPolicyArns:
- arn:aws:iam::aws:policy/CloudWatchLogsFullAccess
- arn:aws:iam::aws:policy/service-role/AWSLambdaRole
Tags:
- Key: Project
Value: lazy-home
FrontDoorDetector:
Type: AWS::IoTEvents::DetectorModel
Properties:
DetectorModelDefinition:
InitialStateName: closed
States:
- StateName: unlocked
OnInput:
Events: []
TransitionEvents:
- EventName: the_door_was_opened
Condition: $input.front_door_input.door.event == 'open'
Actions: []
NextState: opened
- EventName: left_unlock
Condition: timeout("left_unlock_timer")
Actions: []
NextState: closed
OnEnter:
Events:
- EventName: unlock
Condition: 'true'
Actions:
- Lambda:
FunctionArn: !GetAtt UnlockSesameFunction.Arn
- EventName: set_left_unlock_timer
Condition: 'true'
Actions:
- SetTimer:
TimerName: left_unlock_timer
Seconds: 60
OnExit:
Events:
- EventName: delete_left_unlock_timer
Condition: 'true'
Actions:
- ClearTimer:
TimerName: left_unlock_timer
- StateName: opened
OnInput:
Events:
- EventName: notify_alert
Condition: timeout("left_open_timer")
Actions:
- Lambda:
FunctionArn: !GetAtt NotifyDoorOpeningFunction.Arn
TransitionEvents:
- EventName: the_door_was_closed
Condition: $input.front_door_input.door.event == 'close'
Actions: []
NextState: closed
OnEnter:
Events:
- EventName: set_left_open_timer
Condition: 'true'
Actions:
- SetTimer:
TimerName: left_open_timer
Seconds: 60
OnExit:
Events:
- EventName: delete_left_open_timer
Condition: 'true'
Actions:
- ClearTimer:
TimerName: left_open_timer
- StateName: closed
OnInput:
Events: []
TransitionEvents:
- EventName: the_door_was_opened
Condition: $input.front_door_input.door.event == 'open'
Actions: []
NextState: opened
- EventName: unlocked_remotely
Condition: $input.front_door_input.key.event == 'unlock'
Actions: []
NextState: unlocked
OnEnter:
Events:
- EventName: lock_automatically
Condition: 'true'
Actions:
- Lambda:
FunctionArn: !GetAtt LockSesameFunction.Arn
OnExit:
Events: []
DetectorModelName: front_door
EvaluationMethod: BATCH
RoleArn: !GetAtt IotEventsRole.Arn
Tags:
- Key: Project
Value: lazy-home
FrontDoorInput:
Type: AWS::IoTEvents::Input
Properties:
InputDefinition:
Attributes:
- JsonPath: door.event
- JsonPath: key.event
InputDescription: Inputs used to manage the condition of the entrance
InputName: front_door_input
Tags:
- Key: Project
Value: lazy-home
IotEventsRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service:
- iotevents.amazonaws.com
Action:
- sts:AssumeRole
Path: /
MaxSessionDuration: 3600
Policies:
- PolicyName: IotEvents
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- iotevents:BatchPutMessage
Resource: '*'
- Effect: Allow
Action:
- lambda:InvokeFunction
Resource: '*'
Tags:
- Key: Project
Value: lazy-home