javascript - AWS Lambda ending early (without any explicit return or callback) - Stack Overflow

I'm having a bit of an issue with some node.js code I'm putting into AWS Lambda. I've go

I'm having a bit of an issue with some node.js code I'm putting into AWS Lambda. I've got a couple of async calls that I need to make, and while the first is behaving like I expect, the lambda function is terminating before the second call is plete.

The return is null, which makes me think that lambda is hitting its implicit callback, but I don't think it should be doing that while there is a promise that hasn't been resolved yet.

Code:

exports.handle = async function(event, context) {
  var AWS = require("aws-sdk");

  AWS.config.update({
    region: "eu-west-1",
    endpoint: "dynamodb.eu-west-1.amazonaws"
  });

  var docClient = new AWS.DynamoDB.DocumentClient();
  console.log("Scanning Catalogue...");

  var params = {
    TableName : "Catalogue"
  };

  await docClient.scan(params).promise().then(function (data) {
    console.log("Scan succeeded.");
    data.Items.forEach(function (item) {
      //console.log(item.url);
      checkIfUpdateRequired(item);
    })
  })
}

async function checkIfUpdateRequired (catalogueItem) {
  var request = require("request-promise");
  console.log("Getting " + catalogueItem.url);

  await request(catalogueItem.url).then(function(response) {
    console.log(response.statusCode);
    console.log(response.headers['content-type']);
  });

}

Log output:

START RequestId: 634a55b7-6258-11e8-9f18-6b300c3b5de1 Version: $LATEST
2018-05-28T09:20:44.425Z    634a55b7-6258-11e8-9f18-6b300c3b5de1    Scanning Catalogue...
2018-05-28T09:20:45.446Z    634a55b7-6258-11e8-9f18-6b300c3b5de1    Scan succeeded.
2018-05-28T09:20:47.967Z    634a55b7-6258-11e8-9f18-6b300c3b5de1    Getting .cat
2018-05-28T09:20:48.028Z    634a55b7-6258-11e8-9f18-6b300c3b5de1    Getting .cat
END RequestId: 634a55b7-6258-11e8-9f18-6b300c3b5de1
REPORT RequestId: 634a55b7-6258-11e8-9f18-6b300c3b5de1  Duration: 7882.68 ms    Billed Duration: 7900 ms    Memory Size: 128 MB Max Memory Used: 49 MB

So the log tells me that checkIfUpdateRequired() is getting called, but the lambda ends (reporting success with result value null) before the promise is being fulfilled. I'm not doing any manual returns or callbacks to the handler that seem to be the norm for issues related to lambda's ending 'early'.

I'm at my wits end, can anyone offer any suggestions?

I'm having a bit of an issue with some node.js code I'm putting into AWS Lambda. I've got a couple of async calls that I need to make, and while the first is behaving like I expect, the lambda function is terminating before the second call is plete.

The return is null, which makes me think that lambda is hitting its implicit callback, but I don't think it should be doing that while there is a promise that hasn't been resolved yet.

Code:

exports.handle = async function(event, context) {
  var AWS = require("aws-sdk");

  AWS.config.update({
    region: "eu-west-1",
    endpoint: "dynamodb.eu-west-1.amazonaws."
  });

  var docClient = new AWS.DynamoDB.DocumentClient();
  console.log("Scanning Catalogue...");

  var params = {
    TableName : "Catalogue"
  };

  await docClient.scan(params).promise().then(function (data) {
    console.log("Scan succeeded.");
    data.Items.forEach(function (item) {
      //console.log(item.url);
      checkIfUpdateRequired(item);
    })
  })
}

async function checkIfUpdateRequired (catalogueItem) {
  var request = require("request-promise");
  console.log("Getting " + catalogueItem.url);

  await request(catalogueItem.url).then(function(response) {
    console.log(response.statusCode);
    console.log(response.headers['content-type']);
  });

}

Log output:

START RequestId: 634a55b7-6258-11e8-9f18-6b300c3b5de1 Version: $LATEST
2018-05-28T09:20:44.425Z    634a55b7-6258-11e8-9f18-6b300c3b5de1    Scanning Catalogue...
2018-05-28T09:20:45.446Z    634a55b7-6258-11e8-9f18-6b300c3b5de1    Scan succeeded.
2018-05-28T09:20:47.967Z    634a55b7-6258-11e8-9f18-6b300c3b5de1    Getting https://raw.githubusercontent./BSData/wh40k/master/Aeldari%20-%20Craftworlds.cat
2018-05-28T09:20:48.028Z    634a55b7-6258-11e8-9f18-6b300c3b5de1    Getting https://raw.githubusercontent./BSData/wh40k/master/Imperium%20-%20Adeptus%20Custodes.cat
END RequestId: 634a55b7-6258-11e8-9f18-6b300c3b5de1
REPORT RequestId: 634a55b7-6258-11e8-9f18-6b300c3b5de1  Duration: 7882.68 ms    Billed Duration: 7900 ms    Memory Size: 128 MB Max Memory Used: 49 MB

So the log tells me that checkIfUpdateRequired() is getting called, but the lambda ends (reporting success with result value null) before the promise is being fulfilled. I'm not doing any manual returns or callbacks to the handler that seem to be the norm for issues related to lambda's ending 'early'.

I'm at my wits end, can anyone offer any suggestions?

Share Improve this question edited May 28, 2018 at 11:24 CertainPerformance 372k55 gold badges352 silver badges357 bronze badges asked May 28, 2018 at 9:30 creature124creature124 831 silver badge4 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

You're not waiting for the checkIfUpdateRequired promises to plete; everything in docClient.scan is synchronous in your original code. Use Promise.all to wait for all promises to plete:

await docClient.scan(params).promise().then(function (data) {
  console.log("Scan succeeded.");
  return Promise.all(data.Items.map(checkIfUpdateRequired));
});

Note that if you're using await, your code will be flatter and easier to understand if you habitually use it instead of .then. For example, you could refactor to:

const data = await docClient.scan(params).promise();
return Promise.all(data.Items.map(checkIfUpdateRequired));

and

async function checkIfUpdateRequired (catalogueItem) {
  // actually, better to only require once, rather than on every function call
  const request = require("request-promise");
  console.log("Getting " + catalogueItem.url);
  const response = await request(catalogueItem.url)
  console.log(response.statusCode);
  console.log(response.headers['content-type']);
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745530404a4631667.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信