Tom3652
Tom3652

Reputation: 2957

Websocket API not returning answers AWS

I have a very simple Websocket API hosted in AWS.
I am integrated this API with a Lambda function whose code is :

import json


def lambda_handler(event, context):
    print(event)
    return {
        "isBase64Encoded": False,
        "statusCode": 200,
        "headers": {"Content-Type": "application/json"},
        "body": json.dumps(event)
    }

I have also added integration response for a two-way communication as stated in this documentation

Below is my test using wscat with the command : wscat -c myapiendpoint

result

As you can see, i am connected successfully and if i write an existing route from the request.body.action selection (sendMessage), there is no error but there is no output. If i write a wrong route (test that i have not created in the API Gateway console), i have got an error message.

I have also tried with more simple outputs from the function, none is working. I have finally wrote the above code as stated in this documentation.

I simply want to be able to send back a custom message to the client (actually data from my database) when a route is called.
This is actually what i want also for the $connect and $disconnect routes.

Am i doing something wrong ?

EDIT :

Here is my API Gateway setup for the route :

enter image description here

Here is a screenshot about the permission of the Lambda. By the way, these are the permissions for all of my Lambdas (under HTTP APIs) that all work fine :

permissions

Upvotes: 2

Views: 2055

Answers (2)

nicover
nicover

Reputation: 2633

Sometimes and it’s probably your case, you have not deployed your changes to your WebSocket API. This happened to me when i was using automated stage deployment with HTTP APIs and not with WebSocket APIs.

Upvotes: 2

noninertialframe
noninertialframe

Reputation: 666

It seems like your Lambda function does not have permission to post message to API Gateway connections. Try giving your Lambda function permission for action execute-api:ManageConnections on the API Gateway resource.

Upvotes: 0

Related Questions