-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.yml
334 lines (333 loc) · 10.8 KB
/
template.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
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
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
Api:
Type: AWS::Serverless::Function
Properties:
CodeUri: ./api
Environment:
Variables:
DETECTORDAG_JWT_DURATION: "2h"
DETECTORDAG_JWT_SECRET: "dummy-secret"
Handler: main
Runtime: go1.x
Timeout: 5
Events:
Auth:
Type: Api
Properties:
Path: /v1/auth
Method: post
AuthOptions:
Type: Api
Properties:
Path: /v1/auth
Method: options
UpdateDevice:
Type: Api
Properties:
Path: /v1/devices/{deviceId}
Method: patch
DeviceOptions:
Type: Api
Properties:
Path: /v1/devices/{deviceId}
Method: options
GetAccount:
Type: Api
Properties:
Path: /v1/accounts/{accountId}
Method: get
AccountOptions:
Type: Api
Properties:
Path: /v1/accounts/{accountId}
Method: options
UpdateAccount:
Type: Api
Properties:
Path: /v1/accounts/{accountId}
Method: patch
GetAccountDevices:
Type: Api
Properties:
Path: /v1/accounts/{accountId}/devices
Method: get
AccountDevicesOptions:
Type: Api
Properties:
Path: /v1/accounts/{accountId}/devices
Method: options
Policies:
- Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- 'dynamodb:GetItem'
- 'dynamodb:UpdateItem'
- 'dynamodb:Query'
Resource:
- !Sub "arn:${AWS::Partition}:dynamodb:${AWS::Region}:${AWS::AccountId}:table/accounts"
- !Sub "arn:${AWS::Partition}:dynamodb:${AWS::Region}:${AWS::AccountId}:table/accounts/index/*"
- Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- 'iot:ListThings'
Resource: '*'
- Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- 'iot:DescribeThing'
- 'iot:ListThingGroupsForThing'
- 'iot:GetThingShadow'
- 'iot:UpdateThingShadow'
Resource:
- !Sub "arn:${AWS::Partition}:iot:${AWS::Region}:${AWS::AccountId}:thing/*"
- Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- 'iot:DescribeEndpoint'
Resource: '*'
- Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- 'ses:VerifyEmailIdentity'
- 'ses:GetIdentityVerificationAttributes'
Resource: '*'
PowerStatusChanged:
Type: AWS::IoT::TopicRule
Properties:
TopicRulePayload:
RuleDisabled: 'false'
AwsIotSqlVersion: '2016-03-23'
Sql: SELECT topic(3) as deviceId, timestamp, current.state.reported as state, current.metadata.reported as updated FROM '$aws/things/+/shadow/update/documents' WHERE current.state.reported.status <> previous.state.reported.status
Actions:
- Lambda:
FunctionArn: !GetAtt consumer.Arn
ConnectionStatusChanged:
Type: AWS::IoT::TopicRule
Properties:
TopicRulePayload:
RuleDisabled: 'false'
Sql: SELECT * FROM '$aws/events/presence/#'
AwsIotSqlVersion: '2016-03-23'
Actions:
- Lambda:
FunctionArn: !GetAtt ConnectionStatusListener.Arn
PowerUpdateIsConnected:
Type: AWS::IoT::TopicRule
Properties:
TopicRulePayload:
RuleDisabled: 'false'
AwsIotSqlVersion: '2016-03-23'
Sql: SELECT topic(3) as clientId, timestamp() as timestamp, "connected" as eventType FROM '$aws/things/+/shadow/update'
Actions:
- Lambda:
FunctionArn: !GetAtt ConnectionStatusListener.Arn
ConsumerPermission:
Type: AWS::Lambda::Permission
Properties:
Action: lambda:InvokeFunction
FunctionName: !GetAtt consumer.Arn
Principal: iot.amazonaws.com
SourceArn: !GetAtt PowerStatusChanged.Arn
ConnectionStatusListenerPermission:
Type: AWS::Lambda::Permission
Properties:
Action: lambda:InvokeFunction
FunctionName: !GetAtt ConnectionStatusListener.Arn
Principal: iot.amazonaws.com
SourceArn: !GetAtt ConnectionStatusChanged.Arn
PowerUpdateIsConnectedPermission:
Type: AWS::Lambda::Permission
Properties:
Action: lambda:InvokeFunction
FunctionName: !GetAtt ConnectionStatusListener.Arn
Principal: iot.amazonaws.com
SourceArn: !GetAtt PowerUpdateIsConnected.Arn
consumer:
Type: AWS::Serverless::Function
Properties:
CodeUri: ./consumer
Environment:
Variables:
SENDER_EMAIL: [email protected]
Handler: main
Runtime: go1.x
Policies:
- DynamoDBReadPolicy:
TableName: accounts
- Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- 'ses:SendEmail'
- 'ses:SendRawEmail'
- 'ses:GetIdentityVerificationAttributes'
Resource: '*'
- Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- 'dynamodb:GetItem'
Resource:
- !Sub "arn:${AWS::Partition}:dynamodb:${AWS::Region}:${AWS::AccountId}:table/accounts"
- Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- 'iot:DescribeThing'
- 'iot:GetThingShadow'
Resource:
- !Sub "arn:${AWS::Partition}:iot:${AWS::Region}:${AWS::AccountId}:thing/*"
- Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- 'iot:DescribeEndpoint'
Resource: '*'
ConnectionStatusQueue:
Type: AWS::SQS::Queue
Properties:
DelaySeconds: 5
ConnectionStatusQueueMap:
Type: AWS::Lambda::EventSourceMapping
Properties:
EventSourceArn: !GetAtt ConnectionStatusQueue.Arn
FunctionName: !GetAtt Disconnected.Arn
ConnectionStatusListener:
Type: AWS::Serverless::Function
Properties:
CodeUri: ./connection/listener
Environment:
Variables:
SENDER_EMAIL: [email protected]
DELAY_QUEUE_URL: !Ref ConnectionStatusQueue
Handler: main
Runtime: go1.x
Timeout: 5
Policies:
- Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- 'ses:SendEmail'
- 'ses:SendRawEmail'
- 'ses:GetIdentityVerificationAttributes'
Resource: '*'
- Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- 'iot:DescribeThing'
- 'iot:GetThingShadow'
- 'iot:UpdateThingShadow'
Resource:
- !Sub "arn:${AWS::Partition}:iot:${AWS::Region}:${AWS::AccountId}:thing/*"
- Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- 'iot:Publish'
Resource:
- !Sub "arn:${AWS::Partition}:iot:${AWS::Region}:${AWS::AccountId}:topic/dags/*/status/request"
- Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- 'iot:DescribeEndpoint'
Resource: '*'
- Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- 'sqs:SendMessage'
Resource: !Sub ${ConnectionStatusQueue.Arn}
- Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- 'dynamodb:GetItem'
Resource:
- !Sub "arn:${AWS::Partition}:dynamodb:${AWS::Region}:${AWS::AccountId}:table/accounts"
Disconnected:
Type: AWS::Serverless::Function
Properties:
CodeUri: ./connection/disconnected
Environment:
Variables:
SENDER_EMAIL: [email protected]
Handler: main
Runtime: go1.x
Timeout: 5
Policies:
- Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- 'ses:SendEmail'
- 'ses:SendRawEmail'
- 'ses:GetIdentityVerificationAttributes'
Resource: '*'
- Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- 'iot:DescribeThing'
- 'iot:GetThingShadow'
- 'iot:UpdateThingShadow'
Resource:
- !Sub "arn:${AWS::Partition}:iot:${AWS::Region}:${AWS::AccountId}:thing/*"
- Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- 'iot:DescribeEndpoint'
Resource: '*'
- Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- 'dynamodb:GetItem'
Resource:
- !Sub "arn:${AWS::Partition}:dynamodb:${AWS::Region}:${AWS::AccountId}:table/accounts"
- Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- 'sqs:DeleteMessage'
- 'sqs:GetQueueAttributes'
- 'sqs:ReceiveMessage'
Resource: !Sub ${ConnectionStatusQueue.Arn}
ThingPolicy:
Type: AWS::IoT::Policy
Properties:
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- iot:Connect
Resource:
- arn:aws:iot:eu-west-2:670763423833:client/${iot:Connection.Thing.ThingName}
- Effect: Allow
Action:
- iot:Publish
Resource:
- arn:aws:iot:eu-west-2:670763423833:topic/$aws/things/${iot:Connection.Thing.ThingName}/shadow/update
- Effect: Allow
Action:
- iot:Subscribe
Resource:
- arn:aws:iot:eu-west-2:670763423833:topicfilter/$aws/things/${iot:Connection.Thing.ThingName}/shadow/update/*
- arn:aws:iot:eu-west-2:670763423833:topicfilter/dags/${iot:Connection.Thing.ThingName}/status/request
- Effect: Allow
Action:
- iot:Receive
Resource:
- arn:aws:iot:eu-west-2:670763423833:topic/$aws/things/${iot:Connection.Thing.ThingName}/shadow/update/*
- arn:aws:iot:eu-west-2:670763423833:topic/dags/${iot:Connection.Thing.ThingName}/status/request