docker – Puppetter on Lambda is running into an ECONNRESET socket hang up

I am running a lambda with puppeteer inside a docker container, all run locally.

Docker container file:

FROM public.ecr.aws/lambda/nodejs:14

RUN npm install chrome-aws-lambda@10.1.0 puppeteer-core@10.1.0 
COPY src/*  ${LAMBDA_TASK_ROOT}

CMD [ "app.handler" ]

The code of the lambda is pretty standard: returns the title of the page on success

async function launchBrowser() {
  try { 
    const chromium = require('chrome-aws-lambda');
    console.info('launching chrome-aws-lambda browser');
    const browser = await chromium.puppeteer.launch({
      args: chromium.args,
      defaultViewport: chromium.defaultViewport,
      executablePath: await chromium.executablePath,
      headless: chromium.headless,
      ignoreHTTPSErrors: true,
    });
    return browser;
  } catch (e) { 
   console.log(e);
  }
}

async function lambdaHandler(event, context) {
  let result = null;
  console.info(`EVENT ${JSON.stringify(event, null, 2)}`);
  const browser = await launchBrowser();
  console.info('browser launched');
  const page = await browser.newPage();
  try {
     await page.goto(event.url || 'https://google.com');
    result = await page.title();
  } finally {
    console.info('finally');
    await page.close();
    console.info('page closed');
    await browser.close();
    console.info('browser closed');
  }
  return result;
}

module.exports = { handler: lambdaHandler };

I am running the container using these 2 commands :

docker build --platform=linux/amd64 -t lambda-aws-based . 
docker run -p 9000:8080 lambda-aws-based

And for testing I am simply invoking the lambda using :

curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{"url": "https://google.com"}'

The thing is on the first call I always run into ECONNRESET socket hang up, only in the calls after does the lambda successfully run, which is an issue for my current use.

So if anyone has an idea how to stop this from happening?
Thank you.

Read more here: Source link