Reputation: 3767
I am trying to get my first Lambda function to work, but I can't ever get a test to write to my dynamoDB. I have been following the AWS tutorials up to this point.
Here is my Lambda function (slight modification from the tutorial to fit my DB):
'use strict';
console.log('Loading function');
const doc = require('dynamodb-doc');
const dynamo = new doc.DynamoDB();
/**
* Provide an event that contains the following keys:
*
* - operation: one of the operations in the switch statement below
* - tableName: required for operations that interact with DynamoDB
* - payload: a parameter to pass to the operation being performed
*/
exports.handler = (event, context, callback) => {
console.log('Received event:', JSON.stringify(event, null, 2));
console.log('\nPayload:', event.Item);
console.log('event table', event.TableName);
console.log('\nEvent:', event);
const operation = event.operation;
const payload = event.Item;
// if (event.tableName) {
// payload.TableName = event.tableName;
// }
switch (operation) {
case 'create':
dynamo.putItem(payload, callback);
break;
case 'read':
dynamo.getItem(payload, callback);
break;
case 'update':
dynamo.updateItem(payload, callback);
break;
case 'delete':
dynamo.deleteItem(payload, callback);
break;
case 'list':
dynamo.scan(payload, callback);
break;
case 'echo':
callback(null, payload);
break;
case 'ping':
callback(null, 'pong');
break;
default:
callback(new Error(`Unrecognized operation "${operation}"`));
}
};
Here is my test payload:
{
"TableName": "EVENTS",
"operation": "create",
"Item" : {
"userID": "2",
"kidNo": 2,
"note": "Testing",
"timeStamp": "timer1"
}
}
And here is one of the responses I get:
{
"errorMessage": "There were 6 validation errors:\n* MissingRequiredParameter: Missing required key 'TableName' in params\n* MissingRequiredParameter: Missing required key 'Item' in params\n* UnexpectedParameter: Unexpected key 'userID' found in params\n* UnexpectedParameter: Unexpected key 'kidNo' found in params\n* UnexpectedParameter: Unexpected key 'timeStamp' found in params\n* UnexpectedParameter: Unexpected key 'note' found in params",
"errorType": "MultipleValidationErrors",
"stackTrace": [
"* MissingRequiredParameter: Missing required key 'TableName' in params",
"* MissingRequiredParameter: Missing required key 'Item' in params",
"* UnexpectedParameter: Unexpected key 'userID' found in params",
"* UnexpectedParameter: Unexpected key 'kidNo' found in params",
"* UnexpectedParameter: Unexpected key 'timeStamp' found in params",
"* UnexpectedParameter: Unexpected key 'note' found in params",
"ParamValidator.validate (/var/runtime/node_modules/aws-sdk/lib/param_validator.js:40:28)",
"Request.VALIDATE_PARAMETERS (/var/runtime/node_modules/aws-sdk/lib/event_listeners.js:125:42)",
"Request.callListeners (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:105:20)",
"callNextListener (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:95:12)",
"/var/runtime/node_modules/aws-sdk/lib/event_listeners.js:85:9",
"finish (/var/runtime/node_modules/aws-sdk/lib/config.js:315:7)",
"/var/runtime/node_modules/aws-sdk/lib/config.js:333:9",
"EnvironmentCredentials.get (/var/runtime/node_modules/aws-sdk/lib/credentials.js:126:7)",
"getAsyncCredentials (/var/runtime/node_modules/aws-sdk/lib/config.js:327:24)",
"Config.getCredentials (/var/runtime/node_modules/aws-sdk/lib/config.js:347:9)"
]
}
I understand what it is telling me (I think), but I also don't because I can't fix it! I have tried all the suggestions in the error, and they don't go away. I feel like I am missing something basic.
Upvotes: 2
Views: 2014
Reputation: 1058
It seems like the payload that was passed to dynamodb was incorrect since the payload
was stripped to only objects in the Item (from your test event).
const payload = event.Item;
// the value of payload from above line:
// payload = {
// "userID": "2",
// "kidNo": 2,
// "note": "Testing",
// "timeStamp": "timer1"
// }
A simple hack that you can try
const payload = {
TableName: event.TableName,
Item: event.Item
}
Upvotes: 2