forked from htnani/Assignments-2019
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambda.py
executable file
·74 lines (60 loc) · 1.96 KB
/
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
72
73
74
import boto3
import json
print('Loading function')
dynamo = boto3.client('dynamodb')
def respond(err, res=None):
return {
'statusCode': '400' if err else '200',
'body': json.dumps(str(err) if err else res),
'headers': {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
},
}
def lambda_handler(event, context):
operation = event['httpMethod']
if operation == 'GET':
return respond(None, dynamo.scan(**event['queryStringParameters']))
elif operation == 'POST':
return respond(None, dynamo.put_item(**json.loads(event['body'])))
elif operation == 'DELETE':
return respond(None, dynamo.delete_item(**json.loads(event['body'])))
elif operation == 'PUT':
return respond(None, dynamo.update_item(**json.loads(event['body'])))
else:
return respond(ValueError('Unsupported method %s'%operation))
###### DO NOT COPY TO AWS LAMBDA CONSOLE FROM HERE
print('--------------------GET event test')
get_event = {
'httpMethod': 'GET',
'queryStringParameters': {
'TableName': 'shopping-list'
}
}
print('--------------------REQUEST')
print(json.dumps(get_event, indent=2))
result = lambda_handler(get_event, None)
print('--------------------RESULT')
print(json.dumps(result, indent=2))
print('--------------------RESULT body')
print(json.dumps(json.loads(result['body']), indent=2))
print('--------------------POST event test')
myvar = {
'TableName': 'shopping-list',
'Item': {
'ThingId': {
'S': 'Red apples'
}
}
}
post_event = {
'httpMethod': 'POST',
'body': json.dumps(myvar, separators=(',', ':'))
}
print('--------------------REQUEST')
print(json.dumps(post_event, indent=2))
result = lambda_handler(post_event, None)
print('--------------------RESULT')
print(json.dumps(result, indent=2))
print('--------------------RESULT body')
print(json.dumps(json.loads(result['body']), indent=2))