Infrastructure Overview
The User Service infrastructure is managed through a combination of Serverless Framework (for Lambda and API Gateway) and Terraform (for shared resources like Cognito and DynamoDB).Terraform Resources
Cognito User Pool
DynamoDB Table
EventBridge
Serverless Framework
serverless.yml
CI/CD Pipeline
GitHub Actions Workflow
This workflow uses OIDC federation instead of long-lived AWS access keys. OIDC is the recommended approach for GitHub Actions as it eliminates the need to store AWS credentials as secrets and provides short-lived, automatically rotated credentials.
OIDC IAM Role Setup
The GitHub Actions OIDC provider must be configured in AWS:Pipeline Stages
1
PR Checks
Lint, type-check, unit tests on every pull request
2
Deploy Staging
Automatic deployment to staging on merge to main
3
Integration Tests
Run integration tests against staging environment
4
Deploy Production
Manual approval required, then deploy to production
Monitoring
CloudWatch Metrics
CloudWatch Dashboard
X-Ray Tracing
Enable distributed tracing across services:- End-to-end request traces
- Service map visualization
- Latency breakdown by component
- Error correlation across services
Scaling Considerations
Lambda Cold Starts
DynamoDB Capacity
API Gateway Throttling
GSI Hot Partitions
Risk: If many users have similar email domains, GSI partition keyEMAIL#{email} could create hot partitions.
Mitigation:
- Email addresses are naturally distributed
- Monitor
ConsumedReadCapacityUnitsby partition key - If issues arise, consider write sharding for high-volume patterns
Cost Analysis
Understanding costs at different scales helps with capacity planning and budget forecasting.Cost Breakdown by Scale
MAU = Monthly Active Users. Assumes average 10 API calls per user per month, 1KB average payload. Cognito is the largest cost driver at scale due to per-MAU pricing ($0.0055/MAU after 50K free tier).
Cost Assumptions
Cost Optimization Strategies
DynamoDB: On-Demand vs Provisioned
DynamoDB: On-Demand vs Provisioned
Switch to provisioned capacity with auto-scaling when traffic is predictable. Can reduce costs by 50-70% at steady state.Break-even: ~25% capacity utilization. If you’re consistently above this, provisioned is cheaper.
Cognito Alternatives
Cognito Alternatives
At 1M+ users, consider self-managed auth (e.g., Auth0, or custom JWT issuance). Cognito’s per-MAU pricing becomes expensive.Trade-off: Operational complexity vs. cost savings
Lambda: Reduce Cold Starts
Lambda: Reduce Cold Starts
Use Graviton2 (ARM) processors for 20% cost reduction. Smaller bundles reduce cold start time and execution cost.
Testing Strategy
A comprehensive testing approach ensures reliability without slowing down development.Testing Pyramid
Unit Tests
Tools: Jest, ts-jest What to test:- Business logic in isolation (validation, transformations)
- DynamoDB marshalling/unmarshalling
- Error handling paths
Integration Tests
Tools: Jest, DynamoDB Local, LocalStack What to test:- Lambda handlers with real DynamoDB operations
- Cognito trigger flows
- EventBridge event publishing
Contract Tests
Tools: Pact, EventBridge Schema Registry What to test:- Event schemas match consumer expectations
- API response shapes are stable
E2E Tests
Tools: Cypress, Playwright (for full-stack), or custom scripts What to test:- Full user registration flow
- Critical paths (login → action → logout)