All Projects → deepsyx → simple-headless-browser-serverless

deepsyx / simple-headless-browser-serverless

Licence: MIT license
Simple example of how to use Chrome as headless browser on AWS lambda

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to simple-headless-browser-serverless

browser-pool
A Node.js library to easily manage and rotate a pool of web browsers, using any of the popular browser automation libraries like Puppeteer, Playwright, or SecretAgent.
Stars: ✭ 71 (+343.75%)
Mutual labels:  headless-browsers, puppeteer
Puppeteer-IE
Headless Internet Explorer NodeJS API inspired by Puppeteer
Stars: ✭ 72 (+350%)
Mutual labels:  headless-browsers, puppeteer
CrawlerSamples
This is a Puppeteer+AngleSharp crawler console app samples, used C# 7.1 coding and dotnet core build.
Stars: ✭ 36 (+125%)
Mutual labels:  headless-browsers, puppeteer
trafficator
Traffic generator for local analytics testing
Stars: ✭ 27 (+68.75%)
Mutual labels:  puppeteer
Puppetry
基于Puppeteer的页面E2E测试GUI工具
Stars: ✭ 35 (+118.75%)
Mutual labels:  puppeteer
purr
PURR (PUppeteer RunneR) is a devops-friendly tool for browser testing and monitoring.
Stars: ✭ 37 (+131.25%)
Mutual labels:  puppeteer
ubuntu-vnc-xfce-firefox
Retired. Headless Ubuntu/Xfce containers with VNC/noVNC and Firefox (Generation 1)
Stars: ✭ 20 (+25%)
Mutual labels:  headless-browsers
mugshot
Framework independent visual testing library
Stars: ✭ 126 (+687.5%)
Mutual labels:  puppeteer
after-work.js
[DEPRECATED] CLI for automated tests in web projects.
Stars: ✭ 56 (+250%)
Mutual labels:  puppeteer
hardcider
🍺 CLI for quickly generating citations for websites and books
Stars: ✭ 17 (+6.25%)
Mutual labels:  puppeteer
MTS
Automation Tools for PHP
Stars: ✭ 111 (+593.75%)
Mutual labels:  headless-browsers
readme-ascii
Turns text into images of ASCII art for GitHub README files.
Stars: ✭ 48 (+200%)
Mutual labels:  puppeteer
shuttlepdf
📃 Smashingly simple, and scalable ("serverless") HTML to PDF conversions using Google Cloud Functions, and Puppeteer.
Stars: ✭ 15 (-6.25%)
Mutual labels:  puppeteer
http-server-pwa
👾 http-server alike but for serving and rendering PWA: pwa-server
Stars: ✭ 14 (-12.5%)
Mutual labels:  puppeteer
puppeteer-autoscroll-down
Handle infinite scroll on websites by puppeteer
Stars: ✭ 40 (+150%)
Mutual labels:  puppeteer
puppeteer-loadtest
load test puppeteer (Headless Chrome API) script using node
Stars: ✭ 107 (+568.75%)
Mutual labels:  puppeteer
shallty
Let me suck your fucking trash fansub!
Stars: ✭ 30 (+87.5%)
Mutual labels:  puppeteer
simplechrome
Webrecorders DevTools Protocol Automation Library
Stars: ✭ 16 (+0%)
Mutual labels:  puppeteer
akamai-toolkit
A set of tools to work on Akamai v1 anti-bot solution. Current supported version: 1.70
Stars: ✭ 215 (+1243.75%)
Mutual labels:  puppeteer
puppeteer-proxy
Proxies Puppeteer Page requests.
Stars: ✭ 172 (+975%)
Mutual labels:  puppeteer

logo

The goal of this repo is to provide a basic example of how to get headless browser running on AWS at scale.

How it works

AWS Lambda lets you run code without provisioning or managing servers. You pay only for the compute time you consume. Basically, you set the required hardware parameters of your code and upload the bundle to AWS and they handle the rest. Let's say one HTML to PDF takes 2 seconds to complete while using 100% CPU. If we upload our code to a single-core instance, we'll be limited to 1 render per 2 seconds and you'll be still paying for the instance time while idle. However, with AWS lambda you can run your code unlimited times simultaneously and it'll be executed in parallel. We'll be using the serverless framework to deploy our bundle - it makes the whole process as easy as executing a single command.

"Puppeteer is a Node library that provides a high-level API to control Chrome or Chromium over the DevTools Protocol. Puppeteer runs headless by default, but can be configured to run full (non-headless) Chrome or Chromium."

Since AWS Lambdas is running on Amazon Linux, we'll have to use a Chrome compiled for Amazon Linux, so we use the chrome-aws-lambda package which is providing the ready-to-use binaries.

How to deploy this repo

Setup your AWS credentials then run:

npm install serverless -g
git clone [email protected]:deepsyx/serverlessdemo.git
cd ./serverlessdemo
yarn
serverless deploy

And you should see something like: deploy image

Example

const https = require("https");
const fs = require("fs");

const data = JSON.stringify({ url: "https://example.com", format: "png" });

const options = {
  hostname: "5zxn2gg42h.execute-api.us-east-1.amazonaws.com",
  port: 443,
  path: "/dev/",
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    "Content-Length": data.length
  }
};

const req = https.request(options, res => {
  const buffer = [];

  res.on("data", d => {
    buffer.push(d);
  });

  res.on("end", d => {
    const extension = res.headers["content-type"].includes("/pdf")
      ? "pdf"
      : "png";

      const fileName = `./test.${extension}`;
    fs.writeFileSync(fileName, Buffer.concat(buffer));
    console.log(`Wrote file: ${fileName}`);
  });
});

req.on("error", error => {
  console.error(error);
});

req.write(data);
req.end();

Request properties

Property Type Example Description
url String(Optional) "https://example.com" URl to be screenshoted
html String(Optional) "Hello world!" If we don't want to screenshot a url, we can manually provide the html
format String "pdf" or "png" Specifies the output format
viewport Object (optional) viewport: { width: 1920, height: 1080 } Specifies the browser viewport

Last words

Feel free to fork this repo and extend it with your own functionally, if you find it useful. Improvement pull requests are welcome, as long as they're well formatted and prettified with yarn prettify.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].