Dockerfile
FROM public.ecr.aws/lambda/nodejs:12
COPY index.js package.json /var/task/
RUN npm install --save-prod
CMD [ "index.handler" ]
package.json
{
"name": "lighthouse2",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"chrome-aws-lambda": "^7.0.0",
"puppeteer-core": "^7.0.4",
"lighthouse": "^7.1.0"
}
}
index.js
const chromium = require('chrome-aws-lambda');
const lighthouse = require('lighthouse');
exports.handler = async(event, context, callback) => {
let result = null;
let chrome = null;
try {
console.log('Launching puppeteer chromium');
let chromiumOptions = {
args: chromium.args,
defaultViewport: chromium.defaultViewport,
executablePath: await chromium.executablePath,
headless: chromium.headless,
ignoreHTTPSErrors: true
};
const command = await chromium.puppeteer.launch(chromiumOptions)
.then((chrome) => {
console.log('launch.then called');
return {
chrome,
async start() {
console.log('function start called');
const options = {logLevel: 'info', onlyCategories: ['performance']};
const runnerResult = await lighthouse('https://www.example.com', options)
console.log('Report is done for', runnerResult.lhr.finalUrl);
console.log('Performance score was', runnerResult.lhr.categories.performance.score * 100);
await chrome.kill();
}
}
})
.catch(err => console.error(err));
console.log('Launched.');
return await command.start();
} catch (error) {
console.log(error);
return callback(error);
} finally {
if (chrome !== null) {
await chrome.close();
}
}
return callback(null, result);
};
Build a test Docker image:
docker build -t lighthouse2 .; docker run --rm -p 9000:8080 lighthouse2
Test the handler (in another terminal)
curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{"payload":"hello world!"}'
Expected result:
Lighthouse runs and I can read the report properties.
Actual result:
When lighthouse actually runs, I get this, no matter what settings I seem to try:
Wed, 17 Feb 2021 23:24:18 GMT status Connecting to browser
Wed, 17 Feb 2021 23:24:18 GMT CriConnection:warn Cannot create new tab; reusing open tab.
Wed, 17 Feb 2021 23:24:18 GMT status Disconnecting from browser...
Wed, 17 Feb 2021 23:24:18 GMT CriConnection:error sendRawMessage() was called without an established connection.
Wed, 17 Feb 2021 23:24:18 GMT GatherRunner disconnect:error sendRawMessage() was called without an established connection.
} port: 9222127.0.0.1',,terConnect [as oncomplete] (net.js:1144:16) { INFO Error: connect ECONNREFUSED 127.0.0.1:9222
2021-02-17T23:24:18.145Z 8cd07390-6550-40b7-87a5-c162fc55eedf ERROR Invoke Error {"errorType":"Error","errorMessage":"connect ECONNREFUSED 127.0.0.1:9222","code":"ECONNREFUSED","errno":"ECONNREFUSED","syscall":"connect","address":"127.0.0.1","port":9
222,"stack":["Error: connect ECONNREFUSED 127.0.0.1:9222"," at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1144:16)"]
I managed to get result from Lighthouse. Thanks to your example
However, I'm having issue with the Performance score. Most of the time the performance score is returning null. And the error message says:
I'm hoping someone can point out what I did wrong or maybe missing a configuration