Giter VIP home page Giter VIP logo

aws-lambda-chrome's Introduction

@browserless/aws-lambda-chrome

Last version Dependency status Dev Dependencies Status NPM Status Donate

Chrome (v68.0.3391.0) binary compatible with AWS Lambda.

Install

$ npm install @browserless/aws-lambda-chrome --save

This package content a binary compressed version of Chrome compatible with AWS Lambda.

Usage

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.

API

constructor([options])

options

path

Type: string
Default: 'dist'

Where extract the binary.

FAQ

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 ./

Related

License

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

aws-lambda-chrome's People

Contributors

kikobeats avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.