Chrome (v68.0.3391.0) binary compatible with AWS Lambda.
$ npm install @browserless/aws-lambda-chrome --save
This package content a binary compressed version of Chrome compatible with AWS Lambda.
Note: Ensure to use
--disable-dev-shm-usage
flag.
It has been designed to be used with puppeteer, specially using browserless.
For example, let create a get-browserless.js
file with the follow content:
const browserless = require('browserless')
const getChromePath = require('@browserless/aws-lambda-chrome')({
path: '/tmp'
})
const isLambda = !!process.env.LAMBDA_TASK_ROOT
const getExecutablePath = async () => (isLambda ? getChromePath() : undefined)
module.exports = async () =>
browserless({
ignoreHTTPSErrors: true,
args: [
'--disable-gpu',
'--single-process',
'--no-zygote',
'--no-sandbox',
'--disable-dev-shm-usage',
'--hide-scrollbars'
],
executablePath: await getExecutablePath()
})
Then in your code just call the snippet, like:
;(async () => {
const getBrowserless = require('./get-browserless')
const { screenshot } = await getBrowserless() // serverless!
})()
The first time it will be used it will decompress the file. After that, the decompress version will be reused at next successive calls.
Type: string
Default: 'dist'
Where extract the binary.
Q: How to use a newer version of Node on AWS Lambda?
AWS Lambda runs a old version of Node.js that doesn't support async/await, spread operator, etc.
You need to ship your fresh Node.js version with your lambda function.
This is an shell bash script to get the last node version available:
#!/bin/bash
NODE_BIN=`curl -sL https://semver.io/node/resolve/8`-linux-x64
echo "Downloading $NODE_BIN binary file"
curl -sL https://nodejs.org/dist/latest-v8.x/node-v$NODE_BIN.tar.gz | tar -xz
mkdir -p bin
mv node-v$NODE_BIN/bin/node bin/node
rm -rf node-v$NODE_BIN
echo "Added \`bin/node\` as node-v$NODE_BIN"
You can run the script as postinstall and then user the binary created for run your microservice:
{
"scripts": {
"postinstall": "./scripts/download_node.sh",
"start": "NODE_ENV=production ./bin/node ./node_modules/micro/bin/micro.js --host localhost --port $PORT index.js"
}
}
Q: How Can I create my own Headless Chrome binary?
The idea behind the project is to ship a production-ready last chrome version available.
For do that, is necessary compile Chromium into a AWS Lambda compatible machine, normally a EC2.
These instructions are related with generate the binary for the last (or older) version.
You need to create a EC2 instance:
- AMI:
amzn-ami-hvm-2017.09.0.20170930-x86_64-gp2
(Latest community Amazon Linux). - Instance type: c4.4xlarge.
- Storage: 30GB.
Then execute the script for build the binary:
curl https://rawgit.com/Kikobeats/aws-lambda-chrome/master/build.sh | bash
After the process finished, copy the result (and create a PR with the new binary!)
cd out
export VERSION=<put chrome version here>
tar -zcvf chrome-headless-aws-lambda-$VERSION-x64.tgz ./headless_shell
scp -i path/to/your/key-pair.pem ec2-user@<instance-hostname>:path/to/tarball ./
- browserless – Chrome Headless API made easy.
aws-lambda-chrome © Kiko Beats, Released under the MIT License.
Authored and maintained by Kiko Beats with help from contributors.
kikobeats.com · GitHub Kiko Beats · Twitter @kikobeats