Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nodejs Why am I getting an error "Task timed out after 6.01 seconds" when invoking my lambda function?

My code works fine when tested locally with serverless invoke local -f myFunction -d 3 Once I have deployed it to lambda with node.js 8.10 runtime and try to run it with launch.js I get an error "Task timed out after 6.01 seconds" On the aws console I have changed the timeout to 2 minutes which is plenty of time

This is the response I get:

{ StatusCode: 200,
  FunctionError: 'Unhandled',
  LogResult:
   'U1RBUlQgUmVxdWVzdElkOiBlY2I0OWVjMC05NTFjLTQ2NTAtOTI3ZS01NDdkMzIzM2QyODUgVmVyc2lvbjogJExBVEVTVApFTkQgUmVxdWVzdElkOiBlY2I0OWVjMC05NTFjLTQ2NTAtOTI3ZS01NDdkMzIzM2QyODUKUkVQT1JUIFJlcXVlc3RJZDogZWNiNDllYzAtOTUxYy00NjUwLTkyN2UtNTQ3ZDMyMzNkMjg1CUR1cmF0aW9uOiA2MDA2LjI0IG1zCUJpbGxlZCBEdXJhdGlvbjogNjAwMCBtcyAJTWVtb3J5IFNpemU6IDEwMjQgTUIJTWF4IE1lbW9yeSBVc2VkOiAxMDUgTUIJCjIwMTktMDUtMTZUMTQ6MTE6MTMuOTk0WiBlY2I0OWVjMC05NTFjLTQ2NTAtOTI3ZS01NDdkMzIzM2QyODUgVGFzayB0aW1lZCBvdXQgYWZ0IgNi4wMSBzZWNvbmRzCgo=',
  ExecutedVersion: '$LATEST',
  Payload:
   '{"errorMessage":"2019-05-16T14:11:13.994Z ecb49ec0-951c-4650-927e-547d3233d285 Task timed out after 6.01 seconds"}' }

launch.js This file defines a function to invoke my lambda function to scrape data for however many pages there are

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

var pages = [1]

function deployScraper(page) {
    const lambda = new AWS.Lambda({
      region: "eu-west-2"
    });

    const params = {
      FunctionName: "serverless-dev-myFunction",
      InvocationType: "RequestResponse",
      LogType: "Tail",
      Payload: JSON.stringify(page)
    };

    return lambda.invoke(params, function(error, data) {
      if (error) {
        console.error(JSON.stringify(error));
        return new Error(`Error scraping: ${JSON.stringify(error)}`);
      } else if (data) {
        console.log(data);
        return JSON.stringify(data);
      }
    });
  }

  function swarm(arr) {
    arr.forEach(page => {
      deployScraper(page);
    });
  }

  swarm(pages);

handler.js This is my lambda function it logs into a site and prints out an array of data from a page

"use strict";
const {login, getDataArray, saveToDB} = require("./utils"); 
const username = 'my_username';
const password = 'my_password';

module.exports.myFunction = (page, context, callback) => {
  // 1. login to site
  login(username, password)
  // 2. scrape data from page
  .then( ()  => getDataArray(page))
  // 3. upload data to DB
  .then(data => saveToDB(data))
.catch(error =>
  callback(new Error(`Error scraping ${page}: ${JSON.stringify(error)}`))
);
};
like image 754
imlearningcode Avatar asked Oct 17 '25 02:10

imlearningcode


1 Answers

Your Lambda is likely waiting for a connection to the Internet, which typically is set to expire in ~30 secs. With this being less than your function timeout of 5s, you are seeing the error about the task timing out. You can confirm this by increasing the amount of time you let the Lambda run and see if it eventually comes back with a networking error.

If you haven't enabled Internet access for your Lambda you need to do so for the VPC via a gateway.

https://aws.amazon.com/premiumsupport/knowledge-center/internet-access-lambda-function/

like image 199
doublesharp Avatar answered Oct 18 '25 16:10

doublesharp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!