-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloud-formation.yml
288 lines (262 loc) · 11.5 KB
/
cloud-formation.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
AWSTemplateFormatVersion: "2010-09-09"
Description: "Creates skeleton solution for Chariot Solutions 'IoT on AWS' workshop"
Parameters:
StreamName:
Description: "Name of the Kinesis stream"
Type: "String"
Default: "iot-data"
DynamoTableName:
Description: "Name of the DynamoDB table"
Type: "String"
Default: "environment"
InfluxLambdaName:
Description: "Name of the Lamba Function that writes data from Kinesis to InfluxDB"
Type: "String"
Default: "kinesis-to-influxdb"
PostgresLambdaName:
Description: "Name of the Lamba Function that writes data from Kinesis to PostgreSQL"
Type: "String"
Default: "kinesis-to-postgres"
Resources:
KinesisStream:
Type: "AWS::Kinesis::Stream"
Properties:
Name: !Ref StreamName
ShardCount: 1
KinesisWriterPolicy:
Type: "AWS::IAM::ManagedPolicy"
Properties:
ManagedPolicyName: !Sub "KinesisWriter-${StreamName}"
Description: "Allows logger to write the example Kinesis stream"
PolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Action:
- "kinesis:CreateStream"
- "kinesis:DescribeStream"
- "kinesis:IncreaseStreamRetentionPeriod"
- "kinesis:PutRecords"
- "kinesis:PutRecord"
Resource: !GetAtt KinesisStream.Arn
KinesisReaderPolicy:
Type: "AWS::IAM::ManagedPolicy"
Properties:
ManagedPolicyName: !Sub "KinesisReader-${StreamName}"
Description: "Allows reading the example Kinesis stream"
PolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Action:
- "kinesis:ListStreams"
- "kinesis:DescribeStream"
- "kinesis:GetShardIterator"
- "kinesis:GetRecords"
Resource: !GetAtt KinesisStream.Arn
DynamoTable:
Type: "AWS::DynamoDB::Table"
Properties:
TableName: !Ref DynamoTableName
AttributeDefinitions:
-
AttributeName: "device"
AttributeType: "S"
-
AttributeName: "timestamp"
AttributeType: "N"
KeySchema:
-
AttributeName: "device"
KeyType: "HASH"
-
AttributeName: "timestamp"
KeyType: "RANGE"
ProvisionedThroughput:
ReadCapacityUnits: 10
WriteCapacityUnits: 5
DynamoWriterPolicy:
Type: "AWS::IAM::ManagedPolicy"
Properties:
ManagedPolicyName: !Sub "DynamoWriter-${DynamoTableName}"
Description: "Allows writes to the example DynamoDB table"
PolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Action:
- "dynamodb:PutItem"
Resource: !GetAtt DynamoTable.Arn
DynamoReaderPolicy:
Type: "AWS::IAM::ManagedPolicy"
Properties:
ManagedPolicyName: !Sub "DynamoReader-${DynamoTableName}"
Description: "Allows reading the example DynamoDB table"
PolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Action:
- "dynamodb:GetItem"
- "dynamodb:Query"
- "dynamodb:Scan"
Resource: !GetAtt DynamoTable.Arn
# this is global
IoTCoreExecutionRole:
Type: "AWS::IAM::Role"
Properties:
RoleName: "iot-core-execution-role"
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
Effect: "Allow"
Action: "sts:AssumeRole"
Principal:
Service: "iot.amazonaws.com"
ManagedPolicyArns:
- !Ref DynamoWriterPolicy
- !Ref KinesisWriterPolicy
InfluxLambdaRole:
Type: "AWS::IAM::Role"
Properties:
Path: "/lambda/"
RoleName: !Sub "${InfluxLambdaName}-ExecutionRole"
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
Effect: "Allow"
Principal:
Service: "lambda.amazonaws.com"
Action: "sts:AssumeRole"
ManagedPolicyArns:
- "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
- !Ref KinesisReaderPolicy
PostgresLambdaRole:
Type: "AWS::IAM::Role"
Properties:
Path: "/lambda/"
RoleName: !Sub "${PostgresLambdaName}-ExecutionRole"
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
Effect: "Allow"
Principal:
Service: "lambda.amazonaws.com"
Action: "sts:AssumeRole"
ManagedPolicyArns:
- "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
- !Ref KinesisReaderPolicy
InfluxLambdaFunction:
Type: "AWS::Lambda::Function"
Properties:
FunctionName: !Ref InfluxLambdaName
Description: "Lambda triggered by Kinesis stream that writes data to InfluxDB."
Role: !GetAtt InfluxLambdaRole.Arn
# Layer is a public Chariot Solutions layer that contains PostgreSQL and InfluxDB
Layers:
- arn:aws:lambda:us-east-1:366425516243:layer:IotWorkshopLayer:1
Runtime: "python3.7"
Handler: "index.lambda_handler"
Code:
ZipFile: |
import json
import base64
def lambda_handler(event, context):
# log the event
print(json.dumps(event))
# decode and log each record
for record in event['Records']:
payload = record['kinesis']['data']
decoded = base64.b64decode(payload)
data = json.loads(decoded)
print(data)
# save_to_influx(data)
MemorySize: 128
Timeout: 60
Environment:
Variables:
INFLUXDB_HOSTNAME: "iot.chariotsolutions.dev"
INFLUXDB_USER: "student"
INFLUXDB_PASSWORD: "hydrogen-5"
INFLUXDB_DATABASE: "workshop"
PostgresLambdaFunction:
Type: "AWS::Lambda::Function"
Properties:
FunctionName: !Ref PostgresLambdaName
Description: "Lambda triggered by Kinesis stream that writes data to PostgreSQL"
Role: !GetAtt PostgresLambdaRole.Arn
# Layer is a public Chariot Solutions layer that contains PostgreSQL and InfluxDB
Layers:
- arn:aws:lambda:us-east-1:366425516243:layer:IotWorkshopLayer:1
Runtime: "python3.7"
Handler: "index.lambda_handler"
Code:
ZipFile: |
import json
import base64
def lambda_handler(event, context):
# log the event
print(json.dumps(event))
# decode and log each record
for record in event['Records']:
payload = record['kinesis']['data']
decoded = base64.b64decode(payload)
data = json.loads(decoded)
print(data)
# save_to_postgres(data)
MemorySize: 128
Timeout: 60
Environment:
Variables:
POSTGRESQL_HOSTNAME: "workshop.cwjhgjkfwasa.us-east-1.rds.amazonaws.com"
POSTGRESQL_USER: "student"
POSTGRESQL_PASSWORD: "lemon.box"
POSTGRESQL_DATABASE: "workshop"
# The event source block attaches a trigger to a lambda, but it fails on the initial import because Kinesis takes too long to create the stream
# EventSource:
# Type: "AWS::Lambda::EventSourceMapping"
# Properties:
# EventSourceArn: !Sub "arn:aws:kinesis:${AWS::Region}:${AWS::AccountId}:stream/${StreamName}"
# FunctionName: !Ref InfluxLambdaFunction
# Enabled: true
# StartingPosition: LATEST
# BatchSize: 100
# MaximumBatchingWindowInSeconds: 30
# Restrictive policy for devices
ThingPolicy:
Type: 'AWS::IoT::Policy'
Properties:
PolicyName: ThingPolicy
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action: 'iot:Connect'
Resource:
- !Sub 'arn:aws:iot:${AWS::Region}:${AWS::AccountId}:client/${!iot:Certificate.Subject.CommonName}'
- Effect: Allow
Action:
- 'iot:Publish'
- 'iot:Receive'
Resource:
- !Sub 'arn:aws:iot:${AWS::Region}:${AWS::AccountId}:topic/things/${!iot:ClientId}/*'
- Effect: Allow
Action: 'iot:Subscribe'
Resource:
- !Sub 'arn:aws:iot:${AWS::Region}:${AWS::AccountId}:topicfilter/things/${!iot:ClientId}/*'
# Wide open policy for debugging Things
AllowAllPolicy:
Type: 'AWS::IoT::Policy'
Properties:
PolicyName: AllowAllPolicy
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action: 'iot:*'
Resource:
- '*'