使用cloudformation在Api网关Websocket中创建模拟集成

使用cloudformation在Api网关Websocket中创建模拟集成,websocket,amazon-cloudformation,aws-api-gateway,Websocket,Amazon Cloudformation,Aws Api Gateway,AWS没有太多关于如何使用模拟集成类型的信息 我有一个AWS Api网关,它具有$connect、$disconnect和sendHTTP\u代理操作类型的路由。我想为keepAlive消息添加一条新路由,使websocket保持活动状态(否则,websocket将在大约15分钟后关闭) 我想我可以为这个新路由使用模拟集成类型,因为我实际上不需要在任何后端处理消息 IntegrationType的所有文档说明如下: MOCK:用于将路由或方法请求与API网关集成为 不调用任何后端的“环回”端点。

AWS没有太多关于如何使用模拟集成类型的信息

我有一个AWS Api网关,它具有
$connect
$disconnect
send
HTTP\u代理操作类型的路由。我想为
keepAlive
消息添加一条新路由,使websocket保持活动状态(否则,websocket将在大约15分钟后关闭)

我想我可以为这个新路由使用模拟集成类型,因为我实际上不需要在任何后端处理消息

IntegrationType
的所有文档说明如下:

MOCK:用于将路由或方法请求与API网关集成为 不调用任何后端的“环回”端点。仅支持 WebSocket API

到处都找不到例子。我在某个地方读到,可以使用
RequestTemplates
,但不知道如何使用。因此,问题是:我能得到任何关于如何配置路由
keepAlive
和模拟集成的示例吗?唯一的目的是保持我的Websocket API网关打开?

这是我尝试过的,但不起作用

wsKeepAliveRoute:
  Type: AWS::ApiGatewayV2::Route
  Properties:
    ApiId: !Ref wsApiGateway
    RouteKey: keepAlive
    AuthorizationType: NONE
    OperationName: KeepAliveRoute
    Target: !Join
      - /
      - - integrations
        - !Ref wsKeepAliveIntegration

wsKeepAliveIntegration:
  Type: AWS::ApiGatewayV2::Integration
  Properties:
    ApiId: !Ref wsApiGateway
    Description: Websocket $connect integration
    IntegrationType: MOCK
    RequestTemplates:
      application/json: "{\"statusCode\": 200}"
    PayloadFormatVersion: 1.0
我在websocket上得到以下错误

{"message": "Internal server error", "connectionId":"f1MgZfkODoECEYw=", "requestId":"f1Mj4GvojoEFaHw="}

有效的解决方法

下面的方法很好

wsKeepAliveRoute:
  Type: AWS::ApiGatewayV2::Route
  Properties:
    ApiId: !Ref wsApiGateway
    RouteKey: keepAlive
    AuthorizationType: NONE
    OperationName: KeepAliveRoute
    Target: !Join
      - /
      - - integrations
        - !Ref wsKeepAliveIntegration

wsKeepAliveIntegration:
  Type: AWS::ApiGatewayV2::Integration
  Properties:
    ApiId: !Ref wsApiGateway
    Description: Websocket keepAlive integration
    IntegrationType: MOCK
    # The following does not make any difference
    # RequestTemplates:
    #  application/json: "{\"statusCode\": 200, \"message\": \"all good\"}"
    PayloadFormatVersion: 1.0
尽管由于某些原因,websocket有时会失败并抱怨SSL证书与内部API网关url不匹配(奇怪…) 大多数情况下,这是有效的,
keepAlive
到达API网关

我希望获得回复,但无法为模拟集成添加IntegrationResponse。因此,就目前而言,这已经足够了

$ wscat -c wss://ws.mycompany.io
Connected (press CTRL+C to quit)
> {'action': 'keepAlive'}
>
如果有人知道一个更好的