0

I have the following two JS file. My problem is when i call the Calls.js which calls the Archive.js for archiving logs into DynamoDB the request times out. I have tried out, many things, read about many things, tried in local/AWS environment without luck. What am i missing?

Link1, Link2, Link3, Link4, Link5,

Archive.js

module.exports.archive = archive;
...
function archive(input, callback){
  AWS.config.update({
    region: "eu-west-1",
    endpoint: "http://localhost:8000"
  });
  var documentClient = new AWS.DynamoDB.DocumentClient({
    httpOptions: {
      agent: new https.Agent({
        rejectUnauthorized: true,
        secureProtocol: "TLSv1_method",
        ciphers: "ALL"
      })
    }
  });
...
  var paramsPUT = {
    TableName: "Logging",
    Item: {
      HashKey: dbID,
      archiveEntry: archiveEntry
    }
  };
...
documentClient.put(paramsPUT, function(err, data) {

    if (err) console.log(err);
    if (data) console.log(data);
...
callback(data);
  });


}

Calls.js

exports.handler(event, context, callback)   => {
    const archive = require("./..path..").archive;
    ...
    context.callbackWaitsForEmptyEventLoop = false;
    ...
        archive(input, callback);
    ...
    }

1 Answer 1

1

I can not reproduce a timeout condition with your code. Your code is talking to an AWS endpoint at http://localhost:8000, so I assume you have DynamoDB local up and running, don't you ? Failling to have local DynamoDB running would cause the timeout.

That being said, I would strongly suggest to refactor your code to use Promise and the new async/await provided by NodeJS 8 instead of passing the Lambda callback around.

Here is the modified code.

const AWS = require("aws-sdk");

async function archive(input) {

    return new Promise( (resolve, reject) => {
        AWS.config.update({
            region: "eu-west-1",
            endpoint: 'http://localhost:8000'        
        });

        //use client specific AWS configuration instead of the global one
        const documentClient = new AWS.DynamoDB.DocumentClient();

        var paramsPUT = {
            TableName: "Logging",
            Item: {
                HashKey: "123",
                archiveEntry: input
            }
        };

        documentClient.put(paramsPUT, function (err, data) {

            if (err) {
                console.log("ERROR " + err);
                reject(err);
            }

            console.log("Returned from DDB " + JSON.stringify(data, null,2));
            resolve(data);
        });

    });
}

exports.handler = async (event, context, callback) => {
    const result = await archive("abc");
    callback(result);
}

// stuffs to test locally 

callback = function (data) {
    console.log("callback called with " + JSON.stringify(data,null,2));
}

event = context = {}

exports.handler(event, context, callback);
Sign up to request clarification or add additional context in comments.

1 Comment

Yes i do have Local DynamoDB up. Thanks for the nice solution, The issue was on my local Environment: For some reason local Lambda functions don't reach Local DynamoDB. Probably because of the Containers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.