-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathtemplate.yaml
376 lines (345 loc) · 18.8 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
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this
# software and associated documentation files (the "Software"), to deal in the Software
# without restriction, including without limitation the rights to use, copy, modify,
# merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
#
# The purpose of this template is to create two IoT Rules in your AWS Account:
# - <stack name>_UpdateShadowWithLoRaWANPayload_<binary decoder name>
# - <stack name>_UpdateShadowWithLoRaWANPayload_MapThingName_<binary decoder name>
# Both rules will update a shadow of an AWS IoT Thing with a decoded payload from a LoRaWAN device. The
# difference between both rules is how they detect the name of the AWS IoT Thing for the shadow update:
#
# - The rule "<stack name>_UpdateShadowWithLoRaWANPayload_<binary decoder name>" will update a
# shadow of AWS IoT Thing with the name derived from the value of the attribute "WirelessDeviceId"
#
# - The rule "<stack name>_UpdateShadowWithLoRaWANPayload_MapThingName_<binary decoder name>" allows
# customization of the logic do derive a Thing name throught AWS Lambda function. An example Lambda function
# in this sample will perform a lookup in the IoT Registry based on the attribute name of the thing.
AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Description: >
Sample for publishing the telemetry and transmission metadata from a LoRaWAN device to the shadow of the AWS IoT Thing
# ██████ █████ ██████ █████ ███ ███ ███████ ████████ ███████ ██████ ███████
# ██ ██ ██ ██ ██ ██ ██ ██ ████ ████ ██ ██ ██ ██ ██ ██
# ██████ ███████ ██████ ███████ ██ ████ ██ █████ ██ █████ ██████ ███████
# ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
# ██ ██ ██ ██ ██ ██ ██ ██ ██ ███████ ██ ███████ ██ ██ ███████
#
Parameters:
# Name of binary decode to transform incoming binary LoRaWAN Payloads to JSON.
# For the demonstration purposes you can use the included sample decoder which will generate
# the payload like this:
# {
# temperature": 26.55 -> random number
# "humidity": 42.44, -> random number
# "input_length": 11 -> length of payload received from a LoRaWAN device
# "input_hex": "CBC4091501700109027FFF" -> payload received from a LoRaWAN device
# }
# Please review src-iotrule-transformation/app.py for instructions on how to add
# a binary decoder for your LoRaWAN device.
ParamBinaryDecoderName:
Type: String
Default: sample_device
Description: Name of binary decoder as configured in src-iotrule-transformation/app.py
# The IoT Rule will publish error messages on the topic configured below.
TopicOutgoingErrors:
Type: String
Default: iotthingshadowsample_error
Description: Topic for errors
# The IoT Rule will publish debug messages on the topic configured below.
TopicOutgoingDebug:
Type: String
Default: iotthingshadowsample_debug
Description: Name of topic to publish debug messages to
# The parameter below specifies a name of the IoT Thing shadow to update
ParamShadowName:
Type: String
Default: LoRaWANTelemetry
# The attribute according to the JSON path specified below will be used to
# detect the name of the IoT Thing for shadow update.
# Example: if you specify "WirelessDeviceId" and the incoming payload is
# {"WirelessDeviceId": # "8b00de4a-0fac-407b-93e6-8c59fd411f16",..."}
# , the shadow of the AWS IoT Thing with the name "8b00de4a-0fac-407b-93e6-8c59fd411f16" will be updated.
ParamShadowThingNamePath:
Type: String
Default: WirelessDeviceId
AllowedValues:
- WirelessDeviceId
- WirelessMetadata.LoRaWAN.DevEui
Description: JSON path to retrieve the name of the shadow IoT Thing
#
# ██████ ███████ ███████ ██████ ██ ██ ██████ ██████ ███████ ███████
# ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
# ██████ █████ ███████ ██ ██ ██ ██ ██████ ██ █████ ███████
# ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
# ██ ██ ███████ ███████ ██████ ██████ ██ ██ ██████ ███████ ███████
#
Resources:
############################################################################################
# This AWS IoT Rule rule will update a shadow of an AWS IoT Thing with a decoded payload from a LoRaWAN
# device. It will use of attributes of the incoming payload message (e.g. value of attribute
# WirelessDeviceId) as a Thing name
############################################################################################
UpdateShadowWithLoRaWANPayloadRule:
Type: "AWS::IoT::TopicRule"
Properties:
RuleName: !Sub "${AWS::StackName}_UpdateShadowWithLoRaWANPayload_${ParamBinaryDecoderName}"
TopicRulePayload:
AwsIotSqlVersion: "2016-03-23"
RuleDisabled: false
Sql: !Sub
- |
SELECT aws_lambda("${LambdaARN}",
{"PayloadDecoderName": "${ParamBinaryDecoderName}",
"PayloadData":PayloadData,
"WirelessDeviceId": WirelessDeviceId,
"WirelessMetadata": WirelessMetadata}) as state.reported
- { LambdaARN: !GetAtt TransformLoRaWANBinaryPayloadFunction.Arn }
Actions:
- Republish:
RoleArn: !GetAtt UpdateShadowWithLoRaWANPayloadRuleActionRole.Arn
Topic:
!Join [
"",
[
"$$aws/things/${",
!Ref ParamShadowThingNamePath,
"}/shadow/name/",
!Ref ParamShadowName,
"/update",
],
]
Qos: 0
- Republish:
RoleArn: !GetAtt UpdateShadowWithLoRaWANPayloadRuleActionRole.Arn
Topic: !Join ["", [!Ref TopicOutgoingDebug]]
Qos: 0
ErrorAction:
Republish:
RoleArn: !GetAtt UpdateShadowWithLoRaWANPayloadRuleActionRole.Arn
Topic: !Ref TopicOutgoingErrors
Qos: 0
# Permission for AWS IoT Rule action
UpdateShadowWithLoRaWANPayloadRuleActionRole:
Type: "AWS::IAM::Role"
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- iot.amazonaws.com
Action:
- "sts:AssumeRole"
Policies:
- PolicyName: root
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action: iot:Publish
Resource:
!Join [
"",
[
"arn:aws:iot:",
!Ref "AWS::Region",
":",
!Ref "AWS::AccountId",
":topic/*",
],
]
############################################################################################
# This AWS IoT Rule rule will update a shadow of an AWS IoT Thing with a decoded payload from a LoRaWAN
# device. It allows customization of the logic do derive a Thing name throught AWS Lambda function. An
# example Lambda function. In this sample will perform a lookup in the IoT Registry based on the attribute # name of the thing.
############################################################################################
UpdateShadowWithLoRaWANPayloadMappedThingNameRule:
Type: "AWS::IoT::TopicRule"
Properties:
RuleName: !Sub "${AWS::StackName}_UpdateShadowWithLoRaWANPayload_MapThingName_${ParamBinaryDecoderName}"
TopicRulePayload:
AwsIotSqlVersion: "2016-03-23"
RuleDisabled: false
Sql: !Sub
- |
SELECT aws_lambda("${LambdaARN}",
{"PayloadDecoderName": "${ParamBinaryDecoderName}",
"PayloadData":PayloadData,
"WirelessDeviceId": WirelessDeviceId,
"WirelessMetadata": WirelessMetadata}) as state.reported
- { LambdaARN: !GetAtt TransformLoRaWANBinaryPayloadFunction.Arn }
Actions:
- Republish:
RoleArn: !GetAtt UpdateShadowWithLoRaWANPayloadRuleActionRole.Arn
Topic:
!Join [
"",
[
"$$aws/things/${",
"aws_lambda(",
'"',
!GetAtt MapThingNameFunction.Arn,
'"',
", {'searchvalue': WirelessDeviceId, 'searchtype': 'ASSOCIATED_THING'}).ThingName",
"}/shadow/name/",
!Ref ParamShadowName,
"/update",
],
]
Qos: 0
- Republish:
RoleArn: !GetAtt UpdateShadowWithLoRaWANPayloadRuleActionRole.Arn
Topic: !Join ["", [!Ref TopicOutgoingDebug]]
Qos: 0
ErrorAction:
Republish:
RoleArn: !GetAtt UpdateShadowWithLoRaWANPayloadRuleActionRole.Arn
Topic: !Ref TopicOutgoingErrors
Qos: 0
#########################################################################################################
# AWS Lambda function for mapping of the message payload to the name of AWS IoT Thing for shadow update #
#########################################################################################################
MapThingNameFunction:
Type: AWS::Serverless::Function
Name: !Sub "${AWS::StackName}-MapThingNameFunction"
Properties:
CodeUri: src-mapthingname
Handler: app.lambda_handler
Runtime: python3.7
Timeout: 10
Environment:
Variables:
# Allowed valeu for SEARCH_TYPE:
# ASSOCIATED_THING: Lookup the Thing associated to WirelessDeviceId (https://docs.aws.amazon.com/iot-wireless/2020-11-22/apireference/API_AssociateWirelessDeviceWithThing.html)
# THING_INDEX: Lookup the Thing by search of IoT Registry index. If using THING_INDEX, also specify parameter SEARCH_THING_ATTRIBUTENAME to define name of an attribute for matching in thing index,
# for example:
# SEARCH_THING_ATTRIBUTENAME: WirelessDeviceId
SEARCH_TYPE: ASSOCIATED_THING
Policies:
- Statement:
- Sid: AllowSearchInIotIndex
Effect: Allow
Action: iot:SearchIndex
Resource:
!Join [
"",
[
"arn:aws:iot:",
!Ref "AWS::Region",
":",
!Ref "AWS::AccountId",
":index/AWS_Things",
],
]
- Sid: AllowGetWirelessDevice
Effect: Allow
Action: iotwireless:GetWirelessDevice
Resource:
!Join [
"",
[
"arn:aws:iotwireless:",
!Ref "AWS::Region",
":",
!Ref "AWS::AccountId",
":WirelessDevice/*",
],
]
- Sid: AllowDescribeThibg
Effect: Allow
Action: iot:DescribeThing
Resource:
!Join [
"",
[
"arn:aws:iot:",
!Ref "AWS::Region",
":",
!Ref "AWS::AccountId",
":thing/*",
],
]
# Provide AWS IoT a permission to invoke the lambda function
MapThingNameFunctionInvocationPermission:
Type: AWS::Lambda::Permission
Properties:
SourceArn:
!Join [
"",
[
"arn:aws:iot:",
!Ref "AWS::Region",
":",
!Ref "AWS::AccountId",
":rule/",
!Ref UpdateShadowWithLoRaWANPayloadMappedThingNameRule,
],
]
Action: lambda:InvokeFunction
Principal: iot.amazonaws.com
FunctionName: !Ref MapThingNameFunction
############################################################################################
# AWS Lambda function for binary decoding. This function will refer to
# layer "DecoderLayer" to include the necessary decoding libraries
############################################################################################
TransformLoRaWANBinaryPayloadFunction:
Type: AWS::Serverless::Function
Name: !Sub "${AWS::StackName}-TransformLoRaWANBinaryPayloadFunction"
Properties:
CodeUri: src-iotrule-transformation
Handler: app.lambda_handler
Runtime: python3.7
Layers:
- Ref: LoRaWANPayloadDecoderLayer
Environment:
Variables:
RETURN_RAW_DATA: True
# Provide AWS IoT a permission to invoke the lambda function
TransformLoRaWANBinaryPayloadFunctionPermission:
Type: AWS::Lambda::Permission
Properties:
FunctionName: !GetAtt TransformLoRaWANBinaryPayloadFunction.Arn
Action: lambda:InvokeFunction
Principal: iot.amazonaws.com
############################################################################################
# AWS Lambda layer with decoders
############################################################################################
LoRaWANPayloadDecoderLayer:
Type: AWS::Serverless::LayerVersion
Properties:
LayerName: !Sub "${AWS::StackName}-LoRaWANPayloadDecoderLayer"
Description: Payload decoders for LoRaWAN devices
ContentUri: src-payload-decoders
CompatibleRuntimes:
- python3.8
RetentionPolicy: Retain
# ██████ ██ ██ ████████ ██████ ██ ██ ████████ ███████
# ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
# ██ ██ ██ ██ ██ ██████ ██ ██ ██ ███████
# ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
# ██████ ██████ ██ ██ ██████ ██ ███████
#
Outputs:
UpdateShadowWithLoRaWANPayloadRuleName:
Description: >
Option 1: Please use the value below to configure the Destination in AWS IoT Core for LoRaWAN. This rule will will update a shadow of an AWS IoT Thing with the name derived from the value of the attribute WirelessDeviceId. For example, if incoming payload is {"WirelessDeviceId": # "8b00de4a-0fac-407b-93e6-8c59fd411f16",..."}, the shadow of the AWS IoT Thing with the name "8b00de4a-0fac-407b-93e6-8c59fd411f16" will be updated with a decoded payload.
To test this rule, please publish the payload below to the topic
Value: !Ref UpdateShadowWithLoRaWANPayloadRule
UpdateShadowWithLoRaWANPayloadMappedThingNameRuleName:
Description: >
Option 2: please use the value below to configure the Destination in AWS IoT Core for LoRaWAN. This rule will will update a shadow of an AWS IoT Thing with the name calculated by the AWS Lambda function <stack name>-MapThingNameFunction". For example, if incoming payload is {"WirelessDeviceId": # "8b00de4a-0fac-407b-93e6-8c59fd411f16",..."}, the Lambda function will make a lookup in the AWS registry for a Thing with an attribue 'WirelessDeviceId=8b00de4a-0fac-407b-93e6-8c59fd411f16'. If it finds such a thing, the shadow of this thing will be updated with a decoded payload.
Value: !Ref UpdateShadowWithLoRaWANPayloadMappedThingNameRule