erezrokah / lighthouse-layer

A Lambda layer with all the required dependencies to run Google Lighthouse
MIT License
37 stars 4 forks source link

Lambda Size Limit Reached #74

Open usfkhoury opened 3 years ago

usfkhoury commented 3 years ago

I'm new to lambda and trying to deploy and run lighthouse on AWS Lambda but whenever I try to run yarn deploy the package I get the below error:

An error occurred: LambdaFunction - Unzipped size must be smaller than 262144000 bytes (Service: AWSLambdaInternal; Status Code: 400; Error Code: InvalidParameterValueException; Request ID: xxxx-xxxx-xxxx-xxxx)

This is due to the uncompressed chromium binary, so I then tried using the chrome-aws-lambda module instead of the chrome-launcher with the below lambda function and environment variables: index.js:

const chromium = require('chrome-aws-lambda');
const lighthouse = require('lighthouse');
const log = require('lighthouse-logger');

exports.handler = async (event, context, callback) => {
  let result = null;
  let browser = null;

  try {
    browser = await chromium.puppeteer.launch({
      args: chromium.args,
      defaultViewport: chromium.defaultViewport,
      executablePath: await chromium.executablePath,
      headless: chromium.headless,
      ignoreHTTPSErrors: true,
    });

    let page = await browser.newPage();

    await page.goto(event.url || 'https://google.com');

    result = await page.title();

    const options = {
      port: browser.port,
      logLevel: 'info',
    };
    log.setLevel(options.logLevel);
    const results = await lighthouse('https://google.com', options);

  } catch (error) {
    return callback(error);
  } finally {
    if (browser !== null) {
      await browser.close();
    }
  }

  return callback(null, result);
};

Environment Variables: image But I'm now getting this error:

{ "errorType": "Error", "errorMessage": "connect ECONNREFUSED 127.0.0.1:9222", "trace": [ "Error: connect ECONNREFUSED 127.0.0.1:9222", " at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16)" ] }

So my question is:

  1. How can I run the original chrome-launcher lambda without running into the lambda size limitation issue?
  2. How can I use the chrome-aws-lambda to get the lighthouse results?
usfkhoury commented 3 years ago

To Note that if I run the same code with chrome-aws-lambda but without const results = await lighthouse('https://google.com', options);, I get the expected result:

"Google"

So maybe I'm doing something wrong with the deployment of the layer or there's an issue with lighthouse using chrome-aws-lambda

azproweb commented 3 years ago

Having same problem here.

wafendy commented 3 years ago

Same error message.

darinlarimore commented 3 years ago

I also ran into this Lambda size limit issue and might have a possible solution for:

  1. How can I run the original chrome-launcher lambda without running into the lambda size limitation issue?

I was able to remove some additional files that were not necessary located in the node_modules/chrome-aws-lambda/bin directory. Since we are already copying them with the node decompress-binary.js script.

"name": "lighthouse-layer-nodejs",
  "private": true,
  "version": "0.0.1",
  "description": "lighthouse layer nodejs",
  "engines": {
    "node": ">= v12.18.0"
  },
  "scripts": {
    "postinstall": "node decompress-binary.js && rm -rf ./node_modules/chrome-aws-lambda/bin"
  },
  "author": "Erez Rokah",
  "license": "MIT",
  "homepage": "https://github.com/adieuadieu/erezrokah/lighthouse-runner/layers/lighthouse/nodejs",
  "dependencies": {
    "axios": "^0.19.2",
    "chrome-aws-lambda": "^7.0.0",
    "chrome-launcher": "^0.13.0",
    "google-spreadsheet": "^3.0.13",
    "lighthouse": "^7.0.0",
    "lighthouse-logger": "^1.2.0",
    "readline": "^1.3.0"
  }
}