How To Enforce Serverless Products and services and Run Chrome Headless in AWS Lambda



All about JavaScript, AWS, and Serverless in one position.

Each proprietor of a pc or telephone is aware of what a internet browser is and makes use of it on a daily basis. Even this text was once written and skim in a internet browser. Then again, no longer everyone is aware of that browsers can also be run in a headless mode and be used for resolving more than a few duties but even so easy browsing on the net. On this article, you’re going to be told what headless browsers are, what are the use circumstances and how one can enforce serverless products and services and run Chrome headless in AWS Lambda.

What are Headless Browsers?

The overall definition of a headless browser is lovely simple — this is a browser and not using a consumer interface. One of these browser persists the entire commonplace capability, however since there’s no wish to render any content material to the true display screen, any such browser consumes much less reminiscence, doesn’t require GPU, is extra performant, and can also be managed programmatically. Those options permit builders to successfully make the most of browser chances for enforcing internet crawlers, operating UI assessments, taking screenshots, monitoring internet web page efficiency, automating web page interactions, and different issues. Additional within the article, we can focal point on headless Chrome from AWS Lambda and discover how one can enforce and make the most of each.

How you can Check out Working a Headless Chrome?

Earlier than studying how one can run Headless Chrome on AWS Lambda, let’s move deeper into the functionalities of Headless Chrome. When you’ve got Google Chrome put in in your laptop, you’ll attempt to run it in a headless mode at the moment. As an example, it’s imaginable to transform any internet web page right into a PDF report with a unmarried command. Open the command line, navigate to a folder you’d like to avoid wasting the PDF report to, and execute the next command:

$ chrome --headless --disable-gpu --print-to-pdf

The chrome browser will get started with none consumer interface, it’s going to silently load TechMagic’s web page, render it within the reminiscence as a substitute of the true display screen and create the output.pdf report at the disk in a couple of seconds.

If it doesn’t occur and you notice an error within the console pronouncing that any such command isn’t discovered, you want to inform your device that command “chrome” stands for executing Chrome browser which is situated in its set up folder. If you’re a Home windows consumer, you could take a look at following the course atmosphere directions with a purpose to unravel this factor.

If you’re the usage of Mac, this can also be simply carried out with the following command:

alias chrome="/Programs/Google Chrome"

Now, attempt to rerun the preliminary command. If for some reason why it nonetheless doesn’t paintings, don’t concern, as after studying the under sections it is possible for you to to run a a lot more cool instance on AWS!

Complicated Eventualities with Headless Chrome

Being able to create a PDF or a picture of a web page with a unmarried command is cool, however what about extra advanced eventualities? Is it imaginable to present some directions the browser will have to carry out at the web page? Without a doubt sure!

Chrome DevTools Protocol

Chrome DevTools Protocol or CDP is a distinct approach of speaking with the operating browser example by way of sending particular instructions by means of a community connection to the precise port opened by way of that example.

CDP permits debugging and entirely controlling the browser programmatically, which means that it’s imaginable to jot down a script that may carry out more than a few duties within the browser. As an example, you could increase an application that may run a browser, navigate to a selected web page, click on buttons, fill in paperwork, unload contents, take photos, and plenty of different issues.

In fact, it’s imaginable to jot down such packages from scratch, together with managing the community connection, messaging, and strictly following the protocol by yourself. Then again, the use of particular libraries will save your time and save you errors within the implementation.


Probably the most nice libraries exposing a perfect high-level API for speaking with Chrome or Chromium is Puppeteer. It was once at the beginning written in Node.js after which additionally ported to Python and .Web, however there are some (*10*)choices for different programming languages.

Puppeteer permits you to write sublime and readable code and be aware of the specified capability and eventualities slightly than correct community communique. Additionally it is a serverless internet scraper for Chrome headless that gives automation. Within the observe a part of this text, you’re going to discover operating headless Chrome by means of Puppeteer and coding an app with Nodej.js

Take a look at Challenge: Screenshot Provider

One of the simplest ways of working out issues is by way of attempting them your self. The speculation of a take a look at undertaking is in enforcing a web based app ready to reply with an image of the way a selected web page appears to be like on a selected display screen dimension. The application expects 2 parameters to be despatched as part of the consumer’s request: the URL of the web page to be captured and the display screen answer. As an example, request to: screen=1280,800

This may increasingly lead to a screenshot of the way the TechMagic web page appears to be like in 1280 x 800 display screen answer. Website hosting such apps the usage of a serverless way at the AWS Lambda will be the best choice from each efficiency and value views.

Why AWS Lambda?

(*17*)AWS Lambda is a serverless computing carrier supplied by way of Amazon Internet Products and services, it’s extremely cost-effective and scalable. The principle idea of AWS Lambda purposes is operating code in accordance with more than a few occasions like HTTP requests, adjustments in report garage, messages from different AWS products and services, emails, and different issues taking place within the application.

In flip, you might be billed just for the time taken to execute your serve as and also you by no means pay for the idle. Code runs within the digital stateless packing containers the place one container is processing most effective one tournament at a time and if there are 20 simultaneous incoming occasions AWS will straight away create 20 packing containers to maintain the spike and can shut the ones down afterload decreases. Provider is absolutely controlled by way of Amazon and builders don’t wish to concern about infrastructure and might be aware of the code and application common sense as a substitute. The advantages and boundaries of AWS Lambda aren’t the themes of this text, however the few above details already make AWS Lambda a great choice for our take a look at undertaking!

Imposing a Provider

Earlier than diving into the code, it is important to have Node.js put in in your gadget. You’ll additionally wish to set up (*6*)Serverless Framework — a awesome command-line software for deploying and managing packages according to AWS Lambda. In spite of everything, you’ll have an AWS account and also you will have to (*7*)configure Serverless Framework together with your AWS credentials — it’s going to be no longer imaginable to deploy packages with out doing this. The entire preparation procedure might take a little time, however the ultimate outcome will unquestionably be value it.

