AWS Builder Center
Building a Real-Time Chat App with AWS WebSockets, Lambda, and DynamoDB

Building a Real-Time Chat App with AWS WebSockets, Lambda, and DynamoDB

A step-by-step walkthrough of building a serverless real-time chat application with room management using API Gateway WebSockets, Lambda, DynamoDB, Cognito, and S3.

AWS Community Builder - AI Engineering | Junior Engineer @ MulticoreWare

What I Built

A fully serverless real-time chat application where users can sign up, create chat rooms, and invite others using a shareable Room ID - all without managing any servers.

AWS Services Used

  • Amazon Cognito - user sign-up, sign-in, and email verification
  • API Gateway (WebSocket) - real-time bidirectional messaging
  • API Gateway (HTTP API) - room create/join REST endpoint
  • AWS Lambda - all backend logic (connect, disconnect, send message, manage rooms)
  • Amazon DynamoDB - stores active connections and room metadata
  • Amazon S3 - hosts the static frontend

Architecture

When a user connects, their  connectionId ,  username , and  roomId  are saved in DynamoDB. When they send a message, Lambda queries only connections in the same room and broadcasts to them. When the last user leaves a room, it is automatically deleted from DynamoDB.


Key Challenges

CORS was the first real blocker - API Gateway and the Lambda response both need the  Access-Control-Allow-Origin  header. Missing it on either side breaks the preflight request silently.
Room scoping required adding a  roomId  attribute to the connections table and filtering every broadcast by it, so messages never leak across rooms.
Auto-deletion was solved by having  chat-disconnect  call the room manager Lambda after removing a connection - if no connections remain for that room, the room is deleted.

What I Learned

You can build a production-like chat system entirely on AWS serverless with zero EC2 instances. The WebSocket API in API Gateway handles persistent connections cleanly, and DynamoDB's flexibility made it easy to evolve the schema without migrations.
Try it out :  Link 
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article