Amazon API Gateway WebSocket API to Amazon SQS to AWS Lambda

API Gateway WebSocket API → SQS → AWS Lambda

Create an API Gateway WebSocket API that sends events to SQS and then to Lambda

The API Gateway handles websocket message events by putting the message into a SQS FIFO (First-In-First-Out) queue.
The SQS FIFO queue is used to ensure processing order of the websocket messages.
The message payload must contain a MessageGroupId field which is used by SQS FIFO queue.
If message order is not required, a standard SQS queue can also be used and the message payload would not require the MessageGroupId field.
The API Gateway websocket request integration passes the connectionId and requestId as metadata and the message body as the payload.
The Lambda function can now process the SQS FIFO requests in a batch manner (10 requests at a time) and send a response back to the websocket connection.

< Back to all patterns

GitHub icon Download this pattern (.zip)

GitHub icon View this pattern on GitHub


Clone repo

git clone https://github.com/aws-samples/serverless-patterns/cd serverless-patterns/apigw-websocket-api-sqs-lambda-terraform

Deploy

terraform initterraform apply


Testing

See the repo for detailed testing instructions.

Cleanup

1. Delete the stack: terraform destroy.
2. Confirm the stack has been deleted: terraform show

Created by:

Carlos Bauer

I am Technical Account Manager at AWS