I'm using DynamoDB/DocumentCient on Lambda, and attempting a simple put operation, thusly:
var doc = require('dynamodb-doc');
var dynamo = new doc.DynamoDB();
...
var params = {
"TableName": "Recipients",
"Item": {
"recipient_id": 'XXX',
"user_id": 'XXX'
}
};
dynamo.putItem(params, function(err, res) {
...
and also:
var AWS = require('aws-sdk');
var docClient = new AWS.DynamoDB.DocumentClient({region: 'us-west-2'});
...
docClient.put(params, function(err, res) {
...
However both are returning the same error, as if they are expecting AttributeValue values for the Item:
{
"errorMessage": "There were 8 validation errors:\n* InvalidParameterType: Expected params.Item['recipient_id'] to be a structure\n* UnexpectedParameter: Unexpected key '0' found in params.Item['recipient_id']\n* UnexpectedParameter: Unexpected key '1' found in params.Item['recipient_id']\n* UnexpectedParameter: Unexpected key '2' found in params.Item['recipient_id']\n* InvalidParameterType: Expected params.Item['user_id'] to be a structure\n* UnexpectedParameter: Unexpected key '0' found in params.Item['user_id']\n* UnexpectedParameter: Unexpected key '1' found in params.Item['user_id']\n* UnexpectedParameter: Unexpected key '2' found in params.Item['user_id']",
"errorType": "MultipleValidationErrors",
I have tried both the Lambda-included aws-sdk, as well as bundling it myself, but both yield the same results.
Can someone please tell me what silly mistake I'm making?
Also, if I do try to use AttributeValues instead, I get the following for the DocumentClient:
{
"errorMessage": "shape.toType is not a function",
"errorType": "TypeError",
"stackTrace": [
"Translator.translateScalar (/var/runtime/node_modules/aws-sdk/lib/dynamodb/translator.js:76:16)",
"Translator.translate (/var/runtime/node_modules/aws-sdk/lib/dynamodb/translator.js:30:26)",
"Object.<anonymous> (/var/runtime/node_modules/aws-sdk/lib/dynamodb/translator.js:42:25)",
"Object.each (/var/runtime/node_modules/aws-sdk/lib/util.js:468:32)",
"Translator.translateStructure (/var/runtime/node_modules/aws-sdk/lib/dynamodb/translator.js:39:8)",
"Translator.translate (/var/runtime/node_modules/aws-sdk/lib/dynamodb/translator.js:27:35)",
"Translator.translateInput (/var/runtime/node_modules/aws-sdk/lib/dynamodb/translator.js:11:15)",
"Request.<anonymous> (/var/runtime/node_modules/aws-sdk/lib/dynamodb/document_client.js:471:31)",
"Request.callListeners (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:105:20)",
"Request.emit (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:77:10)",
"Request.emit (/var/runtime/node_modules/aws-sdk/lib/request.js:668:14)",
"Request.transition (/var/runtime/node_modules/aws-sdk/lib/request.js:22:10)",
"AcceptorStateMachine.runTo (/var/runtime/node_modules/aws-sdk/lib/state_machine.js:14:12)",
"Request.runTo (/var/runtime/node_modules/aws-sdk/lib/request.js:394:15)",
"Request.send (/var/runtime/node_modules/aws-sdk/lib/request.js:358:10)",
"DocumentClient.put (/var/runtime/node_modules/aws-sdk/lib/dynamodb/document_client.js:305:15)"
]
}
and this for dynamodb-doc:
{
"errorMessage": "Unrecognized Datatype to be formatted.",
"errorType": "Error",
"stackTrace": [
"DynamoDBDatatype.formatDataType (/var/runtime/node_modules/dynamodb-doc/lib/datatypes.js:115:19)",
"DynamoDBDatatype.formatRecursiveType (/var/runtime/node_modules/dynamodb-doc/lib/datatypes.js:61:31)",
"DynamoDBDatatype.formatDataType (/var/runtime/node_modules/dynamodb-doc/lib/datatypes.js:113:40)",
"formatAttrValInput (/var/runtime/node_modules/dynamodb-doc/lib/formatter.js:19:41)",
"Request.DynamoDBFormatter.formatInput (/var/runtime/node_modules/dynamodb-doc/lib/formatter.js:281:29)",
"Request.callListeners (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:105:20)",
"Request.emit (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:77:10)",
"Request.emit (/var/runtime/node_modules/aws-sdk/lib/request.js:668:14)",
"Request.transition (/var/runtime/node_modules/aws-sdk/lib/request.js:22:10)",
"AcceptorStateMachine.runTo (/var/runtime/node_modules/aws-sdk/lib/state_machine.js:14:12)",
"Request.runTo (/var/runtime/node_modules/aws-sdk/lib/request.js:394:15)",
"Request.send (/var/runtime/node_modules/aws-sdk/lib/request.js:358:10)",
"makeRequest (/var/runtime/node_modules/aws-sdk/lib/service.js:180:27)",
"svc.(anonymous function) [as putItem] (/var/runtime/node_modules/aws-sdk/lib/service.js:430:23)",
"/var/task/index.js:50:32",
"tryCatcher (/var/task/node_modules/lob/node_modules/bluebird/js/release/util.js:16:23)"
]
}
Anyone have any clues?
I have worked around this by using dynamodb-marshaler, however I would prefer to work directly using json without additional dependencies.
Facing the same issue myself :( Been struggling with coming up with any real solutions.
It looks you're using the dynamo-doc package, which was merged into the SDK in 2015. Have you tried using the SDK's bundled document client?
Closing due to age. Using the SDK's bundled document client rather than the dynamo-doc package should take care of the issue as reported.
I have confirmed that the same issue occurs with the bundled document client, but weirdly _only_ with certain Lambda functions.
E.g. the following fails with validation errors (expecting Attribute Values) in some functions, but works fine in others.
var AWS = require('aws-sdk');
var docClient = new AWS.DynamoDB.DocumentClient({region: 'us-west-2'});
...
docClient.put(params, function(err, res) {
...
Both functions are configured the same (same runtime, memory, etc). Is it possible that the bundled document client simply isn't the same version in older functions?
@karlwhite I see _some_ functions exhibiting this behaviour too, while others appear to be fine. I know it's a frustration to hear, but it's usually an issue with our code. For example, I've been struggling massively with this error, because it doesn't explain the _actual_ the problem. In my case, I was populating an Item property with a null. Please check that your params object has all properties populated. DynamoDB will not insert nulls. Can you post a full snippet of code that's not working?
@karlwhite did you ever solve this? I've had this just come up in a single lambda, using the document client. the db is passed in as is the library of functions to call it, so there are no syntax errors, i can see in my logging that the payload is correct and i still get the same error. 25 other functions fine.
@delprofundo No, unfortunately not (although I haven't revisited since). I used the "dynamodb-marshaler" npm as a workaround, and that worked for these particular functions (albeit by adding a huge package to the lambda function), and I've not really touched them since.
No clue why it only occurs with certain lambda functions and not others (despite identical setup).
I just figure it out how to reproduce this. Add this code to your lambda somewhere:
Object.prototype.isEmpty = function() {
for(var key in this) {
if(this.hasOwnProperty(key))
return false;
}
return true;
}
Check your code if you have specified a data type. For instance, compare your code with the below one and check if you have specified "{N:" for numbers and "{S:" for strings:
var params = {
TableName: 'TABLE',
Item: {
'CUSTOMER_ID' : {N: '001'},
'CUSTOMER_NAME' : {S: 'Richard Roe'},
}
};
// Call DynamoDB to add the item to the table
ddb.putItem(params, function(err, data) {
if (err) {
console.log("Error", err);
} else {
console.log("Success", data);
}
});
pilhokim: AWS鈥檚 recommended library for doing this work doesn鈥檛 require the explicit references in normal scenarios.
But to be comprehensive, my Code was working fine, then suddenly it stopped working. I started adding explicit references and one by one the errors related to the sort of code you have in your snippet resolved. Until eventually the error moved to a deeper level (I believe a constructor call but it鈥檚 been a while). So I reverted to the original code with implicit conversions.
I then remembered the prototype I鈥檇 added (see my last comment re isempty). I removed that and the code started working again.
Same issue here
@supportcoinsafer - might want to double check whether you've added any prototypes to Object. Or added another library that may have done something like that.
@var1ableX You saved my life! I had prototypes on Object and everything magically stopped working. Prototypes removed and everything works now!
After some investigation, discovered that it has nothing to do with aws-sdk client, but JS:
Object.prototype.doSomething = function() {
return 'test'
}
values = {}
console.log(`doSomething = ${values['doSomething']}`)
Outputs the function contents.
Coming back to the library, perhaps somewhere around this line https://github.com/aws/aws-sdk-js/blob/master/lib/dynamodb/translator.js#L46 one could check the typeof shape, and when this is a function, simply ignore?
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.
Most helpful comment
@var1ableX You saved my life! I had prototypes on Object and everything magically stopped working. Prototypes removed and everything works now!