-
Notifications
You must be signed in to change notification settings - Fork 248
/
write_data_to_dynamodb.py
78 lines (57 loc) · 2.85 KB
/
write_data_to_dynamodb.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
72
73
74
75
"""
Lambda function that decodes and writes data from your portfolio website to your DynamoDB database.
Author: Explore Data Science Academy.
Note:
---------------------------------------------------------------------
The contents of this file should be added to a AWS Lambda function
created as part of the EDSA Cloud-Computing Predict.
For further guidance around this process, see the README instruction
file which sits at the root of this repo.
---------------------------------------------------------------------
"""
# Lambda dependencies
import boto3 # Python AWS SDK
import json # Used for handling API-based data.
import base64 # Needed to decode the incoming POST data
def lambda_handler(event, context):
# Perform JSON data decoding
body_enc = event['body']
dec_dict = json.loads(base64.b64decode(body_enc))
# --- Write to dynamodb ---
# ** Create a variable that can take a random value between 1 and 1 000 000 000.
# This variable will be used as our key value i.e the ResponsesID and should be of type integer.
# It is important to note that the ResponseID i.e. the rid variable, should take
# on a unique value to prevent errors when writing to DynamoDB. **
# --- Insert your code here ---
rid = None # <--- Replace this value with your code.
# -----------------------------
# ** Instantiate the DynamoDB service with the help of the boto3 library **
# --- Insert your code here ---
dynamodb = None # <--- Replace this value with your code.
# -----------------------------
# Instantiate the table. Remember pass the name of the DynamoDB table created in step 4
table = dynamodb.Table('# Insert the name of your generated DynamoDB table here')
# ** Write the responses to the table using the put_item method. **
# Complete the below code so that the appropriate
# incoming data is sent to the matching column in your DynamoDB table
# --- Insert your code here ---
db_response = table.put_item(Item={'ResponsesID': None, # <--- Insert the correct variable
'Name': None, # <--- Insert the correct variable
'Email': None, # <--- Insert the correct variable
'Cell': None, # <--- Insert the correct variable
'Message': None # <--- Insert the correct variable
})
# -----------------------------
# ** Create a response object to inform the website
# that the workflow executed successfully. **
lambda_response = {
'statusCode': 200,
'body': json.dumps({
'Name': dec_dict['name'],
'Email': dec_dict['email'],
'Cell': dec_dict['phone'],
'Message': dec_dict['message'],
'DB_response': db_response
})
}
return lambda_response