This is the advanced guide for AWS Lambda. If you’re just getting started, check out the Quickstart Guide first.
Using Raw Fetch API
If you prefer not to add the SDK dependency, use the native fetch API:Node.js
Python
Using AWS Secrets Manager
For production deployments, store your API key in AWS Secrets Manager instead of environment variables:Grant Lambda permission
Add the
secretsmanager:GetSecretValue permission to your Lambda execution role:Deployment Strategies
Using Lambda Layers
Create a Lambda Layer to share the Lettr SDK across multiple functions:Using Container Images
Deploy Lambda functions as container images for more control:Event Triggers
API Gateway Integration
Connect your Lambda function to API Gateway for HTTP-triggered emails:EventBridge Integration
Trigger emails from EventBridge events:SQS Integration
Process email requests from an SQS queue for reliable, async sending:Monitoring and Logging
CloudWatch Logs
Lambda automatically sends logs to CloudWatch. Structure your logs for easy searching:CloudWatch Metrics
Create custom metrics for email sending:Troubleshooting
Cold start performance
Cold start performance
Lambda cold starts can add 1-3 seconds to the first invocation. Optimize by:
- Using Provisioned Concurrency for predictable latency
- Minimizing dependencies — use native fetch instead of axios
- Caching clients outside the handler function
Function timeouts
Function timeouts
If emails fail to send before the function times out:
- Increase timeout to 30 seconds (default is 3 seconds)
- Use async processing — send to SQS and process in a separate function
- Check for network issues — ensure Lambda has internet access
Authentication errors (401 Unauthorized)
Authentication errors (401 Unauthorized)
Rate limiting (429 errors)
Rate limiting (429 errors)
If you’re sending high volumes, you may hit rate limits. Use exponential backoff:
Secrets Manager errors
Secrets Manager errors
If you have issues retrieving secrets:
- Verify IAM permissions — ensure
secretsmanager:GetSecretValueis granted - Check secret name — it must match exactly
- Verify region — Secrets Manager is region-specific
- Cache the client — avoid fetching on every invocation
Best Practices
- Use environment variables for configuration (API key, from email, etc.)
- Cache the Lettr client outside the handler to improve performance
- Set appropriate timeouts — 10-30 seconds for email sending functions
- Implement proper error handling — distinguish between retryable and permanent errors
- Log request IDs for debugging and tracking delivery
- Use IAM roles with least privilege permissions
- Enable X-Ray tracing for performance monitoring
- Consider SQS for high-volume or batch sending