Search code examples
amazon-web-servicesaws-lambdaaws-amplify

How to add chrome to AWS Lambda Api?


I'm making an Api that returns a pdf by puppeteer. I just instaled puppeteer with npm install chrome-Aws-lambda, install puppeteer --save-dev but when I run the Api, I get this exception.

enter image description here

I tried runing npm install but it doesn't work, how can I install chromium or make puppeteer works???

this is my code and package.json

let browser = await chromium.puppeteer.launch({ headless: true });
    let page = await browser.newPage();
    await page.goto("https://www.google.com");

    const pdf = await page.pdf({
        format: "A4",
        printBackground: false,
        preferCSSPageSize: true,
        displayHeaderFooter: false,

        headerTemplate: `<div class="header" style="font-size:20px; padding-left:15px;"><h1>Main Heading</h1></div> `,
        footerTemplate: '<footer><h5>Page <span class="pageNumber"></span> of <span class="totalPages"></span></h5></footer>',
        margin: { top: "200px", bottom: "150px", right: "20px", left: "20px" },
        height: "200px",
        width: "200px",
    });

    return {
        statusCode: 200,
        //  Uncomment below to enable CORS requests
        headers: {
            "Access-Control-Allow-Origin": "*",
            "Access-Control-Allow-Credentials": true
        },
        body: pdf
    };

Pakage:

{
"name": "amplifysandboxpdf",
"version": "2.0.0",
"description": "Lambda function generated by Amplify",
"main": "index.js",
"license": "Apache-2.0",
"devDependencies": {
  "@types/aws-lambda": "^8.10.92",
  "@types/puppeteer": "^5.4.6",
"puppeteer": "^17.1.2"
},
"dependencies": {
   "chrome-aws-lambda": "^10.1.0",
   "puppeteer-core": "^10.0.0"
  } 
}

Solution

  • Usually, when you upload a zip to lambda, you need to provide both - your source code and node_modules (modules can also be added as a lambda layer). In your case error is because of missing package, so the first place I'd look at is the zip you provide to lambda and does it contain all the necessary packages

    I see in your package.json you have both puppeteer and puppeteer-core. Puppeteer downloads chromium on installation (that's the bit that errors for you). First you need to decide if puppeteer is even necessary, maybe core package is enough, but if it is necessary - once again, it should be in your zip that you provide.

    I'm not sure about how puppeteer does this, but if it downloads chromium into node_modules it should work once your zip file is correctly packaged. Otherwise, you might need to create your own docker container by using puppeteer base image Puppeteer base image. If this is the case, these are the steps you need to take:

    • Create a dockerfile with puppeteer base image, build your own image and make sure your application is installed in there. Example of what this file could look like:
    FROM ghcr.io/puppeteer/puppeteer:16.1.0
    
    WORKDIR /home/pptruser/app
    ADD ./package-lock.json /home/pptruser/app/package-lock.json
    ADD ./package.json /home/pptruser/app/package.json
    RUN npm ci
    # Customize the line below - copy files that your application requires
    ADD ./src/. /home/pptruser/app/src/
    # Remove development dependencies (in your case puppeteer is not a devDependency)
    RUN npm prune --production
    
    CMD [ "index.js" ]
    
    • Test locally if it works in the container and later publish this image to ECR (AWS registry for docker containers)
    • Launch lambda by using the image instead of using NodeJS environment