By using AWS re:Post, you agree to the Terms of Use

CDK NodejsFunction with Lambda Layer

0

I want to import node_modules in Lambda Layer in a Lambda function defined in CDK NodejsFunction.
Pass the deployed Lambda Layer's arn to NodejsFunction and execute cdk synth --no-staging before sam local invoke.
At this time, if the module you are importing in the Lambda function is not installed in the CDK node_modules, you will get a Could not resolve <module name> error.
So, if we install the module in the CDK node_modules (yarn add <module name>), the sam local invoke will seem to refer to the CDK node_modules instead of the layer.
How can I make the Lambda function reference the node_modules in the deployed layer instead of the node_modules in the CDK?

lambda-stack.ts

new lambda.NodejsFunction(this, 'ExportQuotationPDF', {
  runtime: Runtime.NODEJS_14_X,
  entry: 'lambda/export-quotation-pdf/index.ts',
  layers: [
    LayerVersion.fromLayerVersionArn(
      this,
      'layer:datefns',
      'arn:aws:lambda:ap-northeast-1:1234567890:layer:datefns:1
    )
  ]
})

lambda/sample-function/index.ts

// When I run cdk synth without running `yarn add data-fns`, it returns the following error
// Could not resolve "date-fns"
// But if I do `yarn add data-fns`,
// this function appears to reference the node_modules in the CDK, not the layers.
import { format } from 'date-fns'

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