question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Porting to aws-cdk ( in python ).

See original GitHub issue

I have been attempting to port this chat-app to a aws-cdk application, and feel like i’m close, but not quite there yet.

This is the response i’m getting `[ec2-user@ip-172-31-x-x websocket]$ wscat -c wss://xxxxxxx.execute-api.ap-southeast-2.amazonaws.com/prod Connected (press CTRL+C to quit)

{“action”:“sendmessage”,“data”:“hello world”} < {“message”: “Internal server error”, “connectionId”:“VNRUSeldSwMCIRQ=”, “requestId”:“VNRXVFJOywMF3-Q=”}`

I know that the message is arriving at the lambda associated with the sendmessage lambda. I’m able to to print it and see it in the lambda logs. I’m getting a client error when it trys to post_connection.

ERROR] EndpointConnectionError: Could not connect to the endpoint URL: "https://execute-api.ap-southeast-2.amazonaws.com/@connections/VNmugd9KSwMCFwQ%3D"

I have attempted to reduce my lambda down to a simple as seems sensible. Just to try and diganose the issue, i also gave the lambda function full administrative permissions, but that made no difference either.

Heres the labmda code.

import boto3
import os
from botocore.exceptions import ClientError
import json
def lambda_handler(event, context):
    message = 'one two three'.encode('utf-8')
    body = event['body']
    print(body)
    connectionId = event['requestContext']['connectionId']
    print(connectionId)
    api_client = boto3.client('apigatewaymanagementapi')
    api_client.post_to_connection(
            Data = message,
            ConnectionId = connectionId
    )
    return {    
            'statusCode': 200, 
            'body': 'Message Sent' 
    }

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:6

github_iconTop GitHub Comments

2reactions
tuanardouincommented, Nov 1, 2020

I’m also trying to use websocket with CDK. Your code helped me a lot !

I created this repository if you want :

https://github.com/tuanardouin/WebSocket-CDK

1reaction
mrpacketheadcommented, Oct 31, 2020

There is a bug / error in the Boto3 documentation!!! The boto3 client docs dont’ say how to specifiy the url! I foudn this issue in the boto github issues, its been an issue for nearly 2 years with multiple people hitting the same issue! Minor change to the lambda…

My problem is resolved.

import boto3
import os
from botocore.exceptions import ClientError
import json


def lambda_handler(event, context):

    message = 'one two three'
    print(event)
    
    connectionId = event['requestContext']['connectionId']
    print(connectionId)
    
    url = 'https://' + event['requestContext']['domainName'] + '/prod'
    
    api_client = boto3.client('apigatewaymanagementapi', endpoint_url=url)
    api_client.post_to_connection(
            Data = message,
            ConnectionId = connectionId
    )

    return {    
            'statusCode': 200, 
            'body': 'Message Sent' 
    }```

Read more comments on GitHub >

github_iconTop Results From Across the Web

Working with the AWS CDK in Python
To work with the AWS CDK, you must have an AWS account and credentials and have installed Node.js and the AWS CDK Toolkit....
Read more >
How to add from and to port in aws cdk? - Stack Overflow
I added below code and started working for me. mws_vpc_sg.add_ingress_rule(peer= ec2.Peer.ipv4('172.30.0.0/15'), connection = ec2.
Read more >
How to use VPC Peering in AWS CDK construct with Python ...
To complete this test we need to create 3 VPC Networks, 1 with 2 subnets (Public & Private with NAT), and 2 as...
Read more >
aws-cdk-lib · PyPI
When migrating a CloudFormation stack to the AWS CDK, it can be useful to include fragments of an existing template verbatim in the...
Read more >
AWS CDK with Python — ALB + Fargate + Flask
I choose port 80 so it works with default settings, we can modify this from container task definition to run on a different...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found