我正在尝试通过无服务器框架的
sls deploy
命令部署我的 lambda 函数。部署进行得很顺利,但是当 lambda 运行时,我收到以下错误。
{
"errorType": "Runtime.ImportModuleError",
"errorMessage": "Error: Cannot find module 'lambda'\nRequire stack:\n- /var/runtime/index.mjs",
"stack": [
"Runtime.ImportModuleError: Error: Cannot find module 'lambda'",
"Require stack:",
"- /var/runtime/index.mjs",
" at _loadUserApp (file:///var/runtime/index.mjs:1087:17)",
" at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1119:21)",
" at async start (file:///var/runtime/index.mjs:1282:23)",
" at async file:///var/runtime/index.mjs:1288:1"
]
}
我的
src
或 build
文件夹中没有任何 index.mjs 文件,因此我不确定下一步要做什么来解决此问题。我在这里和其他地方遇到了一些类似的问题,但这些解决方案实际上都不适合我。
我的项目文件夹结构
我的
Serverless.yml
文件
service: api-gateway-websocket-poc
frameworkVersion: '3'
provider:
name: aws
runtime: nodejs18.x
stage: ${opt:stage, 'dev'}
region: ${opt:region, 'us-east-2'}
websocketsApiName: api-gateway-ws-poc-${self:provider.stage}
websocketsApiRouteSelectionExpression: $request.body.action
websocketsDescription: API Gateway WS POC
iamRoleStatements:
- Effect: Allow
Action:
- "execute-api:ManageConnections"
Resource:
- "arn:aws:execute-api:*:*:**/@connections/*"
functions:
connectionHandler:
handler: src/lambda.handler
events:
- websocket:
route: $connect
- websocket:
route: $disconnect
defaultHandler:
handler: src/lambda.handler
events:
- websocket:
route: $default
actionHandler:
handler: src/lambda.handler
events:
- websocket:
route: myAction
我的
lambda.ts
文件
import * as AWS from 'aws-sdk';
import { APIGatewayProxyHandler, APIGatewayProxyResult } from "aws-lambda";
export const handler: APIGatewayProxyHandler = async (
event,
context,
callback
): Promise<APIGatewayProxyResult> => {
console.log("on send message", event.requestContext.connectionId, event.requestContext.routeKey, event.body);
const {
routeKey,
connectionId,
domainName,
stage
} = event.requestContext;
return { statusCode: 200, body: 'Data sent.' };
};
我的
tsconfig.json
{
"compilerOptions": {
"composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
"target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
"module": "commonjs", /* Specify what module code is generated. */
"rootDir": "./src", /* Specify the root folder within your source files. */
"allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */
"outDir": "./build", /* Specify an output folder for all emitted files. */
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
"strict": true, /* Enable all strict type-checking options. */
"skipLibCheck": true /* Skip type checking all .d.ts files. */
}
}
包.json
{
"name": "api-gateway-websocket-poc",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "nodemon src/app.ts",
"start": "tsc && node build/app",
"build": "npm run clearBuild && npm run tsc",
"clearBuild": "npm run rimraf -- build",
"rimraf": "rimraf",
"tsc": "tsc"
},
"dependencies": {
"express": "^4.18.2",
"serverless-http": "^3.1.1"
},
"devDependencies": {
"@types/aws-lambda": "^8.10.137",
"@types/express": "^4.17.21",
"@types/node": "^20.12.11",
"aws-sdk": "^2.1617.0",
"nodemon": "^3.1.0",
"rimraf": "^5.0.5",
"serverless": "^3.38.0",
"ts-node": "^10.9.2",
"typescript": "^5.4.5"
}
}
我正在通过简单的操作从本地计算机部署它
SLS_DEBUG=* serverless deploy --stage dev --region us-east-2
我试图了解 index.mjs 文件的来源,或者我是否需要在部署到 AWS 之前更改自己的模式。
package.json
/安装以下pkg:"serverless-plugin-typescript"
serverless.yml
文件:plugins:
- serverless-plugin-typescript
上述 2 个更改应该可以解决该错误