puppeteer on Lambda


I want to use puppeteer on Lambda to convert HTML to PDF.
With the chrome-aws-lambda module or the puppeteer module, I'm trying to run (sam local invoke) a function that calls puppeteer.launch(), but the function returns an error.
If anyone is running puppeteer on Lambda, please let me know how you configured it!

CDK Stack

const pdfExportFunction = new lambda.NodejsFunction(this, 'ExportPDF', {
  runtime: Runtime.NODEJS_14_X,
  entry: 'lambda/export-pdf/index.ts',
  timeout: Duration.seconds(10),
  memorySize: 1024,
  // https://github.com/shelfio/chrome-aws-lambda-layer
  layers: [
    LayerVersion.fromLayerVersionArn(this, 'layer:chrome-aws-lambda', 'arn:aws:lambda:ap-northeast-1:764866452798:layer:chrome-aws-lambda:31')

Lambda Function Code (lambda/export-pdf/index.ts)

const chromium = require('@sparticuz/chrome-aws-lambda')

export const handler = async () => {
  try {
    const browser = await chromium.puppeteer.launch()
  } catch (error) {
    // Error: Cannot find module '/var/task/puppeteer/lib/Browser'

I've tried several different ways to write imports, but each one gives me different errors when call puppeteer.launch().

// Error: Cannot find module '/var/task/puppeteer/lib/Browser'
const chromium = require('chrome-aws-lambda')
// Error: _projectRoot is undefined. Unable to create a BrowserFetcher.
const puppeteer = require('puppeteer')
// The argument 'filename' must be a file URL object, file URL string, or absolute path string. Received undefined
import * as puppeteer from 'puppeteer'
asked 4 months ago80 views
No Answers

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions