-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathiot-backend.yaml
270 lines (253 loc) · 7.26 KB
/
iot-backend.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
AWSTemplateFormatVersion: 2010-09-09
Transform: 'AWS::Serverless-2016-10-31'
Description: Creating AWS IoT Serverless Backend Resources.
Parameters:
SNSEmail:
Type: String
Default: '[email protected]'
InstanceType:
Description: Divive simulator EC2 instance type
Type: String
Default: t2.micro
AllowedValues:
- t2.micro
ConstraintDescription: must be a valid EC2 instance type.
SSMLocation:
Description: The IP address range that can be used to SSM to the EC2 instances
Type: String
MinLength: '9'
MaxLength: '18'
Default: 0.0.0.0/0
AllowedPattern: "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/(\\d{1,2})"
ConstraintDescription: must be a valid IP CIDR range of the form x.x.x.x/x.
Mappings:
RegionMap:
us-east-1:
AMI: "ami-00068cd7555f543d5"
us-east-2:
AMI: "ami-0dacb0c129b49f529"
us-west-1:
AMI: "ami-0b2d8d1abb76a53d8"
us-west-2:
AMI: "ami-0c5204531f799e0c6"
eu-west-1:
AMI: "ami-01f14919ba412de34"
Resources:
ECSSMRole:
Type: AWS::IAM::Role
Properties:
Path: /
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service:
- ec2.amazonaws.com
Action:
- sts:AssumeRole
Policies:
- PolicyName: root
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- 'iot:*'
Resource: '*'
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AmazonEC2RoleforSSM
Description: "IAM Role for IoT Simulator instances"
ECInstanceProfile:
Type: AWS::IAM::InstanceProfile
Properties:
Path: /
Roles:
- !Ref 'ECSSMRole'
EC2Instance:
Type: AWS::EC2::Instance
DependsOn: ECSSMRole
Properties:
InstanceType:
Ref: InstanceType
SecurityGroups:
- Ref: InstanceSecurityGroup
IamInstanceProfile: !Ref ECInstanceProfile
Tags:
- Key: "Name"
Value: "EC2 Instance for IoT Simulator"
ImageId: !FindInMap [RegionMap, !Ref "AWS::Region", AMI]
UserData:
Fn::Base64: !Sub |
#!/bin/bash
yum update -y aws-cli
yum update -y aws-cfn-bootstrap
yum install -y python37
cd ~
yum install -y git
git clone https://github.com/aws/aws-iot-device-sdk-python.git
cd aws-iot-device-sdk-python
python3 setup.py install
cd ~
aws s3 cp s3://awslambda-reference-architectures/iot-backend/v2/iot-backend.sh .
aws s3 cp s3://awslambda-reference-architectures/iot-backend/v2/environment.zip .
chmod 744 iot-backend.sh
cp -r * /home/ec2-user
cd /home/ec2-user
unzip environment.zip
aws configure set region ${AWS::Region}
sh iot-backend.sh
mv serverless-iot-backend.cert.pem environment/certs/certificate.pem.crt
mv serverless-iot-backend.private.key environment/certs/private.pem.key
cd environment
IOT_ENDPOINT=$(aws iot describe-endpoint --endpoint-type iot:Data-ATS)
python3 app.py "$IOT_ENDPOINT"
InstanceSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Enable SSM access via port 22
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: '22'
ToPort: '22'
CidrIp:
Ref: SSMLocation
IoTPolicy:
Type: AWS::IoT::Policy
Properties:
PolicyName: 'serverless-iot-backend-policy'
PolicyDocument:
Version: 2012-10-17
Statement:
-
Sid: ManageConnections
Effect: Allow
Action:
- iot:*
Resource: '*'
IoTThing:
Type: AWS::IoT::Thing
Properties:
ThingName: 'serverless-iot-backend-thing'
IoTTopicRule:
Type: AWS::IoT::TopicRule
Properties:
RuleName: 'SoilHumidityTestRule'
TopicRulePayload:
RuleDisabled: false
Sql: Select * FROM 'device/+/devicePayload'
Actions:
-
S3:
BucketName:
Ref: RuleFilterBucket
RoleArn: !GetAtt IoTRuleRole.Arn
Key: '${topic()}/${timestamp()}'
RuleFilterBucket:
Type: AWS::S3::Bucket
IoTRuleRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
-
Effect: "Allow"
Principal:
Service: "iot.amazonaws.com"
Action: "sts:AssumeRole"
ManagedPolicyArns:
- Ref: LambdaPublisherS3PutPolicy
LambdaPublisherS3PutPolicy:
Type: AWS::IAM::ManagedPolicy
Properties:
ManagedPolicyName: LambdaPublisherS3PutPolicy
PolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Action:
- s3:PutObject
Resource:
- !Sub arn:aws:s3:::${RuleFilterBucket}/*
- !Sub arn:aws:s3:::${RuleFilterBucket}
AlertSNSTopic:
Type: 'AWS::SNS::Topic'
Properties:
Subscription:
- Endpoint: !Ref SNSEmail
Protocol: email
IoTFunction:
Type: 'AWS::Serverless::Function'
DependsOn: AlertSNSTopic
Properties:
Handler: lambda_function.lambda_handler
Role: !GetAtt LambdaExecutionRole.Arn
CodeUri: 's3://awslambda-reference-architectures/iot-backend/v2/lambda_function.zip'
Timeout: 10
Runtime: python3.7
Events:
PutImageEvent:
Type: IoTRule
Properties:
Sql: Select * FROM 'device/+/devicePayload' where humidity < 35
Environment:
Variables:
alert_sns: !Ref AlertSNSTopic
LambdaExecutionRole:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- 'sts:AssumeRole'
Path: /
Policies:
- PolicyName: cwlogs
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action: 'sns:*'
Resource: '*'
- Effect: Allow
Action: 'iot:*'
Resource: '*'
- Effect: Allow
Action: 's3:*'
Resource: '*'
- Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
- cloudwatch:*
Resource: "*"
Outputs:
InstanceId:
Description: InstanceId of the newly created EC2 instance
Value:
Ref: EC2Instance
AZ:
Description: Availability Zone of the newly created EC2 instance
Value:
Fn::GetAtt:
- EC2Instance
- AvailabilityZone
PublicDNS:
Description: Public DNSName of the newly created EC2 instance
Value:
Fn::GetAtt:
- EC2Instance
- PublicDnsName
PublicIP:
Description: Public IP address of the newly created EC2 instance
Value:
Fn::GetAtt:
- EC2Instance
- PublicIp