[英]AWS CDK NodejsFunction: import a module exported as "export ="
Is there any way to use modules (eg sharp) exported as export = someModule
in a Lambda function defined using NodejsFunction
in the aws-cdk-lib
?有什么方法可以在使用
aws-cdk-lib
中的NodejsFunction
定义的 Lambda 函数中使用导出为export = someModule
的模块(例如 sharp)?
The require statement( const xxx = require('module')
) does not seem to work with the Lambda TypeScript code that CDK bundles. require 语句 (
const xxx = require('module')
) 似乎不适用于 CDK 捆绑的 Lambda TypeScript 代码。
Both of the following import writing methods resulted in the error.以下两种import写入方式均导致报错。
import sharp from 'sharp'
import * as sharp from 'sharp'
import sharp = require('sharp')
Something went wrong installing the \"sharp\" module
Cannot find module '../build/Release/sharp-linux-x64.node'
Require stack:
- /var/task/index.js
- /var/runtime/index.mjs
The CDK code defines the Lambda function as follows. CDK 代码定义 Lambda 函数如下。
import { aws_lambda_nodejs as lambda } from 'aws-cdk-lib'
const fn = new lambda.NodejsFunction(scope, 'fn-id', {
entry: 'lib/lambda/my-fn.ts',
functionName: 'fn-name'
})
If you're introducing node_modules
or external modules
for your NodejsFunction, you'll need to specify this in your CDK Stack.如果您要为 NodejsFunction 引入
node_modules
或external modules
,则需要在 CDK 堆栈中指定它。
Look at bundling options to learn more.查看捆绑选项以了解更多信息。
Here's an example that uses externalModules
, nodeModules
and layers
to access resources.下面是一个使用
externalModules
、 nodeModules
和layers
访问资源的示例。
this.lambdaFunctionExample= new NodejsFunction(this, `lambdaFunctionExample`, {
runtime: Runtime.NODEJS_18_X,
handler: "handler",
bundling: { minify: false, nodeModules: ["@aws-sdk/client-sfn", "axios", "axios-retry"], externalModules: ["aws-sdk", "crypto-js"] },
layers: [lambdaLayerStack.getSecrets, lambdaLayerStack.generateMagentoAuthorisationHeader]
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.