-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.yaml
560 lines (549 loc) · 19.2 KB
/
stack.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
AWSTemplateFormatVersion: "2010-09-09"
Parameters:
ContactFlowId:
Type: String
SourcePhoneNumber:
Type: String
ConnectInstance:
Type: String
Resources:
SugarMonitor:
Type: AWS::IoT::Thing
SugarMonitorTopicRule:
Type: AWS::IoT::TopicRule
Properties:
TopicRulePayload:
RuleDisabled: false
Sql: >-
SELECT * FROM 'slider'
Actions:
-
Firehose:
RoleArn: !GetAtt SugarMonitorTopicRuleRole.Arn
DeliveryStreamName: !Ref SugarMonitorFirehose
SugarMonitorTopicRuleRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service: iot.amazonaws.com
Action:
- sts:AssumeRole
Path: "/"
Policies:
-
PolicyName: "putrecord"
PolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Action: "firehose:PutRecord"
Resource: !GetAtt SugarMonitorFirehose.Arn
SugarMonitorFirehose:
Type: AWS::KinesisFirehose::DeliveryStream
Properties:
S3DestinationConfiguration:
BufferingHints:
IntervalInSeconds: 60
SizeInMBs: 1
CompressionFormat: UNCOMPRESSED
RoleARN: !GetAtt SugarMonitorFirehoseRole.Arn
BucketARN: !GetAtt SugarMonitorS3.Arn
SugarMonitorFirehoseRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service: firehose.amazonaws.com
Action:
- sts:AssumeRole
Path: "/"
Policies:
-
PolicyName: "writetobucket"
PolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Action:
- "s3:AbortMultipartUpload"
- "s3:GetBucketLocation"
- "s3:GetObject"
- "s3:ListBucket"
- "s3:ListBucketMultipartUploads"
- "s3:PutObject"
Resource:
- !Sub '${SugarMonitorS3.Arn}/*'
- !GetAtt SugarMonitorS3.Arn
SugarMonitorS3:
Type: AWS::S3::Bucket
SugarMonitorKinesisAnalytics:
Type: AWS::KinesisAnalytics::Application
Properties:
ApplicationCode: !Sub |
-- ** Anomaly detection **
-- Compute an anomaly score for each record in the source stream using Random Cut Forest
-- Creates a temporary stream and defines a schema
CREATE OR REPLACE STREAM "MONITOR_STREAM" (
"PatientID" varchar(16),
"DeviceID" INTEGER,
"Type" varchar(16),
"COL_Value" DOUBLE,
"ANOMALY_SCORE" DOUBLE);
CREATE OR REPLACE STREAM "TEMP_STREAM" (
"PatientID" varchar(16),
"DeviceID" INTEGER,
"Type" varchar(16),
"COL_Value" DOUBLE,
"ANOMALY_SCORE" DOUBLE);
-- Creates an output stream and defines a schema
CREATE OR REPLACE STREAM "DESTINATION_SQL_STREAM" (
"PatientID" varchar(16),
"DeviceID" INTEGER,
"Type" varchar(16),
"COL_Value" DOUBLE,
"ANOMALY_SCORE" DOUBLE);
-- Creates an trigger count stream and defines a schema
CREATE OR REPLACE STREAM "TRIGGER_COUNT_STREAM" (
"PatientID" varchar(16),
"DeviceID" INTEGER,
"Type" varchar(16),
"COL_Value" DOUBLE,
"ANOMALY_SCORE" DOUBLE,
"trigger_count" INTEGER);
CREATE OR REPLACE PUMP "MONITOR_PUMP" AS INSERT INTO "MONITOR_STREAM"
SELECT STREAM "PatientID", "DeviceID", "Type", "COL_Value", "ANOMALY_SCORE" FROM
TABLE(RANDOM_CUT_FOREST(
CURSOR(SELECT STREAM "PatientID", "DeviceID", "Type", "COL_Value" FROM "SugarMonitor_001"),
5,
50,
100,
10
)
);
-- Compute an anomaly score for each record in the source stream
-- using Random Cut Forest
CREATE OR REPLACE PUMP "STREAM_PUMP" AS INSERT INTO "TEMP_STREAM"
SELECT STREAM "PatientID", "DeviceID", "Type", "COL_Value", "ANOMALY_SCORE" FROM "MONITOR_STREAM" where "ANOMALY_SCORE" > 1;
CREATE OR REPLACE PUMP "trigger_count_pump" AS INSERT INTO "TRIGGER_COUNT_STREAM"
SELECT STREAM "PatientID", "DeviceID", "Type", "COL_Value", "ANOMALY_SCORE", "trigger_count"
FROM (
SELECT STREAM "PatientID", "DeviceID", "Type", "COL_Value", "ANOMALY_SCORE", COUNT(*) OVER W1 as "trigger_count"
FROM "TEMP_STREAM"
--window to perform aggregations over last 10 minutes to keep track of triggers
WINDOW W1 AS (PARTITION BY "PatientID" RANGE INTERVAL '10' MINUTE PRECEDING)
)
WHERE "trigger_count" >= 1;
-- Sort records by descending anomaly score, insert into output stream
CREATE OR REPLACE PUMP "OUTPUT_PUMP" AS INSERT INTO "DESTINATION_SQL_STREAM"
SELECT STREAM "PatientID", "DeviceID", "Type", "COL_Value", "ANOMALY_SCORE" FROM "TRIGGER_COUNT_STREAM" where "trigger_count" >= 8
ORDER BY FLOOR("TRIGGER_COUNT_STREAM".ROWTIME TO SECOND), ANOMALY_SCORE DESC;
-- CREATE OR REPLACE PUMP "OUTPUT_PUMP" AS INSERT INTO "DESTINATION_SQL_STREAM"
-- SELECT STREAM "PatientID", "DeviceID", "Type", "COL_Value", "ANOMALY_SCORE" FROM "MONITOR_STREAM" where "COL_Value" <= 3.5 OR "COL_Value" >= 7
-- ORDER BY FLOOR("MONITOR_STREAM".ROWTIME TO SECOND), ANOMALY_SCORE DESC;
Inputs:
- NamePrefix: "SugarMonitor"
InputSchema:
RecordColumns:
- Name: "PatientID"
SqlType: "VARCHAR(16)"
Mapping: "$.PatientId"
- Name: "DeviceID"
SqlType: "INTEGER"
Mapping: "$.DeviceId"
- Name: "COL_TimeStamp"
SqlType: "TIMESTAMP"
Mapping: "$.Timestamp"
- Name: "COL_Value"
SqlType: "DOUBLE"
Mapping: "$.Value"
- Name: "Type"
SqlType: "VARCHAR(16)"
Mapping: "$.Type"
- Name: "Unit"
SqlType: "VARCHAR(8)"
Mapping: "$.Unit"
- Name: "GeoData"
SqlType: "VARCHAR(32)"
Mapping: "$.GeoData"
RecordFormat:
RecordFormatType: "JSON"
MappingParameters:
JSONMappingParameters:
RecordRowPath: "$"
KinesisFirehoseInput:
ResourceARN: !GetAtt SugarMonitorFirehose.Arn
RoleARN: !GetAtt SugarMonitorAnalyticsRole.Arn
SugarMonitorAnalyticsRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service: kinesisanalytics.amazonaws.com
Action:
- sts:AssumeRole
Path: "/"
Policies:
-
PolicyName: "readfirehose"
PolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Action:
- "firehose:DescribeStream"
- "firehose:GetShardIterator"
- "firehose:GetRecords"
- "firehose:ListShards"
Resource: !GetAtt SugarMonitorFirehose.Arn
-
Effect: "Allow"
Action:
- "lambda:*"
Resource: !GetAtt SugarMonitorLambdaTrigger.Arn
SugarMonitorAnalyticsOutput:
Type: AWS::KinesisAnalytics::ApplicationOutput
Properties:
ApplicationName: !Ref SugarMonitorKinesisAnalytics
Output:
DestinationSchema:
RecordFormatType: "JSON"
LambdaOutput:
ResourceARN: !GetAtt SugarMonitorLambdaTrigger.Arn
RoleARN: !GetAtt SugarMonitorAnalyticsRole.Arn
Name: DESTINATION_SQL_STREAM
SugarMonitorLambdaTrigger:
Type: "AWS::Lambda::Function"
Properties:
Handler: "index.handler"
Role: !GetAtt SugarMonitorLambdaTriggerRole.Arn
Code:
ZipFile: >
import json
import os
import boto3
import botocore
import base64
import datetime
def handler(event, context):
lockminutes = 50
arn = os.environ['BUCKET']
sourcephone = os.environ['SOURCEPHONE']
contactflowid = os.environ['CONTACTFLOWID']
connectinstance = os.environ['CONNECTINSTANCE']
elements = arn.split(':')
bucket = elements[5]
s3 = boto3.resource('s3')
# get PatientID from event triggered
patRec = json.dumps(event)
b64 = json.loads(patRec)['records'][0]['data']
decoded = base64.b64decode(b64)
patientData = json.loads(decoded)
pID = patientData['PatientID']
print(pID)
# get list of Patients from S3 json file
key = 'private/' + pID + '/config.json'
response = s3.Object(bucket, key).get()
patRecJson = response['Body'].read().decode('utf-8')
json_content = json.loads(patRecJson)
doctorN = json_content['Doctor#']
patientN = json_content['Patient#']
Doctor = json_content['Doctor']
Name = json_content['Name']
# check if timeout
try:
timeout = s3.Object(bucket, pID+'.lock').get()
lmodified = timeout['LastModified'] #Here was a typo
print(lmodified)
flag=0
#Works now. Just test and fix if required
if (lmodified + datetime.timedelta(minutes=lockminutes)) > datetime.datetime.now(datetime.timezone.utc):
print("STOP")
else:
print(datetime.datetime.now(datetime.timezone.utc))
print(datetime.timedelta(minutes=3))
print(lmodified)
print("GO")
flag=1
newFile = s3.Object(bucket, pID+'.lock')
newFile.put(Body="null")
except botocore.exceptions.ClientError as e:
print("nofile" + Name)
newFile = s3.Object(bucket, pID+'.lock')
newFile.put(Body="null")
print("created")
flag=1
if(flag==1):
connect = boto3.client('connect')
response = connect.start_outbound_voice_contact(
DestinationPhoneNumber=patientN,
ContactFlowId=contactflowid,
InstanceId=connectinstance,
SourcePhoneNumber=sourcephone,
Attributes = {
"doctorName": Doctor,
"patientName": Name,
"doctorPhoneNumber": doctorN
}
)
output = []
for record in event['records']:
output.append({'recordId': record['recordId'], 'result': 'Ok'})
return { 'records': output }
Runtime: "python3.7"
Timeout: 25
Environment:
Variables:
BUCKET: !GetAtt SugarMonitorPatientsS3.Arn
SOURCEPHONE: !Ref SourcePhoneNumber
CONTACTFLOWID: !Ref ContactFlowId
CONNECTINSTANCE: !Ref ConnectInstance
SugarMonitorLambdaTriggerRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action:
- sts:AssumeRole
Path: "/"
Policies:
-
PolicyName: "callconnect"
PolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Action:
- "connect:*"
- "logs:*"
Resource: "*"
-
Effect: "Allow"
Action:
- "s3:AbortMultipartUpload"
- "s3:GetBucketLocation"
- "s3:GetObject"
- "s3:ListBucket"
- "s3:ListBucketMultipartUploads"
- "s3:PutObject"
Resource:
- !Sub '${SugarMonitorPatientsS3.Arn}/*'
- !GetAtt SugarMonitorPatientsS3.Arn
SugarMonitorLambdaTriggerLogs:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName:
Fn::Join: ["/", ["/aws/lambda", {"Ref": "SugarMonitorLambdaTrigger"}]]
RetentionInDays: 1
SugarMonitorPatientsS3:
Type: AWS::S3::Bucket
Properties:
CorsConfiguration:
CorsRules:
- AllowedOrigins: ['*']
AllowedMethods: [HEAD, GET, PUT, POST, DELETE]
MaxAge: '3000'
AllowedHeaders: ['*']
ExposedHeaders: [ETag, x-amz-server-side-encryption, x-amz-request-id, x-amz-id-2]
Id: amplifyAccess
SugarMonitorIdPool:
Type: "AWS::Cognito::IdentityPool"
Properties:
AllowUnauthenticatedIdentities: true
SugarMonitorPoolRoleAttach:
Type: "AWS::Cognito::IdentityPoolRoleAttachment"
Properties:
IdentityPoolId: !Ref SugarMonitorIdPool
Roles:
unauthenticated: !GetAtt SugarMonitorIdPoolRole.Arn
authenticated: !GetAtt SugarMonitorIdPoolRole.Arn
SugarMonitorIdPoolRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Federated: cognito-identity.amazonaws.com
Action:
- sts:AssumeRoleWithWebIdentity
Condition:
StringEquals:
cognito-identity.amazonaws.com:aud: !Ref SugarMonitorIdPool
Path: "/"
Policies:
-
PolicyName: "cognito"
PolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Action:
- "mobileanalytics:PutEvents"
- "cognito-sync:*"
- "cognito-identity:*"
- "iot:*"
Resource: "*"
- Effect: Allow
Action:
- s3:GetObject
- s3:PutObject
- s3:DeleteObject
Resource:
- !Sub '${SugarMonitorPatientsS3.Arn}/private/${!cognito-identity.amazonaws.com:sub}/*'
- !Sub '${SugarMonitorPatientsS3.Arn}/private/${!cognito-identity.amazonaws.com:sub}'
- Effect: Allow
Action:
- s3:ListBucket
Resource:
- !Sub '${SugarMonitorPatientsS3.Arn}'
IotEndpointProviderRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action:
- sts:AssumeRole
Path: "/"
Policies:
-
PolicyName: "callconnect"
PolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Action:
- iot:DescribeEndpoint
Resource: "*"
IotEndpointProvider:
Type: "AWS::Lambda::Function"
Properties:
Timeout: 3
Runtime: "python3.7"
Handler: "index.handler"
Role: !GetAtt IotEndpointProviderRole.Arn
Code:
ZipFile: >
import boto3
import json
from botocore.vendored import requests
def handler(event, context):
client = boto3.client('iot')
val = client.describe_endpoint()
response_body = {
"Status": "SUCCESS",
'Reason': 'See the details in CloudWatch Log Stream',
'PhysicalResourceId': 'Custom Lambda Function',
'StackId': event['StackId'],
'RequestId': event['RequestId'],
'LogicalResourceId': event['LogicalResourceId'],
"Data": { "IotEndpointAddress": val['endpointAddress'] }
}
requests.put(event['ResponseURL'], data=json.dumps(response_body))
IotEndpoint:
Type: 'Custom::IotEndpoint'
Properties:
ServiceToken: !GetAtt IotEndpointProvider.Arn
SugarMonitorGlueRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Principal:
Service:
- "glue.amazonaws.com"
Action:
- "sts:AssumeRole"
Path: "/"
Policies:
-
PolicyName: "root"
PolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Action: "*"
Resource:
- !Sub '${SugarMonitorS3.Arn}/*'
- !GetAtt SugarMonitorS3.Arn
-
Effect: "Allow"
Action:
- "logs:*"
Resource: "*"
-
Effect: "Allow"
Action:
- "glue:*"
Resource: "*"
SugarMonitorGlueDatabase:
Type: AWS::Glue::Database
Properties:
CatalogId: !Ref AWS::AccountId
DatabaseInput:
Name: sugarmonitorgluedb
Description: "Glue for patient data"
SugarMonitorGlueCrawler:
Type: AWS::Glue::Crawler
Properties:
Role: !GetAtt SugarMonitorGlueRole.Arn
Schedule:
ScheduleExpression: 'cron(0/15 * ? * * *)'
DatabaseName: !Ref SugarMonitorGlueDatabase
Targets:
S3Targets:
- Path: !Sub 's3://${SugarMonitorS3}'
SchemaChangePolicy:
UpdateBehavior: "UPDATE_IN_DATABASE"
DeleteBehavior: "LOG"
Configuration: "{\"Version\":1.0,\"CrawlerOutput\":{\"Partitions\":{\"AddOrUpdateBehavior\":\"InheritFromTable\"},\"Tables\":{\"AddOrUpdateBehavior\":\"MergeNewColumns\"}}}"
SugarMonitorGlueCrawlerLogs:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName:
Fn::Join: ["/", ["/aws-glue/crawlers", {"Ref": "SugarMonitorGlueCrawler"}]]
RetentionInDays: 1
AthenaNamedQuery:
Type: AWS::Athena::NamedQuery
Properties:
Database: !Ref SugarMonitorGlueDatabase
QueryString: !Sub
- 'select patientid, value, timestamp from ${S3Bucket} order by timestamp asc;'
- S3Bucket: !Join
- '_'
- !Split
- '-'
- !Ref SugarMonitorS3
Outputs:
IdPool:
Description: Cognito IdPool
Value: !Ref SugarMonitorIdPool
PatientBucket:
Description: PatientBucket
Value: !Ref SugarMonitorPatientsS3
IotEndpointAddress:
Value: !GetAtt IotEndpoint.IotEndpointAddress
AthenaNamedQuery:
Value: !Ref AthenaNamedQuery