alixaxel / chrome-aws-lambda

Chromium Binary for AWS Lambda and Google Cloud Functions
MIT License
3.17k stars 289 forks source link

Incorrectly formed path to /bin/chromium.br' #270

Open Facundojs opened 2 years ago

Facundojs commented 2 years ago

Im using chrome-aws-lambda and playwright-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:

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({
    args: chromium.args,
    executablePath: await chromium.executablePath',
    headless: chromium.headless,
  });
  return await browser.newContext();
};

This is how my dir looks like

.
├── layers
│   └── chromium
│       └── nodejs
├── resources
└── src
    ├── scrapper
    │   ├── freelancer
    │   │   ├── basic
    │   │   ├── check-service-state
    │   │   ├── record
    │   │   ├── emit-invoice
    │   │   ├── generate-vep
    │   │   ├── get-invoices
    │   │   ├── get-sale-points
    │   │   └── get-veps
    │   ├── workers
    │   │   ├── debt
    │   │   ├── basic
    │   │   ├── check-service-state
    │   │   └── generate-vep
    │   └── validate-credentials
    └── utils

My system

Architecture:                    x86_64
CPU op-mode(s):                  32-bit, 64-bit
Byte Order:                      Little Endian
CPU(s):                          12
Vendor ID:                       AuthenticAMD
CPU family:                      23
Model:                           8
Model name:                      AMD Ryzen 5 2600X Six-Core Processor
Distributor ID: Ubuntu
Description:    Ubuntu 21.10
Release:    21.10

AWSLambda os is the default

Soviut commented 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?

https://github.com/shelfio/chrome-aws-lambda-layer

nwaguvictor commented 10 months ago

Any solution to this yet?

Facundojs commented 10 months ago

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

nwaguvictor commented 10 months ago

@Facundojs , can I get a repo example of how you used these modules in the lambda handler itself after separating the layers

Facundojs commented 10 months ago

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
nwaguvictor commented 10 months ago

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:

nwaguvictor commented 10 months ago

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',

Facundojs commented 10 months ago

I cant se correctly the file. Looks like a github problem Try copi/paste

nwaguvictor commented 10 months ago

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: @.***>