JamesG JamesG - 3 months ago 14
JSON Question

Process exited before completing request (Lambda + DynamoDB)

For some reason I am getting the Process exited before completing request error.

Here is my code:

var http = require('http');
var aws = require('aws-sdk');
var ddb = new aws.DynamoDB();

function getUser(userid) {
var q = ddb.getItem({
TableName: "clients",
Key: {
ClientID: { S: userid } }
}, function(err, data) {
if (err) {
console.log(err);
return err;
}
else {
console.log(data);
}
});
//console.log(q);
}


exports.handler = function(event, context) {
getUser('user23');
console.log("called DynamoDB");

};


After googling a few people suggested changing the time out to a higher amount. Which I did to one minute.

However the function only took:


Duration : 2542.23 ms


I have also checked and double checked the table name and the key name etc...

The console log has this :


2016-03-21T04:09:46.390Z - Received event

2016-03-21T04:09:46.751Z - called DynamoDB

2016-03-21T04:09:47.012Z - {}

END RequestId: id123


Can anyone see why this is not working?

Edit



As per the answer below I tried:

console.log('Loading event');
var AWS = require('aws-sdk');
var dynamodb = new AWS.DynamoDB({apiVersion: '2012-08-10'});

exports.handler = function(event, context) {
console.log(JSON.stringify(event, null, ' '));
dynamodb.listTables(function(err, data) {
console.log(JSON.stringify(data, null, ' '));
});
var tableName = "clients";
var datetime = new Date().getTime().toString();
dynamodb.getItem({
TableName: tableName,
Key: {
ClientID: { S: "gr5f4sgnca25hki" } }

}, function(err, data) {
if (err) {
context.done('error','putting item into dynamodb failed: '+err);
}
else {
context.done(data);
}
});
};


but now my response is:


"errorMessage": "[object Object]"


What I am trying to do is this: Check if Item exists in database. Get the parameters from the entry if exists, then do something with the parameters


  • Can anyone help me?


Answer

First of all, context.done expects an Error object as the first argument, not a string containing the word "error". Second, if the Error object is null or undefined, then the termination will be taken as a succeed.

Now, consider your callback function:

function (err, data)
{
    if (err) {
       context.done('error', 'putting item into dynamodb failed: ' + err);
    }
    else {
       context.done(data);
    }
}

If you have an error, then your lambda will terminate in a failure, which is expected, but the errorMessage you'll get would simply be "error", which isn't much informative.

If you don't have an error, then your lambda will also terminate in a failure, because you are passing in data as the first argument to context.done, and remember that the first argument is always the Error object.

To fix this, you can simply do:

function (err, data)
{
    if (err) {
       context.done(err);
    } else {
       context.done(null, data);
    }
}

Or even better:

function (err, data)
{
    context.done(err, data);
}

If you don't want to handle the item and just return it immediately, you can use context.done as your callback function to the DynamoDB operation:

dynamodb.getItem({
    TableName: tableName,
    Key: {
        ClientID: { S: "gr5f4sgnca25hki" }
    }
}, context.done);
Comments