Martin Šťáva
Martin Šťáva

Reputation: 357

Adding integration response to AWS websocket API with @aws-cdk/aws-apigatewayv2

Is there a way to add an integration response to the AWS WebSocket API using AWS CDK with the aws-apigatewayv2 package? This answer shows a great way to achieve just that using CloudFormation. But I haven't been able to translate it into the AWS CDK. Thanks!

EDIT:

Sorry, I should have clarified how I am trying to add the integration response now:

  const webSocketApi = new WebSocketApi(this, 'Api', {
    defaultRouteOptions: {
      integration: new LambdaWebSocketIntegration({ handler: lambdaFn }),
    },
  })
  new CfnIntegrationResponse(this, 'response', {
    apiId: webSocketApi.apiId,
    integrationId: /* ? */,
    integrationResponseKey: '$default',
  })
  const stage = new WebSocketStage(this, 'Stage', {
    webSocketApi,
    stageName: 'dev',
    autoDeploy: true,
  })

I could add the integration response using CfnIntegrationResponse but I don't have a way to access the integration id of the LambdaWebSocketIntegration.

Upvotes: 8

Views: 1310

Answers (2)

yeahwhat
yeahwhat

Reputation: 855

The solution is to use CfnRouteResponse instead of CfnIntegrationResponse, like this:

const api = new WebSocketApi(...)
const route = api.addRoute(...)

new apigateway.CfnRouteResponse(this, "wsRouteResponse", {
    apiId: api.apiId,
    routeId: route.routeId,
    routeResponseKey: "$default",
});

Upvotes: 1

Stoyan Georgiev
Stoyan Georgiev

Reputation: 415

There's still no way to rewrite this CloudFormation example in cdk.

##########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

I tried using escape hatches, but I did not manage to add valid resources references to the synthesized Cloudformation template.

I suggest to include the entire template fragment in a stack using the CfnInclude construct.

new CfnInclude(this, 'ID', {
  template: {
    Resources: {
      Bucket: {
        Type: 'AWS::S3::Bucket',
        Properties: {
          BucketName: 'my-shiny-bucket'
        }
      }
    }
  },
});

Upvotes: 0

Related Questions