forked from aws-samples/aws-iot-core-lorawan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.yaml
155 lines (142 loc) · 8.21 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
# 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.
AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Description: >
Send a base64 encoded payload to your LoRaWAN device by publishing to topic cmd/downlink/<AWSIoTCoreforLoRaWAN Device Id>
# ██████ █████ ██████ █████ ███ ███ ███████ ████████ ███████ ██████ ███████
# ██ ██ ██ ██ ██ ██ ██ ██ ████ ████ ██ ██ ██ ██ ██ ██
# ██████ ███████ ██████ ███████ ██ ████ ██ █████ ██ █████ ██████ ███████
# ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
# ██ ██ ██ ██ ██ ██ ██ ██ ██ ███████ ██ ███████ ██ ██ ███████
#
Parameters:
LoRaWANAFPort:
Type: String
Default: 1
Description: FPort to use when sending messages
LoRaWANTransmitMode:
Type: Number
Default: 1
AllowedValues:
- 0
- 1
Description: 0 for UM (unacknowledge mode), 1 for AM (acknowledge mode)
#
# ██████ ███████ ███████ ██████ ██ ██ ██████ ██████ ███████ ███████
# ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
# ██████ █████ ███████ ██ ██ ██ ██ ██████ ██ █████ ███████
# ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
# ██ ██ ███████ ███████ ██████ ██████ ██ ██ ██████ ███████ ███████
#
Resources:
SendDataToWirelessDeviceRule:
Type: "AWS::IoT::TopicRule"
Properties:
RuleName: !Sub "${AWS::StackName}_SendDataToWirelessDeviceRule"
TopicRulePayload:
AwsIotSqlVersion: "2016-03-23"
RuleDisabled: false
Sql: !Sub
- |
SELECT aws_lambda("${LambdaARN}",
{"TransmitMode": ${TransmitMode},
"FPort": ${FPort},
"WirelessDeviceId": topic(3),
"PayloadData": encode(*, 'base64')}) as sendresult
FROM 'cmd/downlink/#'
- {
LambdaARN: !GetAtt SendDataToWirelessDeviceFunction.Arn,
FPort: !Ref LoRaWANAFPort,
TransmitMode: !Ref LoRaWANTransmitMode,
}
Actions:
- Republish:
RoleArn: !GetAtt SendDataToWirelessDeviceRuleRepublishRole.Arn
Topic: downlink/status/${topic(3)}
Qos: 0
ErrorAction:
Republish:
RoleArn: !GetAtt SendDataToWirelessDeviceRuleRepublishRole.Arn
Topic: downlink/error/${topic(3)}
Qos: 0
SendDataToWirelessDeviceRuleRepublishRole:
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/downlink/*",
],
]
IoTPermissionForSendMessageToDeviceFunction:
Type: AWS::Lambda::Permission
Properties:
FunctionName: !GetAtt SendDataToWirelessDeviceFunction.Arn
Action: lambda:InvokeFunction
Principal: iot.amazonaws.com
SendDataToWirelessDeviceFunction:
Type: AWS::Serverless::Function
Name: !Sub "${AWS::StackName}-SendDataToWirelessDeviceFunction"
Properties:
CodeUri: src
Handler: app.lambda_handler
Runtime: python3.7
Timeout: 10
Policies:
- Statement:
- Sid: policy1
Effect: Allow
Action:
- iotwireless:SendDataToWirelessDevice
Resource: "*"
# ██████ ██ ██ ████████ ██████ ██ ██ ████████ ███████
# ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
# ██ ██ ██ ██ ██ ██████ ██ ██ ██ ███████
# ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
# ██████ ██████ ██ ██ ██████ ██ ███████
#
Outputs:
SendMessageToDeviceFunctionFunction:
Description: "Lambda Function ARN"
Value: !GetAtt SendDataToWirelessDeviceFunction.Arn
SampleTopic:
Description: "Use this topic to send a message, replacing <WirelessDeviceId> with the id of your AWS IoT Core for LoRaWAN device"
Value: cmd/downlink/<WirelessDeviceId>
SamplePayload:
Description: "Sample payload (0x00 in base64)"
Value: "AA=="