Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding integration response to AWS websocket API with CloudFormation

AWS recently released Cloudformation support for templating websocket API gateways. I have a working example deployed, but I can't work out how to turn on the proxy integration response (see screenshot for how this is done in the console). Does anyone know what cloudFormation setting can be used to turn on a default integration response for lambda proxy integrations? enter image description here

like image 944
Matthew de Nobrega Avatar asked Feb 12 '19 06:02

Matthew de Nobrega


People also ask

How do you integrate WebSocket API?

Set up a WebSocket API integration request using the API Gateway console. Sign in to the API Gateway console, choose the API, and choose Routes. Under Routes, choose the route. In the Route Overview pane, choose Integration Request.

What is integration response in API gateway?

An integration response is an HTTP response encapsulating the backend response. For an HTTP endpoint, the backend response is an HTTP response. The integration response status code can take the backend-returned status code, and the integration response body is the backend-returned payload.

What is the difference between API gateway V1 and V2?

The API V1 namespace represents REST APIs and API V2 represents WebSocket APIs and the new HTTP APIs. You can create an HTTP API by using the AWS Management Console, CLI, APIs, CloudFormation, SDKs, or the Serverless Application Model (SAM).

Does AWS support Websockets?

While AppSync is a fully managed GraphQL API service, it is possible to implement a simple WebSocket API in the service with little or no GraphQL knowledge in minutes thanks to useful tools such as the AWS CDK and AWS Amplify libraries.


1 Answers

Please try below steps

1- Add RouteResponseSelectionExpression in Route as $default(this is only one supported as of now)

2- Create RouteResponse for all routes(bidirectional) Note:- RouteResponseKey: $default // it should be default only

3- Add ConnectIntegResponse(Optional)

Below is a tested CFN snippet, feel free to use it

##########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
like image 90
ifti Avatar answered Oct 22 '22 04:10

ifti