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)
  }
}
gefragt vor 2 Jahren325 Aufrufe
1 Antwort
0

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

profile pictureAWS
beantwortet vor 2 Jahren

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen