无服务器框架未通过配置在 AWS 上部署 API 网关

Serverless framework is not deploying an API Gateway on AWS from configuration

我正在尝试使用无服务器框架创建一个 Lambda,当客户端连接到 websocket API 网关时会调用它。 AWS CloudFormation 正在创建已定义的 Lambda 函数,但未创建 websocket API 网关。

在尝试自己编写(没有用)之后,我求助于将我在无服务器文档上找到的示例复制并粘贴到一个新创建的无服务器文件夹中,只是为了看看它是否可以工作 - 它没有' t,而且我找不到其他人似乎遇到过类似的问题。


到目前为止,我已经尝试了此处记录的简单方法和扩展方法(这是示例代码的基础): https://serverless.com/framework/docs/providers/aws/events/websocket/

我也尝试关注此博客,这也导致创建了 Lambda,但没有创建 API 网关。 https://serverless.com/blog/api-gateway-websockets-example/

这是我的 serverless.yml 文件。它按照我的预期进行部署,除了 API 网关:

service: temp
provider:
  name: aws
  runtime: nodejs8.10
  region: eu-west-2

functions:
  default:
    handler: handler.connect
    events:
      - websocket: 
        route: $default

这是无服务器部署 -v 输出:

$ serverless deploy -v
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Creating Stack...
Serverless: Checking Stack create progress...
CloudFormation - CREATE_IN_PROGRESS - AWS::CloudFormation::Stack - temp-dev
CloudFormation - CREATE_IN_PROGRESS - AWS::S3::Bucket - ServerlessDeploymentBucket
CloudFormation - CREATE_IN_PROGRESS - AWS::S3::Bucket - ServerlessDeploymentBucket
CloudFormation - CREATE_COMPLETE - AWS::S3::Bucket - ServerlessDeploymentBucket
CloudFormation - CREATE_COMPLETE - AWS::CloudFormation::Stack - temp-dev
Serverless: Stack create finished...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service .zip file to S3 (386 B)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
CloudFormation - UPDATE_IN_PROGRESS - AWS::CloudFormation::Stack - temp-dev
CloudFormation - CREATE_IN_PROGRESS - AWS::Logs::LogGroup - DefaultLogGroup
CloudFormation - CREATE_IN_PROGRESS - AWS::IAM::Role - IamRoleLambdaExecution
CloudFormation - CREATE_IN_PROGRESS - AWS::Logs::LogGroup - DefaultLogGroup
CloudFormation - CREATE_COMPLETE - AWS::Logs::LogGroup - DefaultLogGroup
CloudFormation - CREATE_IN_PROGRESS - AWS::IAM::Role - IamRoleLambdaExecution
CloudFormation - CREATE_COMPLETE - AWS::IAM::Role - IamRoleLambdaExecution
CloudFormation - CREATE_IN_PROGRESS - AWS::Lambda::Function - DefaultLambdaFunction
CloudFormation - CREATE_IN_PROGRESS - AWS::Lambda::Function - DefaultLambdaFunction
CloudFormation - CREATE_COMPLETE - AWS::Lambda::Function - DefaultLambdaFunction
CloudFormation - CREATE_IN_PROGRESS - AWS::Lambda::Version - DefaultLambdaVersionY0DDREbM8apFqgW7p0WqFe2SjYB4Wt7O63fYPiljU
CloudFormation - CREATE_IN_PROGRESS - AWS::Lambda::Version - DefaultLambdaVersionY0DDREbM8apFqgW7p0WqFe2SjYB4Wt7O63fYPiljU
CloudFormation - CREATE_COMPLETE - AWS::Lambda::Version - DefaultLambdaVersionY0DDREbM8apFqgW7p0WqFe2SjYB4Wt7O63fYPiljU
CloudFormation - UPDATE_COMPLETE_CLEANUP_IN_PROGRESS - AWS::CloudFormation::Stack - temp-dev
CloudFormation - UPDATE_COMPLETE - AWS::CloudFormation::Stack - temp-dev
Serverless: Stack update finished...
Service Information
service: temp
stage: dev
region: eu-west-2
stack: temp-dev
api keys:
  None
endpoints:
  None
functions:
  default: temp-dev-default
layers:
  None

Stack Outputs
DefaultLambdaFunctionQualifiedArn: arn:aws:lambda:eu-west-2:[redacted]:function:temp-dev-default:3
ServerlessDeploymentBucketName: temp-dev-serverlessdeploymentbucket-[redacted]

如果有人能对此有所说明,因为我可能完全遗漏了一些明显的东西,我将不胜感激。

从字面上看一遍与 Serverless 相关的所有内容后,我意识到我的 Serverless 版本不是最新版本(不要问我这是怎么发生的,我 运行 yarn add serverless 得到一个特定的项目昨天的版本)

相反,版本为 1.35,因此不支持 API 网关 websockets。也许我之前已经全局安装了它而忽略了将它从我的全局 npm 包中删除...

它无法默默地识别标签这一事实对调试过程没有帮助,我可能 - 当我有机会时 - 通过在 Serverless.yml 上添加验证 运行 来为项目做出贡献文件,以便在控制台中标记不支持的选项。

事实上,运行宁 npm install -g serverless@latest 解决了问题,现在 API 网关已正确部署。

感谢 Alex 和 Hugo 的回复!

我遇到了完全相同的问题,对我来说,解决方案是像这样正确缩进 route

functions:
  default:
    handler: handler.connect
    events:
      - websocket: 
          route: $default

所以基本上改变:

- websocket: 
  route: $default

至:

- websocket: 
    route: $default