

Which is the case for chrome-aws-lambda-layer. If the owner grants public access, then any account can use this ARN to reference the layer and use it for their own functions. This is how we tell Serverless: “hey, when you create the function, add these layers”.Įvery layer has an ARN, pretty much as any other AWS resource. aws-sdk is always preinstalled in AWS Lambda in all Node.js runtimesĬonst S3Client = require("aws-sdk/clients/s3") Ĭonst s3 = new S3Client(:764866452798:layer:chrome-aws-lambda:10 this module will be provided by the layerĬonst chromeLambda = require("chrome-aws-lambda") Let’s take a look at the code: // src/capture.js #1 comes in the Lambda event object #2 we can implement with puppeteer for #3 we can use the AWS SDK and #4 is just returning the URL string of the uploaded image.

So the only purpose for installing this module as a dev dependency is just to get some nice intellisense and avoid IDE complains that “the module doesn’t exist”. The nice folks at shelf.io packaged the module in a layer and published it in all AWS regions, and because layers can be shared across accounts, we can use it just by declaring it in our function definition (more on that later). How is that possible? Well, by using a Lambda layer that provides this module. AWS), the module will be preinstalled and ready to use. “Wait Luis, you are installing it wrong! It’s not a dev dependency, since we are going to use this module in our function!” Internally, the module does all kind of bootstrapping and wiring, but all we really need to care is to get the puppeteer instance via the exposed property: const chromeLambda = require("chrome-aws-lambda") Īll good, so far? Cool, let’s install this module then: npm install -save-dev chrome-aws-lambda
#Npm install serverless save dev plus
I was happy to come across this module that elegantly simplifies the whole process, plus it works in Node LTS 8 to 12. So instead of using puppeteer directly, we’ll use chrome-aws-lambda. In a Lambda environment this is tricky and I ran into several problems – failing to bundle the right browser version exceeding package size limits imposed by Lambda facing incompatibility issues between Chromium version and puppeteer API getting it to work in Node 8 but not in Node 12. Now in a typical puppeteer project, we are supposed to install puppeteer and use it like this: const puppeteer = require("puppeteer") Next, we need to install Serverless (as a dev dependency, since it’s not going to be used by our function): npm install -save-dev serverless I’ll keep it as simple as possible in order to focus on what’s relevant here. Creating the projectįirst, we’ll create the folder structure for our Serverless project and initialize a Node.js project in there.

To keep it really simple, let’s say all the function needs to do is take a screenshot of the given URL page and upload the image to S3. We don’t want to run servers, so we’ll implement this as a Lambda function. The goal here is to have a solution for capturing screenshots of web pages. Let’s get started! Screenshots: THE FUNCTION I’m assuming you have a configured environment with AWS CLI installed, proper AWS credentials and Serverless. Me trying to find the definitive article on Chromium and Lambda.įor this project I decided to use Serverless and Lambda Layers.
