我正在尝试使用无服务器框架来创建Lambda,当客户端连接到Websocket API网关时会调用该Lambda。 AWS CloudFormation将创建已定义的Lambda函数,但未创建websocket API网关。
尝试编写自己的文件(不起作用)后,我求助于将在无服务器文档中找到的示例复制并粘贴到一个新创建的无服务器文件夹中,以查看它是否可以工作-没用,我找不到其他人遇到类似的问题。
到目前为止,我已经尝试了此处记录的简单和扩展方法(这是示例代码所基于的方法): 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]
如果有人可以阐明这一点,因为我可能完全缺少明显的东西,我将不胜感激。
答案 0 :(得分:1)
您的事件数组格式错误,并且route
属性不在正确的位置(添加另一个缩进)。
当您将样本转换为JSON时,它看起来像这样:
{
"service": "temp",
"provider": {
"name": "aws",
"runtime": "nodejs8.10",
"region": "eu-west-2"
},
"functions": {
"default": {
"handler": "handler.connect",
"events": [
{
"websocket": null,
"route": "$default"
}
]
}
}
}
固定有其他缩进,如下所示:
functions:
default:
handler: handler.connect
events:
- websocket:
route: $default
现在在JSON中看起来像这样:
{
"service": "temp",
"provider": {
"name": "aws",
"runtime": "nodejs8.10",
"region": "eu-west-2"
},
"functions": {
"default": {
"handler": "handler.connect",
"events": [
{
"websocket": {
"route": "$default"
}
}
]
}
}
}
答案 1 :(得分:1)
仔细阅读了与无服务器相关的所有内容之后,我意识到我的无服务器版本不是最新版本(不要问我是怎么回事,我昨天运行yarn add serverless
来获得其特定于项目的版本)< / p>
相反,该版本为1.35,因此不支持API Gateway Websocket。也许我之前已经在全球安装了它,却忽略了将其从我的全局npm软件包中删除...
无法默默地识别标签的事实并没有帮助调试过程,我可能-当我有机会时-通过在Serverless.yml文件上添加验证运行来为项目做出贡献,因此在不支持的选项中标记了控制台。
实际上,运行npm install -g serverless@latest
解决了该问题,现在API网关已正确部署。
谢谢Alex和Hugo的回应!
答案 2 :(得分:0)
我遇到了完全相同的问题,对我来说,解决方法是像这样正确缩进route
:
functions:
default:
handler: handler.connect
events:
- websocket:
route: $default
所以基本上要改变:
- websocket:
route: $default
收件人:
- websocket:
route: $default