当lambda函数通过cdk进行部署时,没有在该函数中导入Typescript模块



我使用CDK在AWS中开发我的无服务器应用程序。当我试图在TS文件编译后部署lambda函数post时,它不会将lambda函数中的TS模块作为JS模块导入。

因此,当我调用lambda时,我面临一个Module not found错误。

我在部署堆栈之前遵循的步骤:

  1. tsc->以编译TS文件
  2. cdk合成器
  3. cdk部署

Ts-config:

{
"compilerOptions": {
"target": "ES2018",
"module": "commonjs",
"lib": [
"es2018"
],
"noEmit": false,
"declaration": false,
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"noImplicitThis": true,
"alwaysStrict": true,
"noUnusedLocals": false,
"noUnusedParameters": false,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": false,
"inlineSourceMap": true,
"inlineSources": true,
"experimentalDecorators": true,
"strictPropertyInitialization": false,
"typeRoots": [
"./node_modules/@types"
]
},
"exclude": [
"node_modules",
"cdk.out"
]
}

代码:

import { APIGatewayProxyEventV2, APIGatewayProxyResultV2 } from 'aws-lambda';
import { DynamoDBClient, GetItemCommand, GetItemCommandInput } from "@aws-sdk/client-dynamodb";
import { marshall, unmarshall } from "@aws-sdk/util-dynamodb";
export async function handler(event: APIGatewayProxyEventV2,): Promise<APIGatewayProxyResultV2> {
console.log('event', event);
let userLat = event.pathParameters?.lat;
let userLng = event.pathParameters?.lng;    
let tableName = process.env.TABLE_NAME;
let results;
const dbClient: DynamoDBClient = new DynamoDBClient({ region: "ap-south-1" });
let params: GetItemCommandInput;
if (tableName) {
params = {
TableName: tableName,
Key: marshall({
"lat": userLat, 
"lng": userLng
}),
};
}
const run = async function () {
try {
const resp = await dbClient.send(new GetItemCommand(params));
results = unmarshall(resp.Item || {});
} catch(err) {
results = err;
}
};
run();
return {
body: JSON.stringify(
results
),
statusCode: 200
};     }

详细信息:

  • 节点:v14.20.0
  • NPM:6.14.17
  • CDK:2.40.0(内部版本56ba2ab(
  • Ts:版本4.8.2

错误:

{"errorType":"Runtime.ImportModuleError";,"errorMessage":"错误:找不到模块'@aws-sdk/client-dynolderdb'\n要求堆栈:\n-/var/task/fetchpartner.js\n-/var/runtime/UserFunction.js\n-/var/runtime/runtime.js\n-/var/runtime/index.js","trace":["Runtime.ImportModuleError:错误:找不到模块'@aws sdk/client dynawdb'","需要堆栈:";,"-/var/task/fitchpartner.js";,"-/var/runtime/UserFunction.js";,"-/"var/runtime/runtime.js";,"-/var/runtime/index.js";,"at_loadUserApp(/var/runtime/UserFunction.js:22:13(";,"在Object.module.exports.load(/var/runtime/UserFunction.js:279:17(";,"在对象处。(/var/runtime/index.js:43:34(";,"在模块中_compile(internal/modules/cjs/loader.js:1085:14(";,"在Object.Module_extensions.js(internal/modules/cjs/loader.js:1114:10(处";,"在Module.load(internal/modules/cjs/loader.js:950:32(";,"在Function.Module_load(internal/modules/cjs/loader.js:790:12(处";,"在Function.executeUserEntryPoint[作为runMain](internal/modules/run_main.js:75:12(";,"在internal/main/run_main_module.js:17:47"处;]}

您可以在包含lambda代码的目录中运行npm init,并在那里安装所需的模块。另一种方法是使用类似webpack的东西将代码和模块编译并组合到一个文件中。

您可以使用NodeJsFunction而不是Lambda函数。这将在部署之前在目录中执行esbuild。

https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_lambda_nodejs.NodejsFunction.html

https://github.com/schuettc/single-stack-full-stack-example/blob/9c5c3f51381a4c3d813ed097a298fccc16e81509/src/infrastructure.ts#L38-L51

相关内容

  • 没有找到相关文章

最新更新