-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
85 lines (71 loc) · 2.2 KB
/
main.py
File metadata and controls
85 lines (71 loc) · 2.2 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import os
import uuid
import boto3
import base64
import json
import datetime
from logging import error
from common import get_ssm_parameter
def create_customer(firstname, lastname, email):
dynamo_db = boto3.client('dynamodb')
db_name = get_ssm_parameter(os.environ.get('DB_NAME'))
print(f"DB_NAME: {db_name}")
create_item = {'id': {'S': str(uuid.uuid4())}}
if firstname:
create_item['firstname'] = {'S': firstname}
if lastname:
create_item['lastname'] = {'S': lastname}
if email:
create_item['email'] = {'S': email}
create_item['created_time'] = {'S': datetime.datetime.utcnow().isoformat()+'Z' }
print(f"Create_item {create_item}")
try:
dynamo_db.put_item(TableName=db_name, Item=create_item)
return True
except Exception as ex:
error(f"ERROR: {str(ex)}")
resp = {
"isBase64Encoded": False,
"statusCode": 500,
"body": "Server error: " + str(ex)
}
return resp
def lambda_handler(event, context):
print(f"Event: {event}")
body = event.get('body', '')
isBase64Encoded = event.get('isBase64Encoded', True)
print(f"body content: {body}")
if not body:
resp = {
"isBase64Encoded": False,
"statusCode": 400,
"body": "Bad Request: invalid body, should contain 'firstname'."
}
return resp
raw_body = base64.b64decode(body) if isBase64Encoded else body
try:
json_body = json.loads(raw_body)
except:
resp = {
"isBase64Encoded": False,
"statusCode": 400,
"body": "Bad Request: Body must be valid JSON."
}
return resp
firstname = json_body.get('firstname', '')
lastname = json_body.get('lastname', '')
email = json_body.get('email', '')
is_success = create_customer(firstname, lastname, email)
if is_success is True:
resp = {
"isBase64Encoded": False,
"statusCode": 201
}
return resp
else:
resp = {
"isBase64Encoded": False,
"statusCode": 500,
"body": "Internal server error."
}
return resp