Run Google Chrome Lighthouse on AWS Lambda.
Since 2.x, lighthouse-lambda has the same major version of lighthouse. For example, lighthouse-lambda 2.x will use lighthouse 2.x.
This README is for version 2.x. To see version 3.x, visit https://github.com/joytocode/lighthouse-lambda/tree/v3.
$ npm install lighthouse-lambda --saveThe postinstall script of lighthouse-lambda will download a Headless Chrome binary if it does not already exist. The binary is from joytocode/headless-chrome-builder and is tested to make sure it works with Lighthouse.
// index.js
const createLighthouse = require('lighthouse-lambda')
exports.handler = function (event, context, callback) {
Promise.resolve()
.then(() => createLighthouse('https://example.com', { logLevel: 'info' }))
.then(({ chrome, start, createReport }) => {
return start()
.then((results) => {
// Do something with `results`
const html = createReport(results)
// Do something with the html report
return chrome.kill().then(() => callback(null))
})
.catch((error) => {
// Handle errors when running Lighthouse
return chrome.kill().then(() => callback(error))
})
})
// Handle other errors
.catch(callback)
}You can use docker-lambda to test your Lambda function locally.
$ docker run --rm -v "$PWD":/var/task lambci/lambda:nodejs8.10 index.handlerYou can use docker-lambda to install dependencies and pack your Lambda function.
$ docker run --rm -v "$PWD":/var/task lambci/lambda:build-nodejs8.10 bash -c "rm -rf node_modules && npm install"
$ docker run --rm -v "$PWD":/var/task lambci/lambda:build-nodejs8.10 bash -c "rm -f *.zip && zip lambda.zip -r node_modules index.js package.json"- The file will be big (at least 80MB), so you need to upload it to S3 then deploy to Lambda from S3.
- You should allocate at least 512 MB memory and 15 seconds timeout to the function.
Same parameters as Using Lighthouse programmatically.
Returns a Promise of an Object with the following fields:
chrome: an instance ofchromeLauncher.launch().log: an instance of lighthouse-logger (only if you setoptions.logLevel).start(options): a function to start the scan which returns aPromiseof Lighthouse results.options.saveArtifacts: a flag to indicate whether result artifacts should be saved (default:false).
createReport(results): a function to create html report from Lighthouse results.