こちらのプログラムを Lambda で使えるように書き換えました。
Python3: DynamoDB を使う
city という名前のテーブルを作成
dynamo_create_table.py
#! /usr/bin/python
# -*- coding: utf-8 -*-
#
# dynamo_create_table.py
#
# Sep/20/2021
# --------------------------------------------------------------------
import sys
import json
import boto3
#
# --------------------------------------------------------------------
def create_table_proc(name_table):
dynamodb = boto3.resource('dynamodb')
table = dynamodb.create_table(
TableName=name_table,
KeySchema=[
{
'AttributeName': 'key',
'KeyType': 'HASH'
},
],
AttributeDefinitions=[
{
'AttributeName': 'key',
'AttributeType': 'S'
},
],
ProvisionedThroughput={
'ReadCapacityUnits': 5,
'WriteCapacityUnits': 5
}
)
print("Table status:", table.table_status)
#
# --------------------------------------------------------------------
def lambda_handler(event, context):
#
name_table = 'city'
create_table_proc(name_table)
#
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}
# --------------------------------------------------------------------
データの挿入
dynamo_insert.py
#! /usr/bin/python
# -*- coding: utf-8 -*-
#
# dynamo_insert.py
#
# Sep/20/2021
# ---------------------------------------------------------------
import sys
import json
import boto3
from decimal import *
# ---------------------------------------------------------------
def insert_proc (table,key,name,population,date_mod):
response = table.put_item ( \
Item={'key': key,'name': name,'population': population,'date_mod':date_mod}
)
# ---------------------------------------------------------------
def insert_main_proc(name_table):
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table(name_table)
insert_proc (table,"t0921","宇都宮",Decimal ("18249"),"2004-2-12")
insert_proc (table,"t0922","小山",Decimal ("79814"),"2003-5-24")
insert_proc (table,"t0923","佐野",Decimal ("69826"),"2003-7-17")
insert_proc (table,"t0924","足利",Decimal ("59123"),"2003-6-28")
insert_proc (table,"t0925","日光",Decimal ("49521"),"2003-8-29")
insert_proc (table,"t0926","下野",Decimal ("39427"),"2003-10-14")
#
# ---------------------------------------------------------------
def lambda_handler(event, context):
name_table = 'city'
insert_main_proc(name_table)
#
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}
# ---------------------------------------------------------------
データのスキャン
dynamo_scan.py
#! /usr/bin/python
# -*- coding: utf-8 -*-
#
# dynamo_scan.py
#
# Sep/20/2021
# --------------------------------------------------------------------
import sys
import json
import boto3
import decimal
from boto3.dynamodb.conditions import Key, Attr
# --------------------------------------------------------------------
def scan_proc(name_table):
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table(name_table)
response = table.scan()
for it in response['Items']:
str_out = it['key'] + '\t'
str_out += it['name'] + '\t'
str_out += str (it['population']) + '\t'
str_out += it['date_mod']
print(str_out)
# --------------------------------------------------------------------
def lambda_handler(event, context):
name_table = 'city'
scan_proc(name_table)
#
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}
#
# --------------------------------------------------------------------
キーを指定してデータの取得
dynamo_get.py
#! /usr/bin/python
# -*- coding: utf-8 -*-
#
# dynamo_get.py
#
# Sep/20/2021
# --------------------------------------------------------------------
import sys
import json
import boto3
from boto3.dynamodb.conditions import Key, Attr
# --------------------------------------------------------------------
def get_proc(name_table,key_search):
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table(name_table)
response = table.query(
KeyConditionExpression=Key('key').eq(key_search)
)
for it in response['Items']:
str_out = it['key'] + '\t'
str_out += it['name'] + '\t'
str_out += str (it['population']) + '\t'
str_out += it['date_mod']
print(str_out)
#
# --------------------------------------------------------------------
def lambda_handler(event, context):
name_table = 'city'
key_search='t0922'
get_proc(name_table,key_search)
#
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}
# --------------------------------------------------------------------
キーを指定してデータの削除
dynamo_delete.py
#! /usr/bin/python
# -*- coding: utf-8 -*-
#
# dynamo_delete.py
#
# Sep/21/2021
# --------------------------------------------------------------------
import sys
import json
import boto3
from boto3.dynamodb.conditions import Key
# --------------------------------------------------------------------
def delete_proc(name_table,key_delete):
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table(name_table)
response = table.delete_item(
Key={
'key': key_delete,
}
)
# --------------------------------------------------------------------
def lambda_handler(event, context):
name_table = 'city'
key_delete = 't0923'
delete_proc(name_table,key_delete)
#
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}
# --------------------------------------------------------------------
テーブルの削除
dynamo_delete_table.py
#! /usr/bin/python
# -*- coding: utf-8 -*-
#
# dynamo_delete_table.py
#
# Sep/20/2021
# --------------------------------------------------------------------
import sys
import json
import boto3
# --------------------------------------------------------------------
def delete_table_proc(name_table):
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table(name_table)
table.delete()
#
# --------------------------------------------------------------------
def lambda_handler(event, context):
name_table = 'city'
delete_table_proc(name_table)
#
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}
# --------------------------------------------------------------------