CDK NodejsFunction with Lambda Layer



我想在CDKNodejsFunction中定义的Lambda函数中导入Lambda Layer中的node_modules .
sam local invoke之前将部署的Lambda Layer的arn传递给NodejsFunctionexecute cdk synth --no-staging.
此时,如果您在Lambda函数中导入的模块未安装在CDK node_modules中,您将获得Could not resolve <module name>错误。
因此,如果我们在CDK node_modules (yarn add <module name>)中安装模块,那么相同的本地调用似乎会引用CDK node_modules而不是层。
我如何使Lambda函数引用部署层中的node_modules而不是CDK中的node_modules ?

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

λ/样本函数/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)
}
}

try:

new aws_lambda_nodejs.NodejsFunction(this, 'SampleFunction', {
runtime: aws_lambda.Runtime.NODEJS_14_X,
entry: 'lambda/export-quotation-pdf/index.ts',
bundling:{
externalModules:['date-fns']
}
})

相关内容

  • 没有找到相关文章

最新更新