aws / aws-sdk-js-v3

Modularized AWS SDK for JavaScript.
Apache License 2.0
3.06k stars 573 forks source link

DynamoDBDocument transactWrite() errors when the TransactItems object contains Put objects #6007

Closed jwtrim closed 5 months ago

jwtrim commented 5 months ago

Checkboxes for prior research

Describe the bug

I am testing AWS Lambdas locally which store data in a DynamoDB table. The table has the following schema definition:

{
    "TableName": "test-table-v1-local",
    "KeySchema": [
      { "AttributeName": "PK", "KeyType": "HASH" },
      { "AttributeName": "SK", "KeyType": "RANGE" }
    ],
    "AttributeDefinitions": [
      {
        "AttributeName": "PK",
        "AttributeType": "S"
      },
      {
        "AttributeName": "SK",
        "AttributeType": "S"
      }
    ],
    "ProvisionedThroughput": {
      "ReadCapacityUnits": 10,
      "WriteCapacityUnits": 10
    }
}

For the local environment, I am initializing a DynamoDBDocument client in the following way

const client = DynamoDBDocument.from(
    new DynamoDBClient({ endpoint: 'http://host.docker.internal:8000' })
);

When updating the table, I am trying to use the client.transactWrite() API with the following input:

{
  TransactItems:  [
    {
      Put: {
        TableName: 'test-table-v1-local',
        Item: {
          PK: { S: 'test' },
          SK: { S: 'test' }
        }
      }
    }
  ]

The API throws the following error:

ValidationException: Invalid attribute value type
    at throwDefaultError (/var/runtime/node_modules/@aws-sdk/node_modules/@smithy/smithy-client/dist-cjs/index.js:838:20)
    at /var/runtime/node_modules/@aws-sdk/node_modules/@smithy/smithy-client/dist-cjs/index.js:847:5
    at de_CommandError (/var/runtime/node_modules/@aws-sdk/client-dynamodb/dist-cjs/index.js:2150:14)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
  },
  __type: 'com.amazon.coral.validate#ValidationException'
}

I've had success using the client.send(new PutCommand()) and AWS.DynamoDB({ endpoint: 'http://host.docker.internal:8000' }).transactWriteItems() with the same item, but would need/prefer the other method to work.

SDK version number

@aws-sdk/client-dynamodb@3.556.0

Which JavaScript Runtime is this issue in?

Node.js

Details of the browser/Node.js/ReactNative version

v18.19.1

Reproduction Steps

Create a DynamoDB table with the provided schema and try to put a new document with the provided TransactItems list.

Observed Behavior

The API throws the following error:

ValidationException: Invalid attribute value type
    at throwDefaultError (/var/runtime/node_modules/@aws-sdk/node_modules/@smithy/smithy-client/dist-cjs/index.js:838:20)
    at /var/runtime/node_modules/@aws-sdk/node_modules/@smithy/smithy-client/dist-cjs/index.js:847:5
    at de_CommandError (/var/runtime/node_modules/@aws-sdk/client-dynamodb/dist-cjs/index.js:2150:14)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
  },
  __type: 'com.amazon.coral.validate#ValidationException'
}

Expected Behavior

Document created successfully.

Possible Solution

No response

Additional Information/Context

No response

RanVaknin commented 5 months ago

Hi @jwtrim ,

When you are using the Document client you are abstracting away the need to define each item with their corresponding Dynamo attribute types and instead you can supply the value directly as native JS types (strings in this case). Therefore your code should look something like:

 TransactItems:  [
    {
      Put: {
        TableName: 'test-table-v1-local',
        Item: {
-          PK: { S: 'test' },
-          SK: { S: 'test' }
+         PK: 'test',
+         SK: 'test',
        }
      }
    }
  ]

Feel free to revisit our lib-dynamo docs for further context about this.

Thanks, Ran~

jwtrim commented 5 months ago

@RanVaknin thanks for your response!

This does appear to fix my issue, but I swear I repeatedly tried that exact approach during my initial testing, yet somehow the exception persisted.

Anyhow, I can no longer reproduce so I will close this issue. Sorry for the inconvenience.

github-actions[bot] commented 4 months ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs and link to relevant comments in this thread.