DynamoDB, Lambda function / custom module Timeout

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);
    ...
    }

Upvotes: 0

Views: 618

Answers (1)

Sébastien Stormacq
Sébastien Stormacq

Reputation: 14905

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);

Upvotes: 1

Related Questions