Skip to content

Failing to put item into DDB from Lambda with NodeJS

0

Hi,

Recently, my Lambda (NodeJS 22.x running in us-west-2) is failing to add items to DDB. It is failing with this error: "One or more parameter values were invalid: Type mismatch for key pk expected: S actual: M"

In the log, my request looks like this: { "TableName": "ranking", "Item": { "pk": "20250630_overall-rank", "sk": "p1967", "expirationSec": ... "data": ... } }

I am using DynamoDBDocumentClient to insert the item.

When running locally, the code works fine. I have been running the same codes for a while (several years), and they were working fine, but they suddenly started failing yesterday. It is also not consistent. When I tried to insert a few items, then it may pass. However, when I try to insert ~2000 items at about 10 concurrent requests, then it may randomly started failing with the above error for certain items.

As you can see, the pk is already of type string. If the pk is malformatted, it should have failed consistently for all items, but now it is failing randomly for some items.

I suspect there is a bug on AWS side. Can someone help?

asked a year ago198 views
2 Answers
1

The error you're encountering - "Type mismatch for key pk expected: S actual: M" - indicates that DynamoDB is receiving your partition key as a map (M) type instead of a string (S) type, despite your code showing it as a string.

This issue is likely related to how you're using the DynamoDBDocumentClient. When using this client, there are two different approaches to formatting your data:

  1. Using native JavaScript types (recommended with DocumentClient)
  2. Using DynamoDB's attribute value format (used with the low-level client)

It appears your code might be mixing these approaches, which can cause inconsistent behavior. The DocumentClient is designed to accept native JSON data, but your error suggests the client might be receiving or interpreting the data in the wrong format.

For the DocumentClient, your request should look like:

{
TableName: "ranking",
Item: {
pk: "20250630_overall-rank",  // Plain string, not wrapped in {S: "..."}
sk: "p1967",
expirationSec: ...,
data: ...
}
}

The intermittent nature of the failures (especially under load) suggests there might be an issue with how your data is being prepared or serialized before being sent to DynamoDB, possibly in a concurrent context.

To resolve this:

  1. Ensure you're consistently using the DocumentClient's expected format (native JS types)
  2. Check if any part of your code is manually formatting the data as DynamoDB attribute values
  3. Verify that your concurrent processing isn't somehow altering the data structure
  4. Consider adding explicit type checking before sending requests

If you're using any middleware or serialization libraries, they might be affecting how your data is formatted before reaching DynamoDB.
Sources
AWS DynamoDB update item command gets ValidationException: The provided key element does not match the schema | AWS re:Post

answered a year ago
EXPERT
reviewed a year ago
  • Bundling the aws-sdk seems to have fixed the issue. Thank you for the suggestions.

0
Accepted Answer

You mentioned that your function was running for years with no issues. I wonder if the issue might be related to the version of aws-sdk. Are you using the provided package as part of the execution environment or you are including yours?

Relying on the AWS SDK provided by Lambda's execution environment adds risk because AWS can update the SDK version without notice, potentially breaking your application with new versions that introduce breaking changes, modified APIs, or different error handling patterns. The best practice is to include the specific AWS SDK version as a dependency in your deployment package, giving you full control over when to upgrade and ensuring your code behaves predictably across all deployments. While this increases your bundle size, the trade-off is worth it to avoid unexpected runtime failures and maintain application stability.

AWS
answered a year ago
EXPERT
reviewed a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.