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'
console.log(error)
}
}
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'