60
52

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

AWS LambdaからPython Boto3を使用してDynamoDBを操作してみた

Last updated at Posted at 2019-10-11

DBとtableを自分の環境に書き換えてもらえれば、コピペで試せます。

バージョン
Python3.7

##準備


import boto3
dynamoDB = boto3.resource('dynamodb')
table= dynamoDB.Table('sample')

def main(event, context):

    search(event)
    insert(event)
    update(event)
    delete(event)

    return

##取得処理

def search(event):
    query_data = table.get_item(
        Key={
            'id': event['id']
            }
        )
    print("GetItem succeeded:")
    
    # 取り出す時は
    sample_value = query_data['Item']['sample_value']

	return

##登録処理


def insert(event, context):

    table.put_item(
        Item = {
            'id': event['id'],
            'sample_value': event['sample_value']
        }
    )
    print("PutItem succeeded:")
    
    return

##更新処理


def update(event):

    table.update_item(
        Key= {'id': event['id']},
        UpdateExpression='set sample_value = :s',
        ExpressionAttributeValues={
            ':s' : event['sample_value']
        }
    )
    print("UpdateItem succeeded:")

    return

##削除処理


def delete(event):
    
    table.delete_item(
        Key={
            'id': event['id']
        }
    )

    print("DeleteItem succeeded:")

    return

##参考
ステップ 3: 項目を作成、読み込み、更新、削除する
https://docs.aws.amazon.com/ja_jp/amazondynamodb/latest/developerguide/GettingStarted.Python.03.html

60
52
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
60
52

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?