pythonamazon-web-serviceswebsocket

Is a websocket connection handled by one single lambda instance on AWS?


If I have a websocket connection:

client <=> AWS Lambda [Python]

While a connection is active: Will the client be connected to the same lambda instance for the whole time or can it change?


Solution

  • You don't have a persistent WebSocket connection to a Lambda function. Instead, you have a WebSocket connection to something persistent that proxies requests to the Lambda function, and that is typically API Gateway.

    No, the client won't be connected, even indirectly, to the same instance of a Lambda function for the entirety of the WebSocket connection. Lambda is stateless so WebSocket applications using Lambda typically store state e.g. client connection IDs in DynamoDB.

    There's a WebSocket-based example in the announcement blog that's helpful to understand how connection IDs are handled and how the back-end sends messages to all connected clients.