7

I am getting the following error when calling my lambda skill

ClientError: An error occurred (ValidationException) 
when calling the CreateTable operation: 1 validation error detected: 
Value '[com.amazonaws.dynamodb.v20120810.KeySchemaElement@2273ace6, 
com.amazonaws.dynamodb.v20120810.KeySchemaElement@4d13ab9, 
com.amazonaws.dynamodb.v20120810.KeySchemaElement@115e22b2]' at 
'keySchema' failed to satisfy constraint: Member must have length less than or equal to 2

Here is the code:

def write_values_to_db(ddid, token, intent):
    pid = ...
    dynamodb_client = boto3.client('dynamodb')
    try:
        response = dynamodb_client.create_table(
            AttributeDefinitions=[
                {
                    'AttributeName': 'pid',
                    'AttributeType': 'S',
                },
                {
                    'AttributeName': 'ddid',
                    'AttributeType': 'S',
                },
                {
                    'AttributeName': 'token',
                    'AttributeType': 'S',
                },
            ],
            KeySchema=[
                {
                    'AttributeName': 'pid',
                    'KeyType': 'HASH',
                },
                {
                    'AttributeName': 'ddid',
                    'KeyType': 'RANGE',
                },
                {
                    'AttributeName': 'token',
                    'KeyType': 'RANGE',
                },
            ],
            ProvisionedThroughput={
                'ReadCapacityUnits': 5,
                'WriteCapacityUnits': 5,
            },
            TableName='Values',
        )
    except dynamodb_client.exceptions.ResourceInUseException:
        dynamodb_client.put_item(
            TableName='Values',
            Item={
                'pid': pid,
                'ddid': ddid,
                'token': token
            }
        )

According to my dashboard the error is on the TableName='Values' line. I was following a tutorial and only changed certain things so I don't see why this is not working. I can't test on a local environment because I have region/credential issues.

1
  • DynamoDB requires one hash key and not more than one range key in the primary key, does it not? Commented Sep 9, 2017 at 0:32

2 Answers 2

6

The KeySchema in your code should be as below,

AttributeDefinitions=[
            {
                'AttributeName': 'pid',
                'AttributeType': 'S',
            }
        ],
KeySchema=[
                {
                    'AttributeName': 'pid',
                    'KeyType': 'HASH'
                }
]

You can have only one Hash Key and One Range Key Max.

If you want to additional indexes, you can create them with secondary indexes.

http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/LSI.html

Below would be the syntax for Global Secondary Index.

Reference: http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html

GlobalSecondaryIndexes: [
    {
      IndexName: 'STRING_VALUE', /* required */
      KeySchema: [ /* required */
        {
          AttributeName: 'STRING_VALUE', /* required */
          KeyType: HASH | RANGE /* required */
        },
        /* more items */
      ],
      Projection: { /* required */
        NonKeyAttributes: [
          'STRING_VALUE',
          /* more items */
        ],
        ProjectionType: ALL | KEYS_ONLY | INCLUDE
      },
      ProvisionedThroughput: { /* required */
        ReadCapacityUnits: 0, /* required */
        WriteCapacityUnits: 0 /* required */
      }
    },
    /* more items */
  ]
Sign up to request clarification or add additional context in comments.

4 Comments

I'm not sure if I want additional indices, I just want additional attributes. I want pid to be the primary key and ddid and consent_token to be attributes
Let me modify the answer. You can have only primary key without a range key.
Yes I tried that but that causes a mismatch between the number of attribtues in KeySchema and the number in AttributeSchema. I was able to resolve my problem by creating the table in console.aws.amazon.com/dynamodb and using the put_item method
You don't need to define attribute definitions other than the primary key. Rest of them dynamo will accept as it is. No schema definition needed. You may not be able to add a property with null, empty in their values. Other than that it will take any type.
5

AWS explicate that in the documentation:

For a composite primary key (partition key and sort key), you must provide exactly two elements, in this order: The first element must have a KeyType of HASH, and the second element must have a KeyType of RANGE.

They only allow two KeySchema: one KeyType as HASH and another KeyType as RANGE.

{
  "KeySchema": [
    {
      "AttributeName": "ForumName",
      "KeyType": "HASH"
    },
    {
      "AttributeName": "Subject",
      "KeyType": "RANGE"
    }
  ]
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.