我正在尝试通过 CDK 部署在打字稿中实现的 lambda。
我能够成功部署 lambda,但是当我测试它时,出现以下错误:
{
"errorType": "Runtime.ImportModuleError",
"errorMessage": "Error: Cannot find module 'index'\nRequire stack:\n- /var/runtime/index.mjs",
"trace": [
"Runtime.ImportModuleError: Error: Cannot find module 'index'",
"Require stack:",
"- /var/runtime/index.mjs",
" at _loadUserApp (file:///var/runtime/index.mjs:996:17)",
" at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1031:21)",
" at async start (file:///var/runtime/index.mjs:1194:23)",
" at async file:///var/runtime/index.mjs:1200:1"
]
}
lambda 在 CDK 中声明如下:
const postImages = new lambda.Function(scope, utils.prefixed('post_images'), {
runtime: lambda.Runtime.NODEJS_18_X,
handler: 'index.lambdaHandler',
code: lambda.Code.fromAsset('./sd_service/post_images')
})
的内容./sd_service/post_images
看起来像这样:
索引.ts:
import { Context, APIGatewayEvent, APIGatewayProxyResult } from 'aws-lambda';
export const lambdaHandler = async (event: APIGatewayEvent, context: Context): Promise<APIGatewayProxyResult> => {
console.log(`Event: ${JSON.stringify(event, null, 2)}`);
console.log(`Context: ${JSON.stringify(context, null, 2)}`);
return {
statusCode: 200,
body: JSON.stringify({
message: 'hello world',
}),
};
};
tsconfig.json:
{
"$schema": "https://json.schemastore.org/tsconfig",
"display": "Node 18",
"compilerOptions": {
"lib": ["es2022"],
"module": "commonjs",
"target": "es2022",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"moduleResolution": "node"
},
"exclude": ["node_modules", "**/*.test.ts"]
}
包.json:
{
"name": "post_images",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"@types/aws-lambda": "^8.10.111",
"@types/node": "^18.14.6",
"aws-sdk": "^2.1330.0",
"typescript": "^4.9.5"
},
"dependencies": {
"aws-lambda": "^1.0.7"
}
}
我做错了什么,如何让 lambda 工作?