Jeremy Fisher
Jeremy Fisher

Reputation: 2782

DynamoDB validation exception on lambda

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.

Upvotes: 7

Views: 14383

Answers (2)

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"
    }
  ]
}

Upvotes: 5

Kannaiyan
Kannaiyan

Reputation: 13055

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 */
  ]

Upvotes: 6

Related Questions