Create Challenge and Set up Dependencies

Open terminal, navigate to a listing the place you’d love to stay undertaking recordsdata, and run the next command:

$ serverless create --template aws-nodejs --path screenshot-service

It is going to create a folder “screenshot-service” with some initials recordsdata inside of. Then, navigate into this folder and run instructions for initializing Node.js undertaking and putting in puppeteer.

$ npm init

Merely ascertain the entire activates after which run the bundle:

$ npm set up puppeteer-core

Word that you simply in fact set up the “puppeteer-core” module as a substitute of “puppeteer”. The cause of this is as a result of you do not want a browser itself this is incorporated in a “puppeteer” module, as a substitute, you need to have most effective the communique capability. Chances are you’ll most probably ask “How then the browser gets into Lambda?” — this is a just right query and you’re going to uncover the answer within the under phase.

Serverless Configuration

Open the serverless.yml report, which is the primary configuration report in your application and might encompass dozens of (*5*)houses describing long term carrier and the assets required. Listed here are the entire settings required for the take a look at undertaking, so you’ll merely substitute the content material of your serverless.yml report those:

carrier: screenshot-service
frameworkVersion: '2' supplier: identify: aws runtime: nodejs12.x area: eu-west-1 purposes: seize: handler: handler.seize # refers to serve as seize in handler.js occasions: — http: # cause serve as by means of http request course: seize approach: get memorySize: 1536 # RAM dimension for the serve as timeout: 15 layers: # reference to the already current layer with Chromearn:aws:lambda:european-west-1:764866452798:layer:chrome-aws-lambda:20 

Lots of the houses are relatively self-descriptive, however the closing line is value paying somewhat extra consideration to as a result of it’s in fact the solution to the query about how the browser will get into the Lambda atmosphere. This is carried out by means of AWS Lambda Layers — a characteristic for extending Lambda environments with any essential content material similar to libraries, customized runtimes, binaries (like headless Chrome), and different dependencies. It’s imaginable both to create and put up personal layers or use publicly to be had layers ready by way of third-party organizations, open-source fans, and communities.

Getting ready a layer with a customized binary or binary itself could be a difficult procedure because it calls for execution recordsdata to be at the start compiled in an atmosphere very similar to the AWS Lambda device. So for simplicity on this take a look at undertaking, we can discuss with the current and deployed layer by way of pasting its arn (distinctive useful resource identifier in AWS) into the “layers” belongings in serverless.yml report. Then again, it value noting that during a genuine undertaking you additionally might use third-party layers, however you will have to deploy them in your AWS account with a purpose to save you attainable disasters if the creator eliminates the broadcast layer.

Coding the Serve as

Now it’s time to upload some javascript code liable for processing requests. Open the handler.js report and substitute its content material with the next:

const puppeteer = require("puppeteer-core");
const chrome = require("chrome-aws-lambda"); const seize = async (tournament) => { const { queryStringParameters } = tournament; if (!queryStringParameters || !queryStringParameters.url || !queryStringParameters.display screen) { go back { statusCode: 403 }; } const { url } = queryStringParameters; const [width, height] = queryStringParameters.display screen.break up(","); if (!width || !top) { go back { statusCode: 403 }; } const browser = anticipate puppeteer.release({ executablePath: anticipate chrome.executablePath, args: chrome.args }); const web page = anticipate browser.newPage(); anticipate web page.setViewport({ width: Quantity(width), top: Quantity(top) }); anticipate web page.goto(url); const screenshot = anticipate web page.screenshot({ encoding: "base64" }); go back { statusCode: 200, frame: `<img src="information:symbol/png;base64,${screenshot}">`, headers: { "Content material-Sort" : "textual content/html" } };
} module.exports = { seize };

The code above is relatively self-descriptive and comprehensible, so even though you aren’t a JavaScript knowledgeable, you will have to see the primary drift of directions: take a look at that URL and display screen dimension had been outlined as it should be, then open browser, open a brand new web page, set the specified viewport dimension, navigate to the vacation spot, seize screenshot and ship it.


That is most probably the perfect phase. The one factor you want to do is operating the next command:

$ serverless deploy

If AWS credentials are configured as it should be, the deployment procedure begins and might take a minute or two. After all, you’re going to see the end result, together with an URL of your carrier able to make use of!


Checking out the Ultimate Answer

Reproduction the URL of your serve as endpoint, paste it into your browser’s deal with bar, however don’t publish for now as you first wish to upload the right kind question parameters on the finish of the URL, for instance:

?url= screen=800,600

Hit “Input” and look ahead to a couple of seconds and spot the ensuing symbol!

Chances are you’ll exchange the specified URL and display screen dimension and re-run the question, however consider to make use of a legitimate URL with a legitimate protocol like “http://” or “https://” prepended.

What’s Subsequent?

The opportunity of operating headless Chrome on AWS Lambda finds a large spectrum of more than a few helpful answers to construct and get pleasure from them for builders, testers, and end-users. On this article, you realized the core ideas of executing headless browsers in serverless environments, however with a purpose to successfully increase a real-world app, you want to proceed investigating CDP and studying libraries like Puppeteer. You’ll additionally wish to have a seize of serverless ideas, AWS Lambda quotas, and the Lambda layers control with the intention to strengthen your application or unravel rising problems. Needless to say practising is one of the best ways to consolidate your wisdom.

Written by way of Artem Arkhipov

Internet Skilled at Techmagic, full-stack developer, trainer and speaker. Artem is keen about JavaScript, Cloud Computing and Serverless.


Sign up for Hacker Midday

Create your loose account to liberate your customized studying revel in.