Open Facundojs opened 2 years ago
I had a similar issue with my function on Netlify where I needed to make sure chrome-aws-lambda
as an external module.
netlify.toml
[functions.render]
external_node_modules = ["chrome-aws-lambda"]
Perhaps you need one of the Lambda layers to make sure the module is loaded as well?
Any solution to this yet?
Yes, you have use lambda layers In my case i zip a nodejs project for each package (Chrome anda playwright core), uploaded It as lambda layer and included It in serverless template It should work easy
@Facundojs , can I get a repo example of how you used these modules in the lambda handler itself after separating the layers
Assuming you included lambda layers as we talked, you should use a code as below, you can launch browser.
In local await chromium.executablePath
will return null, thats why i use || {my-local-path}
import * as playwright from 'playwright-core';
import chromium from 'chrome-aws-lambda';
export const createBrowserContext: () => Promise<playwright.BrowserContext> =
async () => {
const browser = await playwright.chromium.launch({
headless: chromium.headless || true,
executablePath:
(await chromium.executablePath) || '/bin/google-chrome-stable',
args: chromium.args,
});
return await browser.newContext({
userAgent:
'Mozilla/5.0 (iPhone; CPU iPhone OS 12_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.4 Mobile/15E148 Safari/604.1',
viewport: { width: 808, height: 364 },
screen: { width: 414, height: 896 },
acceptDownloads: true,
deviceScaleFactor: 3,
isMobile: true,
hasTouch: true,
});
};
You shoul use this function for every lambda. Below, i show you an example about the aws-lambda declaration
# Basic
ValidateCredentials:
handler: src/scrapper/validate-credentials/index.handler
layers:
- arn:aws:lambda:us-east-1:{id}:layer:crypto:1
- arn:aws:lambda:us-east-1:{id}:layer:ws-layer:2 # This layer include chromium
- arn:aws:lambda:us-east-1:{id}:layer:playwright:1 # This layer include playwright-core
Thank you a lot so far for your help. I have been able to build/deploy and attach the bundle layer. Now I’m faced with this error when I invoke my lambda cloud version 
Have you encountered such issue?
On 16 Aug 2023, at 02:40, Facundo Julián Serrano @.***> wrote:
Assuming you included lambda layers as we talked, you should use a code as below, you can launch browser. In local await chromium.executablePath will return null, thats why i use || {my-local-path}
import * as playwright from 'playwright-core'; import chromium from 'chrome-aws-lambda';
export const createBrowserContext: () => Promise
= async () => { const browser = await playwright.chromium.launch({ headless: chromium.headless || true, executablePath: (await chromium.executablePath) || '/bin/google-chrome-stable', args: chromium.args, }); return await browser.newContext({ userAgent: 'Mozilla/5.0 (iPhone; CPU iPhone OS 12_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.4 Mobile/15E148 Safari/604.1', viewport: { width: 808, height: 364 }, screen: { width: 414, height: 896 }, acceptDownloads: true, deviceScaleFactor: 3, isMobile: true, hasTouch: true, }); }; You shoul use this function for every lambda. Below, i show you an example about the aws-lambda declaration Basic
ValidateCredentials: handler: src/scrapper/validate-credentials/index.handler layers:
- arn:aws:lambda:us-east-1:{id}:layer:crypto:1
- arn:aws:lambda:us-east-1:{id}:layer:ws-layer:2 # This layer include chromium
- arn:aws:lambda:us-east-1:{id}:layer:playwright:1 # This layer include playwright-core — Reply to this email directly, view it on GitHub https://github.com/alixaxel/chrome-aws-lambda/issues/270#issuecomment-1679843005, or unsubscribe https://github.com/notifications/unsubscribe-auth/ANVQDIDFHV3W3FHIUFAA3CDXVQQKTANCNFSM5S5BHY2A. You are receiving this because you commented.
Here is the generatePDF function and the template for the lambda  
On 16 Aug 2023, at 02:40, Facundo Julián Serrano @.***> wrote:
executablePath: (await chromium.executablePath) || '/bin/google-chrome-stable',
I cant se correctly the file. Looks like a github problem Try copi/paste
I found a solution, but the issue now is how to reference the modules inside the helper method utilized by the generate download handler
On Thu, 17 Aug 2023 at 13:03, Facundo Julián Serrano < @.***> wrote:
I cant se correctly the file. Looks like a github problem Try copi/paste
— Reply to this email directly, view it on GitHub https://github.com/alixaxel/chrome-aws-lambda/issues/270#issuecomment-1682164938, or unsubscribe https://github.com/notifications/unsubscribe-auth/ANVQDIHDGCQPJ56RVTF3C73XVYCBZANCNFSM5S5BHY2A . You are receiving this because you commented.Message ID: @.***>
Im using
chrome-aws-lambda
andplaywright-core
just like the example. At the moment of usage of it in a serverless functions, i get the error"Error: ENOENT: no such file or directory, open '/var/task/src/scrapper/f_service/bin/chromium.br'\"
. Also, i tried to add it in a layer wiht both libraries but still is failing. This is my code:This is how my dir looks like
My system
AWSLambda os is the default