使用AWS CDK NodejsFunction时部分依赖模块运行失败的问题
问题:AWS CDK NodejsFunction创建Lambda后sam local invoke部分模块报错"File not found"
我通过AWS CDK的NodejsFunction创建了一个Lambda函数,在CDK的node_modules目录中安装了Lambda需使用的依赖模块。执行sam local invoke命令时,部分模块可正常运行,部分模块运行失败,报错信息显示“File not found in '/var/task/...'”。请问是否部分模块无法与NodejsFunction兼容?
相关代码示例:
lambda-stack.ts
new lambda.NodejsFunction(this, 'SampleFunction', { runtime: Runtime.NODEJS_14_X, entry: 'lambda/sample-function/index.ts' })
使用date-fns模块(运行成功)
import { format } from 'date-fns' export const handler = async () => { try { console.log(format(new Date(), "'Today is a' eeee")) } catch (error) { console.log(error) } }
使用chrome-aws-lambda模块(运行失败)
const chromium = require('chrome-aws-lambda') export const handler = async () => { try { const browser = await chromium.puppeteer.launch() } catch (error) { // Cannot find module '/var/task/puppeteer/lib/Browser' console.log(error) } }
使用pdfkit模块(运行失败)
const PDFDocument = require('pdfkit') export const handler = async () => { try { const doc = new PDFDocument() } catch (error) { // no such file or directory, open '/var/task/data/Helvetica.afm' console.log(error) } }
内容的提问来源于stack exchange,提问作者kusumoto_teruya
相关产品推荐
相关产品推荐

