你好:)希望能找到你。
我正在尝试通过 serverless-offline
节点包在本地设置测试我的无服务器环境。
这是我的 serverless.yml 文件:
org: etisreal11
app: leaderboards-api
service: leaderboards-api
provider:
name: aws
runtime: nodejs18.x
stage: STG
profile: local
region: eu-central-1
environment:
TABLE_NAME: ${self:custom.TABLE_NAME}
DYNAMODB_HOSTNAME: localhost
AWS_ACCESS_KEY: Local
AWS_SECRET_KEY: Local
logs:
httpApi: true
custom:
serverless-offline:
noPrependStageInUrl: true
resourceRoutes: true # HTTP Proxy
TABLE_NAME: ${self:provider.stage}_Leaderboards
plugins:
- serverless-dynamodb-local
- serverless-offline
- serverless-offline-lambda
package:
individually: true
functions:
adminHandler:
handler: adminHandler/leaderboardsAdminHandler.handler
environment:
TABLE_NAME: ${self:custom.TABLE_NAME}
DYNAMODB_HOSTNAME: ${self:provider.environment.DYNAMODB_HOSTNAME}
events:
- http:
path: /
method: ANY
- http:
path: '{proxy+}'
method: ANY
resources:
Resources:
STGLeaderboards:
Type: AWS::DynamoDB::Table
Properties:
TABLE_NAME: ${self:custom.TABLE_NAME}
AttributeDefinitions:
- AttributeName: PK
AttributeType: S
- AttributeName: SK
AttributeType: S
KeySchema:
- AttributeName: PK
KeyType: HASH
- AttributeName: SK
KeyType: HASH
BillingMode: PAY_PER_REQUEST
这是我引用的文件:
export const handler = async (event) => {
switch (event.routeKey) {
case 'POST /admin/leaderboard':
try {
const validatedResponse = returnCreateLeaderboardReq(JSON.parse(event.body))
// Invalid payload
if (validatedResponse?.statusCode === 422) return JSON.stringify(validatedResponse)
const createdLeadRes = await documentClient.send(new PutCommand(validatedResponse))
return JSON.stringify({
statusCode: 201,
body: { message: createdLeadRes }
})
} catch (error) {
...
这是树命令的输出:tree -L 2
.
├── adminHandler
│ └── leaderboardsAdminHandler.js
├── convert-to-base64.js
├── create-dynamodbtable.md
├── docker
│ └── dynamodb
├── docker-compose.yaml
├── index.js
├── leaderboardsStreamHandler
│ ├── deleteHandler.js
│ ├── deleteHandler.test.js
│ ├── insertHandler.js
│ ├── insertHandler.test.js
│ ├── leaderboardsStreamHandler.js
│ ├── leaderboardsStreamHandler.test.js
│ ├── utils.js
│ └── utils.test.js
├── mock-data
│ ├── encoded-lighter-create
│ ├── encoded-lighter-remove
│ ├── encoded-single-tournament
│ ├── participants.json
│ ├── single-tournament.json
│ ├── single-tournament.zip
│ ├── tournaments.json
│ ├── tournaments-lighter.json
│ ├── tournaments-lighter-remove.json
│ ├── tournaments-lighter-remove.zip
│ ├── tournaments-lighter.zip
│ ├── tournaments-light.json
│ ├── tournaments-light.zip
│ └── tournaments.zip
├── node_modules
|
├── offline
│ └── migrations
├── OLD-leaderboardsStreamHandler
│ ├── insertHandler.js
│ ├── leaderboardsStreamHandler.js
│ ├── modifyHandler.js
│ └── NOTES.md
├── package.json
├── package-lock.json
├── README.md
├── serverless.yml
└── vitest.config.ts
一切正常启动:
Starting Offline at stage STG (eu-central-1)
Offline Lambda Server listening on http://localhost:4000
Offline [http for lambda] listening on http://localhost:3002
Function names exposed for local invocation by aws-sdk:
* adminHandler: leaderboards-api-STG-adminHandler
┌────────────────────────────────────────────────────────────────────────────────┐
│ │
│ ANY | http://localhost:3000/ │
│ POST | http://localhost:3000/2015-03-31/functions/adminHandler/invocations │
│ ANY | http://localhost:3000/{proxy*} │
│ POST | http://localhost:3000/2015-03-31/functions/adminHandler/invocations │
│ │
└────────────────────────────────────────────────────────────────────────────────┘
Server ready: http://localhost:3000 🚀
但是,如果我尝试获取上述任何端点(或任何其他端点),我会收到此错误:
✖ Attempt to invoke function 'adminHandler' failed. Function does not exists.
我研究了这些问题:
"Serverless-offline: route not found." running an AWS Lambda function in offline mode
Serverless Offline: Error while running handler, cannot find module
但我花了太多时间还是没能解决这个问题。老实说,我不知道发生了什么事。我希望我能提供更多见解:/
如果您有任何小费建议,我们将不胜感激......
提前致谢
最佳答案
从 serverless.yml
文件中删除 serverless-offline-lambda
无服务器插件:
plugins:
- serverless-dynamodb-local
- serverless-offline
- serverless-offline-lambda
首先,您现在只需要 serverless-offline
插件即可在本地运行 lambda。
其次,serverless-offline-lambda
插件上次更新是在 4 年前。这些年来,serverless
和 serverless-offline
都发生了很大变化。
关于aws-lambda - 无服务器离线 - 尝试调用函数 'xxx' 失败。函数不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77002363/