# AWS

> Deploy the engine in your AWS account with one template: Lambda behind CloudFront at your address, a new RDS Postgres or yours, and SES.

Applies to: the Sypnia engine (open source, self-hosted).  
Source: https://data.docs.sypnia.com/self-hosting/aws/

For agents: AWS self-host: CloudFormation template deploy/aws.yaml. Creates the engine Lambda (arm64, function URL, install on cold start), an events Lambda (minute tick, SES bounces, admin actions), CloudFront at DomainName with your us-east-1 certificate (origin secret in Secrets Manager), optional Route 53 records, an optional Lambda@Edge that restores WWW-Authenticate, RDS PostgreSQL 16 (DatabaseMode=create: generated password in Secrets Manager, public with TLS, DbAllowedCidr, final snapshot on delete) or your Postgres (DatabaseMode=existing: SSM SecureString DatabaseUrlParameter), SES configuration set + bounce topic and an optional domain identity with DKIM. Prerequisites: the Lambda package in an S3 bucket of the region, a master key in SSM SecureString (MasterKeyParameter), a certificate in us-east-1. Keys: aws lambda invoke on the events function with {action: api_key, ref: default, kind}.

The template `deploy/aws.yaml` builds the [reference implementation](https://data.docs.sypnia.com/architecture/reference-implementation-aws) in your account, for one project at your address.

## What it creates

- **The engine.** The engine on **Lambda** (arm64) with a function URL. It installs or upgrades the schema when it starts.
- **The events function.** A second function for the minute tick (e-mail retries, cleanup), SES bounces and complaints, and admin actions.
- **CloudFront** at your address with your certificate. A secret header, kept in Secrets Manager, makes sure nobody skips it to call the function URL directly.
  - Optionally, a Lambda\@Edge function that puts back the `WWW-Authenticate` header, which Lambda function URLs rename; MCP clients need it.
- **Postgres.** Either a new **RDS PostgreSQL 16** (`DatabaseMode=create`) or yours (`DatabaseMode=existing`).
  - A new instance gets a generated password in Secrets Manager. It is publicly reachable with TLS required: the functions run outside a VPC, so narrow `DbAllowedCidr` when you can.
  - It takes a final snapshot when the stack is deleted.
- **SES.** A configuration set wired to bounce handling, and, if you name a domain, its identity with DKIM records.
- **DNS.** Route 53 records for your address, when you give the zone.

## Prepare

1. **The engine's Lambda package** in an S3 bucket of the same region. Download `sypnia-data-lambda-arm64.zip` from the release, or build it with `cargo lambda build --release --arm64 -p sypnia-data-server --output-format zip`.
   ```bash
   aws s3 cp sypnia-data-lambda-arm64.zip s3://my-bucket/sypnia-data/0.1.0/sypnia-data-lambda-arm64.zip
   ```
2. **A master key** in SSM Parameter Store, generated in place and never pasted anywhere:
   ```bash
   aws ssm put-parameter --name /sypnia-data/master-key --type SecureString --value "$(openssl rand -base64 32)"
   ```
3. **A certificate in `us-east-1`** (ACM) that covers your address, e.g. `api.myapp.com`.
4. With your own Postgres, its connection string as an SSM SecureString. It must be a user that can install (create roles and a schema):
   ```bash
   aws ssm put-parameter --name /sypnia-data/database-url --type SecureString --value 'postgres://…?sslmode=require'
   ```

## Deploy

```bash
aws cloudformation deploy --stack-name my-data --template-file deploy/aws.yaml --capabilities CAPABILITY_IAM \
  --parameter-overrides CodeBucket=my-bucket CodeKey=sypnia-data/0.1.0/sypnia-data-lambda-arm64.zip \
    DomainName=api.myapp.com CertificateArn=arn:aws:acm:us-east-1:…:certificate/… HostedZoneId=Z0123… \
    DatabaseMode=create MailProvider=ses MailFrom='My app <no-reply@myapp.com>' MailDomain=myapp.com
```

| Parameter                                                | Default                                             | What                                                                                                                    |
| -------------------------------------------------------- | --------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------- |
| `CodeBucket`, `CodeKey`                                  | —, `sypnia-data/0.1.0/sypnia-data-lambda-arm64.zip` | where the Lambda package is                                                                                             |
| `MasterKeyParameter`                                     | `/sypnia-data/master-key`                           | the SSM SecureString with the master key                                                                                |
| `DomainName`, `CertificateArn`                           | —                                                   | your address and its us-east-1 certificate                                                                              |
| `HostedZoneId`                                           | empty                                               | your Route 53 zone: alias record and DKIM records                                                                       |
| `DatabaseMode`                                           | `create`                                            | `create` (new RDS) or `existing` (`DatabaseUrlParameter`)                                                               |
| `DatabaseUrlParameter`                                   | `/sypnia-data/database-url`                         | with `existing`: the SSM SecureString with the connection string                                                        |
| `DbInstanceClass`, `DbAllocatedStorage`, `DbAllowedCidr` | `db.t4g.micro`, `20`, `0.0.0.0/0`                   | with `create`                                                                                                           |
| `MailProvider`                                           | `ses`                                               | `ses`, `smtp`, `resend`, `postmark`, `none`                                                                             |
| `MailFrom`, `MailDomain`                                 | empty                                               | the sender; with SES, the domain to verify                                                                              |
| `MailSecretParameter`                                    | empty                                               | with `smtp`/`resend`/`postmark`: an SSM SecureString with the SMTP URL, API key or token                                |
| `ReservedConcurrency`, `PoolMax`                         | `5`, `4`                                            | at most this many instances, each with this many connections                                                            |
| `RestoreWwwAuthenticate`                                 | `true`                                              | the Lambda\@Edge function; deleting a stack that has it waits for CloudFront to remove its replicas (up to a few hours) |

Without `HostedZoneId`, point your address at the output `DistributionDomainName` yourself. A new SES account starts in the sandbox: ask AWS for production access before sending to anyone.

## Keys and a first check

The first request installs the schema. Then create the project's keys with the events function (outputs `EventsFunction`):

```bash
aws lambda invoke --function-name my-data-events --cli-binary-format raw-in-base64-out \
  --payload '{"action":"api_key","ref":"default","kind":"publishable"}' /dev/stdout
curl https://api.myapp.com/health
```

Use `"kind":"secret"` for a secret key, and save its output somewhere safe; it is shown once. The MCP server is at `https://api.myapp.com/mcp`.

## Upgrading and deleting

- **Upgrading.** Upload the new package under a new key and deploy again with that `CodeKey`. The next cold start installs the new migrations, which are additive ([upgrades](https://data.docs.sypnia.com/self-hosting/upgrades-and-backups)).
- **Deleting.** Deleting the stack keeps a final snapshot of a created database. Delete that snapshot yourself when you no longer need it.
