Lambda-Supabase Integration Proposal (Updated)
Executive Summary
This proposal outlines how to integrate Supabase PostgreSQL database with your existing AWS Lambda functions for streaming data from DynamoDB to Supabase. The solution leverages the IPv4-compatible connection patterns discovered during EC2 testing and adapts them for Lambda’s serverless environment with a focus on simplicity, modularity, and production safety.Architecture Overview
Current State
- DynamoDB Tables: ConsultationsTable, TaskStatusTable
- Lambda Functions: Agent processing with SQS triggers
- Missing: Real-time data synchronization to Supabase
Proposed Architecture
Key Technical Decisions
1. VPC and Static IP Requirements
Critical Networking Component:- Lambda functions require a static IP address to connect to Supabase
- Supabase requires whitelisting specific IP addresses for database access
- Solution: Deploy Lambdas in a VPC with NAT Gateway and Elastic IP
2. Environment Isolation Strategy
Multi-Environment Support:- Parameterized CDK stack supporting
devandprodenvironments - All resources prefixed with environment name (e.g.,
Dev-ConsultationsTable) - Separate CloudFormation stacks (e.g.,
AgentStack-dev,AgentStack-prod) - Isolated VPCs and IP addresses per environment
3. Modular Lambda Architecture
Restructured Directory Layout:4. Connection Strategy for Lambda
✅ Transaction Pooler (Recommended for Lambda)- Lambda functions are ephemeral (short-lived)
- Transaction pooler designed for serverless workloads
- Better connection management for Lambda’s lifecycle
- Automatic connection cleanup
5. Simplified Dependencies Strategy
Lightweight Dependencies for Lambda:- Use
boto3.dynamodb.types.TypeDeserializerinstead of manual unmarshalling - Single-file Lambda handlers for better maintainability
- Cached connection pattern leveraging Lambda execution context reuse
Implementation Plan
Phase 1: Environment Configuration
1.1 CDK Environment Parameterization
1.2 VPC with Static IP Configuration
Phase 2: Simplified Lambda Implementation
2.1 Common Database Connector
2.2 Simplified Supabase Stream Processor
2.3 Supabase Scenarios Table Schema
Phase 3: Updated CDK Stack Configuration
3.1 Environment-Aware Resource Creation
Deployment Strategy
1. Pre-deployment Checklist
- Create Supabase scenarios table in dev environment
- Set environment variables in
.envfile:SUPABASE_PASSWORD_DEVSUPABASE_PASSWORD_PROD
- Test Lambda functions locally
- Verify VPC and NAT Gateway configuration
2. Deployment Commands
3. Environment Management
Cost Analysis
Per Environment Costs
VPC Components:- NAT Gateway: ~$45/month per environment
- Elastic IP: ~$3.65/month per environment
- Invocations: ~$0.20 per 1M requests
- Duration: ~$0.0000166667 per GB-second
- Estimated monthly cost: $5-20 per environment
- Read requests: $0.02 per 100,000 read request units
- Estimated monthly cost: $1-5 per environment
Success Metrics
Technical Metrics
- ✅ Connection success rate > 99%
- ✅ Processing latency < 5 seconds
- ✅ Error rate < 1%
- ✅ Zero data loss (idempotency working)
- ✅ Static IP connectivity to Supabase
Business Metrics
- ✅ Real-time data availability in Supabase
- ✅ Environment isolation (dev/prod)
- ✅ Reduced deployment complexity
- ✅ Improved data consistency across systems
Risk Mitigation
1. Network Connectivity
- Risk: Lambda cannot reach Supabase
- Mitigation: VPC with NAT Gateway + IP whitelisting
2. Environment Conflicts
- Risk: Dev changes affecting prod
- Mitigation: Complete resource isolation with environment prefixes
3. Connection Failures
- Risk: Supabase unavailable
- Mitigation: DLQ + retry logic + connection caching
4. Data Loss
- Risk: Lambda failures
- Mitigation: DynamoDB streams retain data for 24 hours + idempotency
Next Steps
- Review and approve this updated proposal
- Set up dev Supabase project and create scenarios table
- Configure environment variables in
.envfile - Deploy dev stack using
cdk deploy AgentStack-dev -c env=dev - Add NAT Gateway IP to Supabase network restrictions
- Test end-to-end flow with sample data
- Monitor and optimize based on real usage patterns
- Plan production migration once dev environment is validated