-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.py
84 lines (77 loc) · 2.24 KB
/
index.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
81
82
83
84
import json
import boto3
from boto3.dynamodb.conditions import Key
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table("backupSet-maptool")
# テーブルスキャン
def operation_scan():
try:
scanData = table.scan()
items=scanData['Items']
print(items)
return scanData
except Exception as e:
print("Error Exception.")
print(e)
# レコード検索
def operation_query(partitionKey):
try:
queryData = table.query(
KeyConditionExpression = Key("title").eq(partitionKey)
)
items=queryData['Items']
print(items)
return queryData
except Exception as e:
print("Error Exception.")
print(e)
# レコード追加・更新
def operation_put(items):
try:
putResponse = table.put_item(
Item={
'title': items[0]['title'],
'describe' : items[0]['describe'],
}
)
if putResponse['ResponseMetadata']['HTTPStatusCode'] != 200:
print(putResponse)
else:
print('PUT Successed.')
return putResponse
except Exception as e:
print("Error Exception.")
print(e)
# レコード削除
def operation_delete(partitionKey):
try:
delResponse = table.delete_item(
Key={
'title': partitionKey
}
)
if delResponse['ResponseMetadata']['HTTPStatusCode'] != 200:
print(delResponse)
else:
print('DEL Successed.')
return delResponse
except Exception as e:
print("Error Exception.")
print(e)
def lambda_handler(event, context):
print("Received event: " + json.dumps(event))
OperationType = event['OperationType']
try:
if OperationType == 'SCAN':
return operation_scan()
if OperationType == 'PUT':
Items = event['Keys']['items']
return operation_put(Items)
PartitionKey = event['Keys']['title']
if OperationType == 'QUERY':
return operation_query(PartitionKey)
if OperationType == 'DELETE':
return operation_delete(PartitionKey)
except Exception as e:
print("Error Exception.")
print(e)