Skip to content

(@aws-cdk/aws-apigatewayv2-alpha): Cannot get API Gateway to trigger connect or disconnect #19532

@brucegl

Description

@brucegl

What is the problem?

Not sure if the problem is with my code, the documentation or the implementation. After deploying the stack, the AWS console correctly displays an API Gateway trigger at the page Lambda>Functions>connect>Configuration>Triggers, however, the following is also displayed:

API Gateway:
arn:aws:execute-api:eu-west-1:1234567890:7sfj6g3ekl//$connect
x The API with ID 7sfj453ekl doesn’t include a route with path /* having an integration arn:aws:lambda:eu-west-1:1234567890:function:process_connect_requests.

If I'm doing something wrong, it's probably in the following lines:

    const webSocketConnectIntegration = new WebSocketLambdaIntegration("ConnectIntegration", connectFunction);
    const webSocketDisconnectIntegration = new WebSocketLambdaIntegration("DisconnectIntegration", disconnectFunction);

    const webSocketApi = new apigwv2.WebSocketApi(this, "WebSocket API", {
      apiName: "webSocket",
      routeSelectionExpression: "$request.body.action",
      connectRouteOptions: { integration: webSocketConnectIntegration },
      disconnectRouteOptions: { integration: webSocketDisconnectIntegration },
    });

Reproduction Steps

import { Duration, RemovalPolicy, Stack, StackProps } from "aws-cdk-lib";
import { Construct } from "constructs";
import * as dynamodb from "aws-cdk-lib/aws-dynamodb";
import * as lambda from "aws-cdk-lib/aws-lambda";
import * as path from "path";
import * as apigwv2 from "@aws-cdk/aws-apigatewayv2-alpha";
import { WebSocketLambdaIntegration } from "@aws-cdk/aws-apigatewayv2-integrations-alpha";

export class ApigwStack extends Stack {
  constructor(scope: Construct, id: string, props?: StackProps) {
    super(scope, id, props);

    const webSocketTableName = "WebSocketConnections";

    const connectFunction = new lambda.Function(this, "Connect Function", {
      functionName: "process_connect_requests",
      runtime: lambda.Runtime.NODEJS_14_X,
      handler: "index.handler",
      code: lambda.Code.fromAsset(path.join(__dirname, "../functions/connect")),
      timeout: Duration.seconds(5),
      environment: {
        TABLE_NAME: webSocketTableName,
      },
    });

    const disconnectFunction = new lambda.Function(this, "Disconnect Function", {
      functionName: "process_disconnect_requests",
      runtime: lambda.Runtime.NODEJS_14_X,
      handler: "index.handler",
      code: lambda.Code.fromAsset(path.join(__dirname, "../functions/disconnect")),
      timeout: Duration.seconds(5),
      environment: {
        TABLE_NAME: webSocketTableName,
      },
    });

    const webSocketLogTable = new dynamodb.Table(this, "WebSocket Log Table", {
      tableName: webSocketTableName,
      partitionKey: { name: "ConnectionId", type: dynamodb.AttributeType.STRING },
      removalPolicy: RemovalPolicy.DESTROY, // not recommended for production
    });
    webSocketLogTable.grantReadWriteData(connectFunction);
    webSocketLogTable.grantReadWriteData(disconnectFunction);

    const webSocketConnectIntegration = new WebSocketLambdaIntegration("ConnectIntegration", connectFunction);
    const webSocketDisconnectIntegration = new WebSocketLambdaIntegration("DisconnectIntegration", disconnectFunction);

    const webSocketApi = new apigwv2.WebSocketApi(this, "WebSocket API", {
      apiName: "webSocket",
      routeSelectionExpression: "$request.body.action",
      connectRouteOptions: { integration: webSocketConnectIntegration },
      disconnectRouteOptions: { integration: webSocketDisconnectIntegration },
    });

    const webSocketStage = new apigwv2.WebSocketStage(this, "Production Stage", {
      webSocketApi: webSocketApi,
      stageName: "prod",
      autoDeploy: true,
    });
  }
}

Please use the typical example connect/disconnect code

What did you expect to happen?

Something like the following to be displayed in Lambda>Functions>connect>Configuration>Triggers

API Gateway: WSM
arn:aws:execute-api:eu-west-1:1234567890:cogh633tqf/*/$connect
API endpoint: https://cogh633tqf.execute-api.eu-west-1.amazonaws.com/prod/
Details
API type: WEBSOCKET
Authorization: NONE
Method: $connect
Stage: prod

What actually happened?

wscat cannot connect

CDK CLI Version

2.17.0 (build f9cd009)

Framework Version

No response

Node.js Version

v16.13.0node.js

OS

Linux Mint

Language

Typescript

Language Version

No response

Other information

No response

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions