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)
  }
}
asked 2 years ago303 views
1 Answer
0

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

profile pictureAWS
answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions