πŸ’‘
Hey β€” It's Sandip Das πŸ‘‹

This project teaches how to design real-time, event-driven architectures without managing servers, giving hands-on experience with WebSocket connections, Lambda workflows, and DynamoDB patterns.

It also builds strong skills in scaling, monitoring, and optimizing modern serverless systems that power chat, notifications, IoT, and live collaboration apps.

⚑ Quick Reference

Keep the momentum going β€” everything you need on LearnXOps in one place:


πŸ‘£
60 Days of DevOps (Complete)
30 Days of MLOps (Complete)
90 Days of Cloud (in progress)

🧠 Learn here

About this project:

A real-time serverless chat application built with AWS API Gateway WebSocket APIs, Lambda, DynamoDB, and S3.

Features

  • User signup and login
  • Real-time chat messaging
  • WebSocket-based communication
  • Serverless architecture for scalability
  • Complete infrastructure as code


Project Architecture

This serverless chat application uses AWS services to provide a scalable, real-time messaging platform.

Serverless chat architecture: S3 website, API Gateway REST API with auth Lambdas and the Users table, WebSocket API with four Lambda routes, Connections and Messages tables, and a DynamoDB Streams processor pushing messages back to every open connection

Components

Frontend (React)

  • Location: S3 Static Website
  • Features:
    • User authentication (signup/login)
    • Real-time chat interface
    • WebSocket connection management
    • Message history display
  • Technologies: React 18, WebSocket API

Authentication API (REST)

  • Service: API Gateway REST API
  • Endpoints:
    • POST /signup - User registration
    • POST /login - User authentication
  • Backend: Lambda functions with bcrypt for password hashing
  • Token: JWT for session management

WebSocket API

  • Service: API Gateway WebSocket API
  • Routes:
    • $connect - Authenticate and store connection
    • $disconnect - Clean up connection
    • sendMessage - Store message in DynamoDB
    • getMessages - Retrieve message history
  • Authentication: JWT token via query parameter

Database (DynamoDB)

Users Table

  • Primary Key: userId
  • GSI: EmailIndex (email)
  • Attributes: userId, email, username, password (hashed), createdAt, updatedAt

Messages Table

  • Primary Key: messageId, timestamp
  • GSI: RoomIndex (roomId, timestamp)
  • Attributes: messageId, roomId, userId, username, message, timestamp
  • Streams: Enabled (NEW_IMAGE)

Connections Table

  • Primary Key: connectionId
  • GSI: UserIndex (userId)
  • Attributes: connectionId, userId, username, email, connectedAt, ttl
  • TTL: Enabled (24 hours)

Stream Processor (Lambda)

  • Trigger: DynamoDB Streams on Messages table
  • Function: Broadcasts new messages to all active WebSocket connections
  • Error Handling: Removes stale connections (410 status)

Data Flow

User Registration

  1. User submits signup form
  2. Frontend sends POST to /signup
  3. Lambda validates input
  4. Lambda checks for existing user via EmailIndex
  5. Lambda hashes password with bcrypt
  6. Lambda stores user in DynamoDB
  7. Success response returned

User Login

  1. User submits login form
  2. Frontend sends POST to /login
  3. Lambda queries user by email via EmailIndex
  4. Lambda verifies password with bcrypt
  5. Lambda generates JWT token
  6. Token and user data returned
  7. Frontend stores token in localStorage

WebSocket Connection

  1. Frontend connects to WebSocket URL with JWT token
  2. API Gateway triggers $connect Lambda
  3. Lambda verifies JWT token
  4. Lambda stores connection in Connections table
  5. Connection established
  6. Frontend requests message history via getMessages

Sending Messages

  1. User types and sends message
  2. Frontend sends via WebSocket with action sendMessage
  3. Lambda validates and stores message in Messages table
  4. DynamoDB Stream triggers Stream Processor Lambda
  5. Stream Processor queries all active connections
  6. Stream Processor broadcasts message to all connections
  7. All clients receive and display new message

Disconnection

  1. User closes browser or navigates away
  2. API Gateway triggers $disconnect Lambda
  3. Lambda removes connection from Connections table

Cost Optimization

DynamoDB

  • On-demand pricing for variable workload
  • TTL automatically removes old connections
  • GSI only on required attributes

Lambda

  • Efficient code with minimal dependencies
  • Appropriate memory allocation
  • Short timeout values

S3

  • Static website hosting (cheapest option)
  • CloudFront can be added for HTTPS and global distribution (S3 website endpoints serve HTTP only)

