Some modules can be used with NodejsFunction(AWS CDK) and some cannot?

0

I defined a lambda function using NodejsFunction of AWS CDK.
I installed the modules I want to use in my lambda function in node_modules of CDK.
When I execute sam local invoke, some modules succeed and others fail.
When an error occurs, the error message "File not found in '/var/task/...'" is displayed.
Does this mean that some modules can be used with NodejsFunction and some cannot?

lambda-stack.ts

new lambda.NodejsFunction(this, 'SampleFunction', {
  runtime: Runtime.NODEJS_14_X,
  entry: 'lambda/sample-function/index.ts'
})

lambda/sample-function/index.ts (use 'date-fns') -> succeeded!

import { format } from 'date-fns'

export const handler = async () => {
  try {
    console.log(format(new Date(), "'Today is a' eeee"))
  } catch (error) {
    console.log(error)
  }
}

lambda/sample-function/index.ts (use 'chrome-aws-lambda') -> failed

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)
  }
}

lambda/sample-function/index.ts (use 'pdfkit') -> failed

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)
  }
}
已提問 2 年前檢視次數 325 次
1 個回答
0

You need to make sure and add the modules like this here

profile pictureAWS
已回答 2 年前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南