-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynamodb_trigger_lambda.py
71 lines (58 loc) · 2.24 KB
/
dynamodb_trigger_lambda.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import json
import boto3
import os
import re
from decimal import Decimal
from datetime import datetime
print('Loading function')
"""
This function takes in a ugly nested dictionary and returns out a dictionary
"""
def clean_data(nest_dict):
new_dict = {}
for k,v in nest_dict.items():
for k2,v2 in v.items():
new_dict[k] = v2
return new_dict
def lambda_handler(event, context):
# Types of events to store in backup table
must_copy = ["REMOVE", "INSERT"]
for record in event['Records']:
rec_type = record["eventName"]
if rec_type in must_copy:
block = record['dynamodb']
time_create = block['ApproximateCreationDateTime']
readable_time = datetime.utcfromtimestamp(time_create).strftime('%Y-%m-%d %H:%M:%S')
# Getting the primary key of the record
primary_key = clean_data(block['Keys'])
# Getting the whole record
try:
the_image = clean_data(block['NewImage'])
except KeyError:
the_image = clean_data(block['OldImage'])
"""
Figuring out which table triggered this lambda and
where to store the backup logs
"""
arn_address = record['eventSourceARN']
x = re.search(r"table\/(\w+)\/", arn_address)
name = x.group(1) + "_Backup"
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table(name)
# try:
response = table.put_item(
Item={
'Time': Decimal(time_create),
'Readable_Time': readable_time,
'Type': rec_type,
'Keys': primary_key,
'Image': the_image
}
)
print("PutItem succeeded")
# except Exception:
# print("Something Went Wrong")
print(record['eventID'])
print(record['eventName'])
print("DynamoDB Record: " + json.dumps(record['dynamodb'], indent=2))
return 'Successfully processed {} records.'.format(len(event['Records']))