-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnalyticsDestinationFunction.py
48 lines (43 loc) · 1.52 KB
/
AnalyticsDestinationFunction.py
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
# Creating a Lambda package with runtime dependencies
# https://docs.aws.amazon.com/lambda/latest/dg/python-package-create.html#python-package-create-with-dependency
from datetime import datetime
import boto3
import os
import base64
import json
dynamodb = boto3.client('dynamodb')
output_table = os.environ.get('OUTPUT_TABLE_NAME')
def handler(event, context):
# Response will be a list of records.
response = {
'records': []
}
for record in event['Records']:
payload = json.loads(base64.b64decode(record.get('kinesis').get('data')).decode('utf-8'))
try:
dynamodb.put_item(TableName=output_table,
Item={
'timestamp': {
'S': str(datetime.now())
},
'location': {
'S': payload['location']
},
'wind_speed': {
'S': str(payload['speed'])
},
'anomaly_score': {
'S': str(payload['score'])
}
})
response['records'].append({
'recordId': record.get('recordId'),
'result': 'Ok'
})
except Exception as e:
print(e)
response['records'].append({
'recordId': record.get('recordId'),
'result': 'DeliveryFailed'
})
return response