Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS DynamoDB returns validation error when called from AWS Lambda

I'm using AWS Lambda and try to write something to AWS DynamoDB. I use the following code:

var tableName = "locations";
var item = {
    deviceId: {
        S: event.deviceId
    },
    timestamps: {
        S: event.timestamp 
    }
}
var params = {
    TableName: tableName,
    Item: item
};

dynamo.putItem(params, function(err, data) {
    if (err) {
        context.fail(new Error('Error ' + err));
    } else {
        context.success(null);
    }
});

And I get the following error:

returns Error ValidationException: One or more parameter values were invalid: Type mismatch for key deviceId expected: S actual: M
like image 201
Martin Kretz Avatar asked Sep 08 '15 07:09

Martin Kretz


1 Answers

This happened because the aws sdk for Nodejs had changed!

If you are using:

var doc = require('dynamodb-doc');
var dynamo = new doc.DynamoDB();

Then the parameters to the putItem call (and most other calls) have changed and instead needs to be:

var tableName = "locations";
var item = {
    deviceId: event.deviceId,
    timestamp: event.timestamp,
    latitude: Number(event.latitude),
    longitude: Number(event.longitude)
}
var params = {
    TableName: tableName,
    Item: item
};

Read all about the new sdk here: https://github.com/awslabs/dynamodb-document-js-sdk

like image 178
Martin Kretz Avatar answered Sep 21 '22 04:09

Martin Kretz