API Gateway

  • WebSocket APIs are billed per message and per connection-minute
  • REST API billed per request
  • No idle costs


Let's deploy it:

Project Structure

.
β”œβ”€β”€ terraform/              # Terraform infrastructure code
β”‚   β”œβ”€β”€ main.tf            # Main infrastructure
β”‚   β”œβ”€β”€ variables.tf       # Input variables
β”‚   β”œβ”€β”€ outputs.tf         # Output values
β”‚   └── lambda.tf          # Lambda configurations
β”œβ”€β”€ lambda/                # Lambda function code
β”‚   β”œβ”€β”€ auth/              # Authentication handlers
β”‚   β”œβ”€β”€ websocket/         # WebSocket handlers
β”‚   └── streams/           # DynamoDB stream processor
β”œβ”€β”€ frontend/              # React frontend
β”‚   β”œβ”€β”€ public/
β”‚   └── src/
β”œβ”€β”€ deploy.sh              # Deployment script
└── cleanup.sh             # Cleanup script

Prerequisites

  • AWS CLI configured with appropriate credentials
  • Terraform >= 1.0
  • Node.js >= 18.x
  • Bash shell

Run these commands to verify you have everything installed:

# Check AWS CLI
aws --version
# Expected: aws-cli/2.x.x or higher

# Check Terraform
terraform version
# Expected: Terraform v1.x.x or higher

# Check Node.js
node --version
# Expected: v18.x.x or higher

# Check npm
npm --version
# Expected: 9.x.x or higher

# Check AWS credentials
aws sts get-caller-identity
# Should show your AWS account details

If any command fails, install the missing tool:

Deploy in 3 Steps

Step 1: Clone Project

git clone https://github.com/sd031/Serverless-Chat-Application.git
cd Serverless-Chat-Application

Step 2: Run Deploy Script

./deploy.sh

Step 3: Confirm Deployment

When prompted, type yes and press Enter.

What Happens During Deployment?

βœ“ Checking prerequisites
βœ“ Building Lambda functions (7 functions)
βœ“ Deploying infrastructure (DynamoDB, Lambda, API Gateway, S3)
βœ“ Building React frontend
βœ“ Uploading to S3
βœ“ Done! Your URLs are displayed

Time: ~5-7 minutes

After deployment, you'll see:

=========================================
Deployment Complete!
=========================================

Website URL: http://serverless-chat-dev-frontend-xxxxx.s3-website-us-east-1.amazonaws.com

API Endpoints

REST API (Authentication)

  • POST /signup - User registration
  • POST /login - User authentication

WebSocket API

  • $connect - Connection handler
  • $disconnect - Disconnection handler
  • sendMessage - Send chat message
  • getMessages - Retrieve message history

Using the Application

1. Open the Website

Copy the Website URL from the deployment output and open it in your browser.

2. Create an Account

  • Click "Sign Up" tab
  • Enter username, email, and password
  • Click "Sign Up" button
  • Switch to "Login" tab

3. Login

  • Enter your email and password
  • Click "Login" button

4. Start Chatting

  • Type a message in the input box
  • Click "Send" or press Enter
  • Your message appears in the chat!

5. Test Real-Time (Optional)

  • Open another browser window (or incognito mode)
  • Create a different account
  • Login with the new account
  • Send messages from both windows
  • See messages appear in real-time! πŸŽ‰

Cleanup

When you're done testing:

./cleanup.sh

Type yes to confirm. This removes all AWS resources and stops any charges.


Challenges

  1. Multiple Chat Rooms: Add room creation and management
  2. Private Messages: Direct messaging between users
  3. File Uploads: S3 integration for media sharing
  4. User Presence: Online/offline status
  5. Message Reactions: Emoji reactions to messages
  6. Search: Full-text search with OpenSearch
  7. Notifications: SNS for push notifications
  8. Analytics: User engagement tracking
  9. CDN: CloudFront for global distribution
  10. Custom Domain: Route53 and ACM for HTTPS


🀷🏻 How to Participate?

βœ… Complete the tasks and challenges.
βœ… Document your progress and key takeaways on GitHub ReadMe, Medium, or Hashnode.
βœ… Share the above in a LinkedIn post tagging me (Sandip Das), and use #90DaysOfCloud to engage with the community!


⚑ Quick Reference

Keep the momentum going β€” everything you need on LearnXOps in one place:

Share this post