【问题标题】:Adding integration response to AWS websocket API with CloudFormation使用 CloudFormation 向 AWS websocket API 添加集成响应
【发布时间】:2019-07-05 17:29:22
【问题描述】:

AWS recently released Cloudformation support for templating websocket API gateways。我部署了一个工作示例,但我不知道如何打开代理集成响应(有关如何在控制台中完成此操作,请参见屏幕截图)。有谁知道可以使用什么 cloudFormation 设置来打开 lambda 代理集成的默认集成响应?

【问题讨论】:

    标签: amazon-web-services websocket amazon-cloudformation aws-api-gateway


    【解决方案1】:

    请尝试以下步骤

    1- 在 Route 中添加 RouteResponseSelectionExpression 作为 $default(目前仅支持一个)

    2- 为所有路由创建 RouteResponse(双向) 注意:- RouteResponseKey: $default // 它应该只是默认的

    3- 添加 ConnectIntegResponse(可选)

    下面是一个经过测试的CFN sn-p,请随意使用

    ##########Socket API###############
      webSocket:
        Type: AWS::ApiGatewayV2::Api
        Properties:
          Name: WebSocket
          ProtocolType: WEBSOCKET
          RouteSelectionExpression: "$request.body.action"
      ConnectRoute:
        Type: AWS::ApiGatewayV2::Route
        Properties:
          ApiId: !Ref webSocket
          RouteKey: $connect
          AuthorizationType: NONE
          OperationName: ConnectRoute
          RouteResponseSelectionExpression: $default # add this 
          Target: !Join
            - '/'
            - - 'integrations'
              - !Ref ConnectInteg
      ConnectInteg:
        Type: AWS::ApiGatewayV2::Integration
        Properties:
          ApiId: !Ref webSocket
          Description: Connect Integration
          IntegrationType: AWS_PROXY
          IntegrationUri: 
            Fn::Sub:
                arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${restAndSocketLambda.Arn}/invocations
    
      ConnectRouteResponse: # Add this
        Type: 'AWS::ApiGatewayV2::RouteResponse'
        Properties:
          RouteId: !Ref ConnectRoute
          ApiId: !Ref webSocket
          RouteResponseKey: $default
    
      ConnectIntegResponse: # Add this(if required)
        Type: 'AWS::ApiGatewayV2::IntegrationResponse'
        Properties:
          IntegrationId: !Ref ConnectInteg
          IntegrationResponseKey: /201/
          ApiId: !Ref webSocket

    【讨论】:

    • 感谢您的回答 1000 次。我今天一整天都不知道“RouteResponseSelectionExpression”。
    • 将其添加到sendmessage 路由时我可以看到响应,但是将其添加到$connect 时我看不到任何响应返回到websocket 客户端。此外,IntegrationResponse 似乎不需要,至少对于 HTTP_PROXY 集成类型
    猜你喜欢
    • 2021-11-28
    • 1970-01-01
    • 1970-01-01
    • 2018-06-09
    • 1970-01-01
    • 2019-12-01
    • 2018-09-28
    • 2019-10-25
    • 2017-10-01
    相关资源
    最近更新 更多