1

Reading https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-websocket-api.html I am confused. Where exactly the WSS is being hosted and where is the processing happening?

In API Gateway you can create a WebSocket API as a stateful frontend for an AWS service (such as Lambda or DynamoDB) or for an HTTP endpoint. The WebSocket API invokes your backend based on the content of the messages it receives from client apps.

So if I use Chalice to deploy my API with websocket support, is it automatically running processing on Lambda? Or do I have to write that manually in boto (ex. invokeFunction) or can I point it to a lambda function?

The maximum lambda function runtime is 15 minutes.

The maximum websocket timeout is 2 hours and 10 minutes of idle.

For example, what happens to the run_some_long_function() that exceeds 15 minutes? If the Lambda function exceeds 15 minutes, does this just disconnect?

from boto3.session import Session
from chalice import Chalice

app = Chalice(app_name='ws-service')
app.experimental_feature_flags.update(['WEBSOCKETS'])
app.websocket_api.session = Session()

app.debug = True


@app.on_ws_connect()
def ws_connect(event):
    app.log.debug(f"New connection established: {event.connection_id}")
    response = run_some_long_process()
    app.websocket_api.send(event.connection_id, response)

I'm also confused as to where the function runs, does it run on Lambda?

Going deeper, looking at the code here on the chalice tutorial, where are the websocket connections being hosted on? Is wss://{id}.execute-api.{region}.amazonaws.com/api/ running on lambda?

  • 2
    lambda is ephemeral, not expected to run for a long time, so the timeout isn't really an issue here, it should respond quickly to events. In the case of wss, those events include a connectionId that you can use to track the state of each connection (in a database like Dynamo, for example). The API Gateway keeps the connection alive. If you need to run some very long process in response to a wss request, you won't be able to do it in Lambda. – Anon Coward Mar 18 '22 at 06:15
  • thanks @AnonCoward, what is the normal procedure to handle such long processes on AWS if its too long for Lambda? Spin up a Lightsail server or ec2? I guess startup time is less important here since the user expects to wait a while until it returns something. – Saroshi Kir Mar 19 '22 at 03:05
  • Too many possible variables to suggest the "normal procedure". Using an ASG with EC2 is one option, ECS is another. Depends on what exactly is being done. – Anon Coward Mar 19 '22 at 03:08

0 Answers0