Skip to main content

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:
X-Ray provides:
  • End-to-end request traces
  • Service map visualization
  • Latency breakdown by component
  • Error correlation across services

Scaling Considerations

Lambda Cold Starts

DynamoDB Capacity

Start with on-demand. After collecting 2-4 weeks of metrics, evaluate if provisioned capacity would be more cost-effective.

API Gateway Throttling

GSI Hot Partitions

Risk: If many users have similar email domains, GSI partition key EMAIL#{email} could create hot partitions. Mitigation:
  • Email addresses are naturally distributed
  • Monitor ConsumedReadCapacityUnits by 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

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.
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
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)
E2E tests are expensive to maintain. Keep them minimal and focused on critical business flows. Most coverage should come from unit and integration tests.

Test Commands


Observability Correlation

Tracing requests across API Gateway, Lambda, DynamoDB, and EventBridge requires consistent correlation IDs.

Request ID Flow

Implementation

Lambda middleware for correlation:
EventBridge event with correlation:
CloudWatch Logs Insights query:

Structured Logging

All logs use JSON format for queryability:
Output: