|
| 1 | +# SES Email Forwarder Lambda Function |
| 2 | + |
| 3 | +This Lambda function forwards emails received by AWS SES to personal email addresses based on alias mappings stored in Airtable. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +When a donor with recurring donations receives a custom email alias (e.g., `john@coders.operationcode.org`), this Lambda function: |
| 8 | +1. Receives the email via SES |
| 9 | +2. Checks Airtable for the alias mapping |
| 10 | +3. Validates the donor's status is "active" |
| 11 | +4. Forwards the email to the donor's personal email address |
| 12 | + |
| 13 | +## Environment Variables |
| 14 | + |
| 15 | +- `EMAIL_BUCKET` - S3 bucket name where SES stores incoming emails |
| 16 | +- `AIRTABLE_SECRET_NAME` - Name of the secret in AWS Secrets Manager containing Airtable credentials |
| 17 | +- `FORWARD_FROM_EMAIL` - Email address to use as the "From" address (e.g., noreply@coders.operationcode.org) |
| 18 | +- `AWS_SES_REGION` - AWS region for SES (us-east-1) |
| 19 | +- `ENVIRONMENT` - Environment name for Sentry (prod/staging) |
| 20 | + |
| 21 | +## Secrets Manager Schema |
| 22 | + |
| 23 | +The secret referenced by `AIRTABLE_SECRET_NAME` must contain: |
| 24 | +```json |
| 25 | +{ |
| 26 | + "airtable_api_key": "patXXXXXXXXXXXXXX", |
| 27 | + "airtable_base_id": "appXXXXXXXXXXXXXX", |
| 28 | + "airtable_table_name": "Email Aliases", |
| 29 | + "sentry_dsn": "https://xxxxx@oxxxxx.ingest.sentry.io/xxxxx" |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +## Local Testing |
| 34 | + |
| 35 | +```bash |
| 36 | +# Create virtual environment |
| 37 | +python3 -m venv venv |
| 38 | +source venv/bin/activate |
| 39 | + |
| 40 | +# Install dependencies |
| 41 | +pip install -r requirements.txt |
| 42 | +pip install pytest moto urllib3 |
| 43 | + |
| 44 | +# Run tests |
| 45 | +pytest tests/ -v |
| 46 | +``` |
| 47 | + |
| 48 | +## Architecture |
| 49 | + |
| 50 | +- **Region**: us-east-1 (required for SES email receiving) |
| 51 | +- **Runtime**: Python 3.12 |
| 52 | +- **Memory**: 256 MB |
| 53 | +- **Timeout**: 30 seconds |
| 54 | +- **Architecture**: ARM64 (Graviton) |
| 55 | + |
| 56 | +## Email Flow |
| 57 | + |
| 58 | +1. Email sent to `alias@coders.operationcode.org` |
| 59 | +2. SES receives email and stores it in S3 |
| 60 | +3. SES invokes Lambda function |
| 61 | +4. Lambda: |
| 62 | + - Retrieves email from S3 |
| 63 | + - Queries Airtable for alias mapping |
| 64 | + - Validates donor status is "active" |
| 65 | + - Rewrites headers (From, Reply-To) |
| 66 | + - Sends email via SES to personal email |
| 67 | +5. Original sender receives replies via Reply-To header |
| 68 | + |
| 69 | +## Error Handling |
| 70 | + |
| 71 | +Errors are logged to: |
| 72 | +- CloudWatch Logs: `/aws/lambda/ses-email-forwarder` |
| 73 | +- Sentry: For alerting and monitoring |
0 commit comments