-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathtemplate.yaml
246 lines (217 loc) · 8.48 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
AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Description: >
Timestream integration
Parameters:
ParamBinaryDecoderName:
Type: String
Default: sample_device
Description: Name of binary decoder as configured in src-iotrule-transformation/app.py
TopicError:
Type: String
Default: dt/lorawanerror
Description: Name of MQTT topic for to publish IoT Rule action error messages
TopicDebug:
Type: String
Default: debug
Description:
|
Prefix of a MQTT topic to publish debugging information.'
Resources:
############################################################################################
# Payload transformation Lambda function to be called from AWS IoT Core. This function will refer to
# layer "DecoderLayer" to include the necessary decoding libraries
############################################################################################
WriteLoRaWANDataToTimestreamFunction:
Type: AWS::Serverless::Function
Name: !Sub '${AWS::StackName}-WriteLoRaWANDataToTimestreamFunction'
Properties:
CodeUri: src-lambda-write-to-timestream
Handler: app.lambda_handler
Runtime: python3.7
Timeout: 10
Environment:
Variables:
DB_NAME: !Ref TimestreamDatabase
TABLE_NAME_TELEMETRY: !Select [ "1", !Split [ "|" , !Ref TimestreamTableTelemetry]]
TABLE_NAME_METADATA: !Select [ "1", !Split [ "|" , !Ref TimestreamTableMetadata]]
Policies:
- Statement:
- Sid: Pol1
Effect: Allow
Action:
- timestream:WriteRecords
Resource: !GetAtt TimestreamTableTelemetry.Arn
- Sid: Pol2
Effect: Allow
Action:
- timestream:WriteRecords
Resource: !GetAtt TimestreamTableMetadata.Arn
- Sid: Pol3
Effect: Allow
Action:
- timestream:DescribeEndpoints
Resource: "*"
WriteLoRaWANDataToTimestreamFunctionPermission:
Type: AWS::Lambda::Permission
Properties:
FunctionName: !GetAtt WriteLoRaWANDataToTimestreamFunction.Arn
Action: lambda:InvokeFunction
Principal: iot.amazonaws.com
TransformLoRaWANBinaryPayloadForTimestreamFunction:
Type: AWS::Serverless::Function
Name: !Sub '${AWS::StackName}TransformLoRaWANBinaryPayloadForTimestreamFunction'
Properties:
CodeUri: src-lambda-transform
Handler: app.lambda_handler
Runtime: python3.7
Layers:
- Ref: LoRaWANPayloadDecoderLayer
Timeout: 10
TransformLoRaWANBinaryPayloadForTimestreamFunctionPermission:
Type: AWS::Lambda::Permission
Properties:
FunctionName: !GetAtt TransformLoRaWANBinaryPayloadForTimestreamFunction.Arn
Action: lambda:InvokeFunction
Principal: iot.amazonaws.com
############################################################################################
# LAYERS
############################################################################################
LoRaWANPayloadDecoderLayer:
Type: AWS::Serverless::LayerVersion
Properties:
LayerName: !Sub '${AWS::StackName}-LoRaWANPayloadDecoderLayer'
Description: Payload decoders for LoRaWAN devices
ContentUri: src-layer-payload-decoders
CompatibleRuntimes:
- python3.8
RetentionPolicy: Retain
############################################################################################
# This IoT Rule converts a binary message to a JSON message using a payload decoder, and then
# writes the payload of message and metadata into a Amazon Timestream database.
# Incoming message topic:
# dt/lorawanbinary/<application id>/<device type id>/<device id>
# Example:
# dt/lorawanbinary/moisture/dragino_lht65/12345678
############################################################################################
WriteLoRaWANDataToTimestreamRule:
Type: "AWS::IoT::TopicRule"
Properties:
RuleName: !Sub '${AWS::StackName}_WriteLoRaWANDataToTimestream_${ParamBinaryDecoderName}'
TopicRulePayload:
AwsIotSqlVersion: "2016-03-23"
RuleDisabled: false
Sql:
!Sub
- |
SELECT aws_lambda("${LambdaARN}",
{"PayloadDecoderName": "${ParamBinaryDecoderName}",
"PayloadData":PayloadData,
"WirelessDeviceId": DeviceId,
"WirelessMetadata": WirelessMetadata}) as transformed_message,
WirelessDeviceId as transformed_message.WirelessDeviceId,
WirelessMetadata.LoRaWAN.DevEui as transformed_message.DevEui,
WirelessDeviceId as lns_message.WirelessDeviceId,
WirelessMetadata as lns_message.WirelessMetadata,
PayloadData as lns_message.PayloadData
- { LambdaARN: !GetAtt TransformLoRaWANBinaryPayloadForTimestreamFunction.Arn }
Actions:
- Lambda:
FunctionArn: !GetAtt WriteLoRaWANDataToTimestreamFunction.Arn
- Republish:
RoleArn: !GetAtt StoreLoRaWANPayloadInTimestreamRuleRepublishActionRole.Arn
Topic:
!Join [
"",
[
!Ref TopicDebug
],
]
Qos: 0
ErrorAction:
Republish:
RoleArn: !GetAtt StoreLoRaWANPayloadInTimestreamRuleRepublishActionRole.Arn
Topic: !Ref TopicError
Qos: 0
StoreLoRaWANPayloadInTimestreamRuleRepublishActionRole:
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/",
!Ref TopicDebug,
"/*"
],
]
- Effect: Allow
Action: iot:Publish
Resource:
!Join [
"",
[
"arn:aws:iot:",
!Ref "AWS::Region",
":",
!Ref "AWS::AccountId",
":topic/",
!Ref TopicError
],
]
TimestreamDatabase:
Type: AWS::Timestream::Database
Properties:
DatabaseName: !Sub ${AWS::StackName}LoRaWANDatabase
TimestreamTableTelemetry:
Type: AWS::Timestream::Table
Properties:
TableName: !Sub ${AWS::StackName}LoRaWANTelemetryTable
DatabaseName:
Ref: TimestreamDatabase
RetentionProperties:
MemoryStoreRetentionPeriodInHours: '24'
MagneticStoreRetentionPeriodInDays: '5'
TimestreamTableMetadata:
Type: AWS::Timestream::Table
Properties:
TableName: !Sub ${AWS::StackName}LoRaWANMetadataTable
DatabaseName:
Ref: TimestreamDatabase
RetentionProperties:
MemoryStoreRetentionPeriodInHours: '24'
MagneticStoreRetentionPeriodInDays: '5'
Outputs:
DatabaseName:
Description: "Database Name"
Value: !Ref TimestreamDatabase
TableNameTelemetry:
Description: "Table Name for Telemetry "
Value: !Select [ "1", !Split [ "|" , !Ref TimestreamTableTelemetry]]
TableNameMetadata:
Description: "Table Name for Metadata"
Value: !Select [ "1", !Split [ "|" , !Ref TimestreamTableMetadata]]
WriteLoRaWANDataToTimestreamFunctionArn:
Value: !Ref WriteLoRaWANDataToTimestreamFunction
TransformLoRaWANBinaryPayloadForTimestreamFunctionArn:
Value: !Ref TransformLoRaWANBinaryPayloadForTimestreamFunction