-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrud.py
80 lines (68 loc) · 1.61 KB
/
crud.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
76
77
78
79
80
import json
import boto3
'''
post
{
"httpMethod": "POST",
"body": {
"name": "John",
"age":30,
"role":"Developer"
}
}
get
{
"httpMethod": "GET",
"body": {
"name": "John"
}
}
'''
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('person')
def lambda_handler(event, context):
http_method = event['httpMethod']
if http_method == 'POST':
body = event['body']
name = body['name']
age = body['age']
role = body['role']
response = table.put_item(
Item = {
'name': name,
'age': age,
'role': role
}
)
print("response", response)
return {
'statusCode': 200,
'body': json.dumps(f'name is {name}, age is {age}, role is {role}')
}
elif http_method == 'GET':
body = event['body']
name = body['name']
response = table.get_item(
Key = {
'name': name
}
)
print("Response ", response)
item = response['Item']
print("item ", item)
return {
'statusCode': 200,
'body': json.dumps(f"Person name is {item['name']} age is {item['age']} Role is {item['role']}")
}
elif http_method == 'DELETE':
body = event['body']
name = body['name']
response = table.delete_item(
Key={
'name': name
}
)
return {
'statusCode': 200,
'body': json.dumps('Item deleted')
}