Boto 3の勉強 その1 (DynamoDB)

Boto 3のチュートリアルにあるCloudWatchを進めていたが、今はあまり触らなくてもよさそうだったので、次へ進める。 ということで、今日からDynamoDBをやっていく。

boto3.amazonaws.com

今日は、DynamoDBのテーブルを作成し、その後テーブルをリストして確認する。

import boto3

dynamodb = boto3.resource("dynamodb")
client = boto3.client("dynamodb")

# DynamoDBのテーブルを作成
table = dynamodb.create_table(
    TableName="users",
    KeySchema=[
        {"AttributeName": "username", "KeyType": "HASH"},
        {"AttributeName": "last_name", "KeyType": "RANGE"},
    ],
    AttributeDefinitions=[
        {"AttributeName": "username", "AttributeType": "S"},
        {"AttributeName": "last_name", "AttributeType": "S"},
    ],
    ProvisionedThroughput={"ReadCapacityUnits": 5, "WriteCapacityUnits": 5},
)

# 作成後にテーブルの有無を確認
response = client.list_tables()
print(response["TableNames"])

実行結果

% python sample-dynamodb.py
['users']