Lambda function using Dynamodb and SNS - timed out

2019-08-05 09:42发布

问题:

Am trying to use a DynamoDb Item value in the SNS service to send the notification. Its failing with the timeout error however fetch from Dynamodb is successful and notification is also sent. But it keep retrying and timeout after processing multiple times.

{ "errorMessage": "2018-01-02T10:14:55.463Z c18d142e-efa5-11e7-8671-7f3af13c58c7 Task timed out after 10.01 seconds" }

Now, I also tried using the parameter context.callbackWaitsForEmptyEventLoop = false but dint help much!! Seems am not using it correctly.

Below is the piece of code, can someone please assist:

exports.handler = function (event, context, callback) {

var AWS = require('aws-sdk');
var doc = require('dynamodb-doc');
var dynamodb = new doc.DynamoDB();

var params = {    
TableName : 'abcmsg',
ProjectionExpression: 'Message',
  Key : { 
   "ind" : "ABC10"
    }
};

var MESSAGE_ID = getmsg();

function getmsg(MESSAGE_ID) {
dynamodb.getItem(params, function (err, data) {
        if (err) {
             console.log('ERROR: Dynamo Failed:', err);
             getmsg(err);
        } else {
               console.log('Dynamo Success: ' + JSON.stringify(data, null, '  ')); 
               let MESSAGE_ID = data['Item'].Message;
               console.log('data',data['Item'].Message); 
               getmsg(MESSAGE_ID);        
        }
});


console.log('MESSAGE_ID',MESSAGE_ID); //print the correct value 'ABCD'

var sns = new AWS.SNS({
      region:'eu-west-1',
      maxRetries: 0

});

sns.publish({
Message: MESSAGE_ID,
Subject:"Test Message",
TopicArn:"arn:aws:sns:eu-west-1:0123456789012:Lambda"
}, function (err, data) {
if (err) {
console.log(err.stack);
return;
}
console.log('push sent');
});

callback(null,{ "statusCode": 200 });
context.callbackWaitsForEmptyEventLoop = false;

}

};

Logs:-----

2018-01-02T10:14:55.005Z c18d142e-efa5-11e7-8671-7f3af13c58c7 Dynamo Success: { "Item": { "Message": "ABCD" } } 2018-01-02T10:14:55.005Z c18d142e-efa5-11e7-8671-7f3af13c58c7 data ABCD 2018-01-02T10:14:55.006Z c18d142e-efa5-11e7-8671-7f3af13c58c7 MESSAGE_ID ABCD 2018-01-02T10:14:55.028Z c18d142e-efa5-11e7-8671-7f3af13c58c7 push sent 2018-01-02T10:14:55.086Z c18d142e-efa5-11e7-8671-7f3af13c58c7 Dynamo Success: { "Item": { "Message": "ABCD" } } 2018-01-02T10:14:55.105Z c18d142e-efa5-11e7-8671-7f3af13c58c7 data ABCD 2018-01-02T10:14:55.106Z c18d142e-efa5-11e7-8671-7f3af13c58c7 MESSAGE_ID ABCD 2018-01-02T10:14:55.127Z c18d142e-efa5-11e7-8671-7f3af13c58c7 push sent 2018-01-02T10:14:55.167Z c18d142e-efa5-11e7-8671-7f3af13c58c7 Dynamo Success: { "Item": { "Message": "ABCD" } } 2018-01-02T10:14:55.185Z c18d142e-efa5-11e7-8671-7f3af13c58c7 data ABCD 2018-01-02T10:14:55.186Z c18d142e-efa5-11e7-8671-7f3af13c58c7 MESSAGE_ID ABCD 2018-01-02T10:14:55.226Z c18d142e-efa5-11e7-8671-7f3af13c58c7 push sent 2018-01-02T10:14:55.253Z c18d142e-efa5-11e7-8671-7f3af13c58c7 Dynamo Success: { "Item": { "Message": "ABCD" } } 2018-01-02T10:14:55.265Z c18d142e-efa5-11e7-8671-7f3af13c58c7 data ABCD 2018-01-02T10:14:55.285Z c18d142e-efa5-11e7-8671-7f3af13c58c7 MESSAGE_ID ABCD 2018-01-02T10:14:55.306Z c18d142e-efa5-11e7-8671-7f3af13c58c7 push sent 2018-01-02T10:14:55.345Z c18d142e-efa5-11e7-8671-7f3af13c58c7 Dynamo Success: { "Item": { "Message": "ABCD" } } 2018-01-02T10:14:55.365Z c18d142e-efa5-11e7-8671-7f3af13c58c7 data ABCD 2018-01-02T10:14:55.366Z c18d142e-efa5-11e7-8671-7f3af13c58c7 MESSAGE_ID ABCD 2018-01-02T10:14:55.386Z c18d142e-efa5-11e7-8671-7f3af13c58c7 push sent 2018-01-02T10:14:55.406Z c18d142e-efa5-11e7-8671-7f3af13c58c7 push sent 2018-01-02T10:14:55.445Z c18d142e-efa5-11e7-8671-7f3af13c58c7 Dynamo Success: { "Item": { "Message": "ABCD" } } 2018-01-02T10:14:55.445Z c18d142e-efa5-11e7-8671-7f3af13c58c7 data ABCD END RequestId: c18d142e-efa5-11e7-8671-7f3af13c58c7 REPORT RequestId: c18d142e-efa5-11e7-8671-7f3af13c58c7 Duration: 10010.15 ms Billed Duration: 10000 ms Memory Size: 128 MB Max Memory Used: 44 MB
2018-01-02T10:14:55.463Z c18d142e-efa5-11e7-8671-7f3af13c58c7 Task timed out after 10.01 seconds

回答1:

Looks like you are trying to get the item with key value "ind" : "ABC10" and notify using SNS.

In that case, the sns.publish should be called on successful of getItem i.e. instead of getmsg(MESSAGE_ID), the sns publish function should be called (i.e. you need to create one to publish the message) or sns publish code block should be executed (i.e. just copy and paste the sns publish code block inside the getItem else part).

I don't understand why you are calling the getmsg(err) (i.e. during error on getItem) or getmsg(MESSAGE_ID) (i.e. on success of getItem)? I think this is not correct. It is basically calling the getItem in a loop which causes time out error.