ablog

不器用で落着きのない技術者のメモ

AWS CLI で Amazon DynamoDB にテーブルを作成する

AWS CLI で DynamoDB のテーブルを作成する

{
    "TableName": "testTable",
    "AttributeDefinitions": [
        {
            "AttributeName": "col1",
            "AttributeType": "S"
        },
        {
            "AttributeName": "col2",
            "AttributeType": "S"
        }
    ],
    "KeySchema": [
        {
            "KeyType": "HASH",
            "AttributeName": "col1"
        },
        {
            "KeyType": "RANGE",
            "AttributeName": "col2"
        }
    ],
    "ProvisionedThroughput": {
        "WriteCapacityUnits": 5,
        "ReadCapacityUnits": 5
    }
}
  • テーブルを作成する
$ aws dynamodb create-table --cli-input-json file://ddb_test_table.json
{
    "TableDescription": {
        "TableArn": "arn:aws:dynamodb:ap-northeast-1:0123456789012:table/testTable",
        "AttributeDefinitions": [
            {
                "AttributeName": "col1",
                "AttributeType": "S"
            },
            {
                "AttributeName": "col2",
                "AttributeType": "S"
            }
        ],
        "ProvisionedThroughput": {
            "NumberOfDecreasesToday": 0,
            "WriteCapacityUnits": 5,
            "ReadCapacityUnits": 5
        },
        "TableSizeBytes": 0,
        "TableName": "testTable",
        "TableStatus": "CREATING",
        "TableId": "40c2e0fc-72c9-4dd1-bc76-1e47275739cd",
        "KeySchema": [
            {
                "KeyType": "HASH",
                "AttributeName": "col1"
            },
            {
                "KeyType": "RANGE",
                "AttributeName": "col2"
            }
        ],
        "ItemCount": 0,
        "CreationDateTime": 1608254891.316
    }
}

補足

  • DynamoDB はスキーマレスなのでテーブル作成時は KeySchema と同じ AttributeDefinitions しか指定できない("Number of attributes in KeySchema does not exactly match number of attributes defined in AttributeDefinitions" と怒られる)。
  • それ以外の Attribute は PutItem のときに指定する。
$  aws dynamodb create-table --cli-input-json file://ddb_test_table.json

An error occurred (ValidationException) when calling the CreateTable operation: One or more parameter values were invalid: Number of attributes in KeySchema does not exactly match number of attributes defined in AttributeDefinitions
{
    "TableName": "testTableError",
    "AttributeDefinitions": [
        {
            "AttributeName": "col1",
            "AttributeType": "S"
        },
        {
            "AttributeName": "col2",
            "AttributeType": "S"
        },
        {
            "AttributeName": "col3",
            "AttributeType": "S"
        }
    ],
    "KeySchema": [
        {
            "KeyType": "HASH",
            "AttributeName": "col1"
        },
        {
            "KeyType": "RANGE",
            "AttributeName": "col2"
        }
    ],
    "ProvisionedThroughput": {
        "WriteCapacityUnits": 5,
        "ReadCapacityUnits": 5
    }
}