Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
brucegl opened this issue Mar 23, 2022 · 14 comments · Fixed by #27732
Closed
Assignees
Labels
@aws-cdk/aws-apigatewayv2 Related to Amazon API Gateway v2 bug This issue is a bug. p1

Comments

@brucegl
Copy link

brucegl commented Mar 23, 2022

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

@brucegl brucegl added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Mar 23, 2022
@github-actions github-actions bot added the @aws-cdk/aws-apigatewayv2 Related to Amazon API Gateway v2 label Mar 23, 2022
@brucegl
Copy link
Author

brucegl commented Mar 23, 2022

Typescript version is 4.5.5

@ryparker ryparker added guidance Question that needs advice or information. and removed bug This issue is a bug. labels Mar 23, 2022
@ryparker ryparker assigned ryparker and unassigned otaviomacedo Mar 23, 2022
@brucegl
Copy link
Author

brucegl commented Mar 24, 2022

A bit of an update... if I remove the (dis)connectRouteOptions, I can add these routes using the AWS console as a workaround.

ryparker added a commit to ryparker/aws-cdk-sample-apigatewayv2-alpha-trigger that referenced this issue Mar 25, 2022
@ryparker
Copy link
Contributor

Hey @brucegl 👋🏻

Thanks for opening the issue with us. I was able to reproduce this and i've uploaded the full code with documentation to this repo.

This might be a bug in the AWS console. Have you noticed any functional problems that might be caused be related to this?

@ryparker ryparker added p1 bug This issue is a bug. and removed needs-triage This issue or PR still needs to be triaged. guidance Question that needs advice or information. labels Mar 25, 2022
@ryparker
Copy link
Contributor

Here's a screenshot of the error:

error-screenshot

@ryparker ryparker added the needs-triage This issue or PR still needs to be triaged. label Mar 25, 2022
@brucegl
Copy link
Author

brucegl commented Mar 26, 2022

Hi @ryparker, I think the issue goes beyond the AWS console as I was also unable to wscat -c to the endpoint.

@rix0rrr rix0rrr added p2 p1 and removed p1 p2 labels Mar 30, 2022
@peterwoodworth peterwoodworth removed the needs-triage This issue or PR still needs to be triaged. label Apr 19, 2022
@Spider-ant
Copy link

If you view (through console) the websocket api route integration request (see screenshot) you will see the lambda function is set to the lambda function name. If the websocket integration applied the function arn instead it would likely fix the issue. (see warning in the screenshot)
Screen Shot 2022-05-13 at 12 56 52 PM

@brucegl
Copy link
Author

brucegl commented Jun 10, 2022

Thanks @Spider-ant, I'll see if I can codify it with the lambda arn next time I build a stack

@otaviomacedo otaviomacedo removed their assignment Jun 27, 2022
@oyatrides
Copy link

any updates on this ? having the same error

@TanjaBayer
Copy link

TanjaBayer commented Apr 3, 2023

Same here, the generated template as the following permissions:

"websocketapitestwebsocketapiconnectRoutedefaultwebsocketroutePermission": {
   "Type": "AWS::Lambda::Permission",
   "Properties": {
    "Action": "lambda:InvokeFunction",
    "FunctionName": {
     "Fn::GetAtt": [
      "websocketapiMyLambda",
      "Arn"
     ]
    },
    "Principal": "apigateway.amazonaws.com",
    "SourceArn": {
     "Fn::Join": [
      "",
      [
       "arn:",
       {
        "Ref": "AWS::Partition"
       },
       ":execute-api:us-east-2:*********:",
       {
        "Ref": "websocketapitestwebsocketapi"
       },
       "/*/*$connect"
      ]
     ]
    }
   }

I think one of the /* before the $connect should not be there, this makes the aws-apigateway unusable.

I ended up using the Cloudformation contruct instead of the higher level L2 Construct here are the required steps for it: #2872 (comment)

@astroalek
Copy link

Same problem here with version 2.86

@katzrkool
Copy link

Same problem in CDK v1.

@ashishpandey001
Copy link

this issue has been open for 16 months, are there no plans to resolve this and make the L2 construct usable?

If not, can we mark the construct as broken so people don't waste their time using it over the L1 constructs?

@WillDelish
Copy link

I am also experiencing this issue and have wasted a bit of time troubleshooting my lambda when it never triggers to begin with due to this bug. Will switch to a different construct, but its unfortunate this has been open for so long without marking the construct as broken

@sumupitchayan sumupitchayan self-assigned this Oct 24, 2023
@mergify mergify bot closed this as completed in #27732 Nov 1, 2023
mergify bot pushed a commit that referenced this issue Nov 1, 2023
… working (#27732)

Closes #19532 

Lambda integrations for `$connect` and `$disconnect` routes were previously broken. Users would see this error message in the Lambda console:

![160185676-15ba5704-a7ba-49ef-b457-bb3f89094de6](https://github.com/aws/aws-cdk/assets/35242245/0938239a-b4f1-440d-9868-86ce3d213386)

Fixing the path by removing the extra `*/` from the [code of the Lambda integration](https://github.com/aws/aws-cdk/blob/main/packages/%40aws-cdk/aws-apigatewayv2-integrations-alpha/lib/websocket/lambda.ts#L36) that adds permission, we no longer see that error message in the Lambda console as the path is now correct:

<img width="973" alt="Screenshot 2023-10-27 at 2 58 11 PM" src="https://github.com/aws/aws-cdk/assets/35242245/c5a8f1f6-4e51-4533-880e-3c5f400cc59e">

I could not manage to figure out how to verify this change via an Integration test assertion. However, I added a new integration test file for Lambda `$connect` and `$disconnect` integrations on a `WebSocketApi`. The attached screenshots above also verify that this change works when I manually run it locally.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
Copy link

github-actions bot commented Nov 1, 2023

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-apigatewayv2 Related to Amazon API Gateway v2 bug This issue is a bug. p1
Projects
None yet
Development

Successfully merging a pull request may close this issue.