amazon-dynamodbdynamo-localnosql

Error creating table in local DynamoDB


I have downloaded local version for Amazon DynamoDB. I am trying to create a table using shell. When I run the code from shell it gives me an error:

"message":"The security token included in the request is invalid."
"code":"UnrecognizedClientException"
"time":"2017-04-27T12:50:35.880Z"
"statusCode":400
"retryable":false

Create code is:

var dynamodb = new AWS.DynamoDB();
var params = {
    "AttributeDefinitions": [
        {
            "AttributeName": "UserId",
            "AttributeType": "N"
        },
        {
            "AttributeName": "FirstName",
            "AttributeType": "S"
        },
        {
            "AttributeName": "LastName",
            "AttributeType": "S"
        },
        {
            "AttributeName": "CellPhoneNumber",
            "AttributeType": "N"
        }
    ],
    "TableName": "Users",
    "KeySchema": [
        {
            "AttributeName": "UserId",
            "KeyType": "HASH"
        },
        {
            "AttributeName": "CellPhoneNumber",
            "KeyType": "RANGE"
        }
    ],
    "LocalSecondaryIndexes": [
        {
            "IndexName": "UserIndex",
            "KeySchema": [
                {
                    "AttributeName": "UserId",
                    "KeyType": "HASH"
                },
                {
                    "AttributeName": "CellPhoneNumber",
                    "KeyType": "RANGE"
                }
            ],
            "Projection": {
                "ProjectionType": "KEYS_ONLY"
            }
        }
    ],
    "ProvisionedThroughput": {
        "ReadCapacityUnits": 5,
        "WriteCapacityUnits": 5
    }
}

dynamodb.createTable(params, function(err, data) {
    if (err) ppJson(err); // an error occurred
    else ppJson(data); // successful response

});

How do I create a table in local DynamoDB? Do I need to create a DB first? I am asking this because I have always worked on SQL and this is the first time I am using NoSQL


Solution

  • No need to create database. Just need to create table.

    Use the below configuration for local dynamodb. The endpoint URL is important. The other attributes are dummy values (i.e. it can be any values).

    var creds = new AWS.Credentials('akid', 'secret', 'session');
    
    AWS.config.update({
      region: "us-west-2",
      endpoint: "http://localhost:8000",
      credentials : creds
    });
    

    Also, no need to define all the attributes while creating the table. Only key attributes need to be defined. Otherwise, you will get error.

    Full code to create table (should be executed on http://localhost:8000/shell/):-

    var dynamodb = new AWS.DynamoDB({
        region: 'us-east-1',
        endpoint: "http://localhost:8000"
    });
    var tableName = "Movies";
    
    var params = {
        "AttributeDefinitions": [
            {
                "AttributeName": "UserId",
                "AttributeType": "N"
            },
            {
                "AttributeName": "CellPhoneNumber",
                "AttributeType": "N"
            }
        ],
        "TableName": "PBUsers",
        "KeySchema": [
            {
                "AttributeName": "UserId",
                "KeyType": "HASH"
            },
            {
                "AttributeName": "CellPhoneNumber",
                "KeyType": "RANGE"
            }
        ],
        "LocalSecondaryIndexes": [
            {
                "IndexName": "UserIndex",
                "KeySchema": [
                    {
                        "AttributeName": "UserId",
                        "KeyType": "HASH"
                    },
                    {
                        "AttributeName": "CellPhoneNumber",
                        "KeyType": "RANGE"
                    }
                ],
                "Projection": {
                    "ProjectionType": "KEYS_ONLY"
                }
            }
        ],
        "ProvisionedThroughput": {
            "ReadCapacityUnits": 5,
            "WriteCapacityUnits": 5
        }
    }
    
    dynamodb.createTable(params, function(err, data) {
        if (err) {
            if (err.code === "ResourceInUseException" && err.message === "Cannot create preexisting table") {
                console.log("message ====>" + err.message);
            } else {
                console.error("Unable to create table. Error JSON:", JSON.stringify(err, null, 2)); 
            }
    
        } else {
            console.log("Created table. Table description JSON:", JSON.stringify(data, null, 2));
        }
    });