DocumentDB Solution Summary
Problem
TwinSpires needs to use AWS DocumentDB instead of MongoDB Atlas for their Bike4Mind deployment. DocumentDB requires a TLS certificate file (tlsCAFile
parameter) in the connection string, but SST v2's NextjsSite doesn't support copyFiles
to include the .pem file in the Lambda bundle.
Solution Overview
We implemented a runtime certificate management approach that:
- Embeds the AWS global certificate bundle as a base64 string (or loads from environment variable)
- Writes the certificate to
/tmp/certs/rds-ca-bundle.pem
on Lambda cold start - Automatically detects DocumentDB connections and modifies the connection string
- Maintains full backward compatibility with MongoDB Atlas
Key Components
1. Certificate Manager (documentdb-cert-manager.ts
)
- Manages certificate lifecycle
- Detects DocumentDB connections (via environment variable or URL pattern)
- Modifies connection strings to include certificate path
- Writes certificate only once per Lambda cold start
2. Enhanced Database Connection (mongo.ts
)
- Updated
connectDB
function to detect and handle DocumentDB - Transparent to existing code - no changes needed in API routes
- Logs certificate operations for debugging
3. Configuration (sst.config.ts
)
- Added
MAIN_DB_TYPE
environment variable to all functions - No infrastructure changes required
- Works with existing VPC configuration
Usage
Basic Deployment
# Set the database type
export MAIN_DB_TYPE=DocumentDB
# Deploy
npx sst deploy --stage twinspires-staging
With External Certificate
# Download and convert certificate
./scripts/update-documentdb-cert.sh
# Set certificate as environment variable
export DOCUMENTDB_CA_BUNDLE_BASE64=$(cat global-bundle-base64.txt)
# Deploy
MAIN_DB_TYPE=DocumentDB npx sst deploy --stage twinspires-staging
Advantages
- No Breaking Changes: Feature-flagged implementation preserves existing functionality
- Minimal Code Changes: Only core database package modified
- Flexible Certificate Management: Supports embedded or environment variable certificates
- Automatic Detection: Can detect DocumentDB URLs without explicit configuration
- Performance Optimized: Certificate written once per cold start
- Fully Tested: Includes unit tests for all functionality
Alternative Approaches Considered
- Lambda Layers: Would require significant SST configuration changes
- Build-time File Injection: SST v2 doesn't support this for NextjsSite
- Public S3 Bucket: Security concerns with certificate distribution
- Base64 in Connection String: Mongoose doesn't support this
Next Steps
-
TwinSpires team to:
- Download actual certificate using provided script
- Set up DocumentDB clusters
- Configure VPC and security groups
- Test in staging environment
-
Future improvements:
- Automatic certificate rotation
- Region-specific certificate optimization
- CloudWatch metrics for certificate operations
Files Modified
b4m-core/packages/core/database/src/certs/documentdb-cert-manager.ts
(new)b4m-core/packages/core/database/src/utils/mongo.ts
(updated)b4m-core/packages/core/database/src/index.ts
(updated)sst.config.ts
(updated)- Documentation and scripts (new)
This solution provides a clean, maintainable approach to supporting DocumentDB without disrupting existing MongoDB Atlas deployments.