-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathModule8-Lambda-Code.py
More file actions
67 lines (44 loc) · 2.28 KB
/
Copy pathModule8-Lambda-Code.py
File metadata and controls
67 lines (44 loc) · 2.28 KB
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
import json
import base64
import boto3
from datetime import datetime
def lambda_handler(event, context):
client = boto3.client('dynamodb')
#print("Received event: " + json.dumps(event, indent=2))
for record in event['Records']:
# Kinesis data is base64 encoded so decode here
t_record = base64.b64decode(record['kinesis']['data'])
# decode the bytes into a string
str_record = str(t_record,'utf-8')
#transform the json string into a dictionary
dict_record = json.loads(str_record)
# create Customer Row
############################
customer_key = dict()
customer_key.update({'CustomerID': {"N": str(dict_record['CustomerID'])}})
ex_customer = dict()
ex_customer.update({str(dict_record['InvoiceNo']): {'Value':{"S":'Some overview JSON for the UI goes here'},"Action":"PUT"}})
response = client.update_item(TableName='Customers', Key = customer_key, AttributeUpdates = ex_customer)
# If we have multiple items the response will be :
# items = {'account_type': {'S': 'standard_user'}, 'username': {'S': 'johndoe'}, 'first_name': {'S': 'John'}}
# Specify the table name
# table_name = 'YourTableName'
# response = client.batch_write_item(RequestItems={table_name: items})
# Create Inventory Row
#############################
inventory_key = dict()
inventory_key.update({'InvoiceNo': {"N": str(dict_record['InvoiceNo'])}})
#create export dictionary
ex_dynamoRecord = dict()
#remove Invoice and Stock code from dynmodb record
stock_dict = dict(dict_record)
stock_dict.pop('InvoiceNo',None)
stock_dict.pop('StockCode',None)
#turn the dict into a json
stock_json = json.dumps(stock_dict)
#create a record (column) for the InvoiceNo
#add the stock json to the column with the name of the stock number
ex_dynamoRecord.update({str(dict_record['StockCode']): {'Value':{"S":stock_json},"Action":"PUT"}})
#print(ex_dynamoRecord)
response = client.update_item(TableName='Invoices', Key = inventory_key, AttributeUpdates = ex_dynamoRecord)
return 'Successfully processed {} records.'.format(len(event['Records']))