A serverless trivia application built with AWS Lambda and ElastiCache Serverless for Valkey, demonstrating real-world integration patterns and Valkey's key features.
Users can sign up, play 5-question trivia sessions, and compete on a weekly leaderboard. Questions are sourced from the Open Trivia Database, cached for performance, and scoring rewards both accuracy and speed. The app showcases modern serverless architecture with proper caching, authentication, and real-time data management.
The application follows a serverless architecture pattern with proper AWS component placement and clear data flow visualization:
Global Services:
- CloudFront CDN: Global content delivery with bidirectional S3 connection
- S3 Frontend Bucket: Static website hosting (single-page vanilla HTML/CSS/JavaScript)
- Cognito User Pool: Direct user authentication with JWT token management
- API Gateway: REST API endpoint management (AWS managed service)
- EventBridge: Scheduled event management for question preloading
VPC Architecture:
- Internet Gateway: VPC boundary component for external connectivity
- Public Subnet: Contains NAT Gateway for outbound internet access
- Private Subnet: Isolated Lambda functions and ElastiCache for security
- Lambda Functions: Game Logic and Question Preloader with bidirectional ElastiCache connections
- ElastiCache Serverless for Valkey: In-memory cache with complete data flow integration
External Integration:
- OpenTDB API: Complete question fetching flow through NAT Gateway and Internet Gateway
Data Flow Legend:
- Solid arrows: Primary data flows (requests, API calls)
- Dashed arrows: Response flows (data returning)
- Dotted arrows: Authentication flows (user login/tokens)
- Bold arrows: Scheduled events (EventBridge triggers)
For detailed data flow description, see Architecture Documentation.
- AWS CLI configured with appropriate permissions
- Node.js 18+
- AWS CDK installed globally:
npm install -g aws-cdk
# Clone and deploy
git clone <repository-url>
cd mock-trading-app-lambda/cdk
npm install
npx cdk bootstrap # First time only
npx cdk deployaws cloudformation describe-stacks \
--stack-name TriviaChallenge \
--query 'Stacks[0].Outputs[?OutputKey==`CloudFrontURL`].OutputValue' \
--output text- Fork this repository
- Set AWS credentials in GitHub Secrets:
AWS_ACCESS_KEY_IDAWS_SECRET_ACCESS_KEY
- Push to main branch for automatic deployment
- User Authentication: AWS Cognito integration
- 5-Question Sessions: Trivia from OpenTDB API
- Smart Caching: Questions cached in ElastiCache with TTL and HTML entity decoding
- Duplicate Prevention: Track seen questions per user
- Speed Scoring: Bonus points for quick answers (10 + max(0, 5 - time_taken))
- Weekly Rankings: Sorted sets in Valkey
- Automatic Cleanup: TTL-based data expiry
- Top 10 Display: Current week's best players
- Question Caching:
valkey:weekly_questions:<year>-W<week>(500 questions loaded weekly) - Session Management:
valkey:session:<uuid>(Hash structure with TTL) - User Tracking:
valkey:user:<id>:seen_questions(Set with 7-day TTL) - Leaderboards:
valkey:leaderboard:<year>-<week>(Sorted sets with 8-week TTL)
| Method | Endpoint | Description |
|---|---|---|
| POST | /start-game |
Initialize new trivia session |
| POST | /submit-answer |
Submit answer and get next question |
| POST | /end-session |
Complete session and update leaderboard |
| GET | /leaderboard |
Get current week's top 10 players |
Start Game:
POST /start-game
{
"category": "general"
}Submit Answer:
POST /submit-answer
{
"sessionId": "uuid-here",
"questionId": "question-id",
"answer": "Selected Answer",
"timeTaken": 3.2
}CDK automatically configures these Lambda environment variables during deployment:
VALKEY_HOST: ElastiCache Serverless for Valkey endpointCOGNITO_USER_POOL_ID: Cognito User Pool IDCOGNITO_CLIENT_ID: Cognito Client ID
For GitHub Actions deployment, set these secrets in your repository:
AWS_ACCESS_KEY_ID: Your AWS access keyAWS_SECRET_ACCESS_KEY: Your AWS secret key
valkey:weekly_questions:2025-W36 # 500 pre-loaded questions (weekly refresh)
valkey:user:123:seen_questions # Set of seen question IDs (7-day TTL)
valkey:session:uuid-123 # Hash: userId, score, currentQuestion (1-hour TTL)
valkey:leaderboard:2025-W36 # Sorted set: userId:username β score (8-week TTL)
# Watch for changes and auto-deploy
cd cdk
npx cdk watch
# Generate CloudFormation template
npx cdk synthThe frontend includes mock data for testing without full AWS deployment.
- Question Pre-loading: 500 questions cached weekly for fast retrieval
- HTML Entity Decoding: Clean text processing for better UX
- Connection Reuse: Persistent ElastiCache connections in Lambda
- TTL Management: Automatic cleanup of expired data
- Efficient Caching: Redis-compatible commands for optimal performance
- VPC Isolation: Lambda functions in private subnets
- TLS Encryption: All data in transit encrypted
- IAM Roles: Least privilege access
- Security Groups: Network-level firewall rules
- Cognito Authentication: JWT token validation
βββ architecture/ # Architecture diagrams and docs
βββ cdk/ # CDK infrastructure code
β βββ trivia-stack.ts # Main CDK stack definition
β βββ app.ts # CDK app entry point
β βββ package.json # CDK dependencies
βββ frontend/ # Static web files
βββ .github/workflows/ # CI/CD pipelines
βββ cloudformation.yml # Legacy template (requires existing resources)
βββ index.js # Main Lambda function
βββ valkey-client.js # Valkey connection and operations
βββ question-service.js # OpenTDB API integration
βββ README.md # This file
MIT License - feel free to use this for learning and development!
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
Built with β€οΈ using AWS Lambda and ElastiCache Serverless for Valkey
