Challenges Faced with AWS CDK, TypeScript Lambda, and Lambda Layers - Issue: chromium.executablePath
Not Functioning Properly
Currently, I am in the process of developing an AWS Lambda function using TypeScript along with AWS CDK for its deployment. The main functionality of my Lambda function involves utilizing Puppeteer and a customized Chromium build to generate PDF files. Unfortunately, I have encountered a TypeError
specifically related to the operation of chromium.executablePath
.
Dependencies within Layer:
In relation to the Lambda Layer, my package.json
lists the following dependencies:
"dependencies": {
"@sparticuz/chromium": "^106.0.2",
"puppeteer-core": "^18.0.5"
}
Lambda Function Implementation:
This excerpt showcases the relevant section from my Lambda function:
const puppeteer = require("/opt/nodejs/puppeteer-core");
const chromium = require("/opt/nodejs/@sparticuz/chromium");
async function createPdf(fileLinks: Record<string, string>, logoDataUri: string, bucketName: string, key: string): Promise<Buffer> {
await chromium.font("https://raw.githack.com/googlei18n/noto-emoji/master/fonts/NotoColorEmoji.ttf"); // Add Font
const browser = await puppeteer.launch({
args: chromium.args,
defaultViewport: chromium.defaultViewport,
executablePath: await chromium.executablePath(),
headless: chromium.headless,
});
// ... remaining functions
}
// Continuing logic for the function...
Error Encounter Details:
Following the Lambda invocation, the error received is as follows:
{
"errorType": "TypeError",
"errorMessage": "chromium.executablePath is not a function",
"trace": [
"TypeError: chromium.executablePath is not a function",
" at createPdf (/var/task/index.js:70735:36)",
" at process.processTicksAndRejections (node:internal/process/task_queues:95:5)",
" at async Runtime.handler (/var/task/index.js:70601:21)"
]
}
CDK Setup Definitions:
The layer and Lambda function definitions in my CDK stack are configured as follows:
this.chromiumLayer = new LayerVersion(this, "chromiumLayer", {
code: Code.fromAsset(join(__dirname, "../../src/layers/chromium")),
compatibleRuntimes: [Runtime.NODEJS_20_X],
compatibleArchitectures: [Architecture.X86_64, Architecture.ARM_64],
description: "Chromium layer for Lambda",
});
this.createDownloadPDF = new NodejsFunction(this, "createDownloadPDF", {
// ... other configurations
layers: [this.chromiumLayer],
bundling: {
externalModules: ["aws-sdk", "@sparticuz/chromium", "puppeteer-core"],
// ... other bundling options
},
});
tsconfig Entry:
"paths": {
"/opt/nodejs/puppeteer-core": ["./src/layers/chromium/nodejs/node_modules/puppeteer-core"],
"/opt/nodejs/@sparticuz/chromium": ["./src/layers/chromium/nodejs/node_modules/@sparticuz/chromium"]
}
Queries:
- Why is the
TypeError
regardingchromium.executablePath
occurring? - Is there a specific method recommended to bundle or configure Lambda layers when using AWS CDK in this scenario?
- Are additional steps necessary to ensure proper integration of
@sparticuz/chromium
with Puppeteer within a Lambda environment?
Your insights and potential solutions to resolve this issue will be highly appreciated. Thank you!