Assumptions and gaps
What the Terraform expects from the application, what is still a placeholder, and what is not built yet.
Application changes
The Terraform expects four things from the application. The application now provides all of them:
- A health route.
GET /healthzreturns 200 (server/app/__main__.py). The ALB target group checks it (health_check_path). - An
/apiprefix. The document routes sit on anAPIRouter(prefix="/api")inserver/app/__main__.py, which matchesapi_path_pattern = "/api/*"./healthzand the WebSocket/wsstay at the root, because CloudFront and the ALB route them as their own paths. - Same-origin URLs.
client/src/config.tsdefaults the API base to/apiand builds the WebSocket URL from the page origin, so anhttpspage opens awss://socket.VITE_API_BASEoverrides the base for a build that targets another host.client/vite.config.tsproxies/apiand/wsto the local server for development, andclient/src/App.tsxandclient/src/Document.tsxuse these values. - A production image.
server/Dockerfileends in aproductiontarget, the default because it is the last stage. It runs one uvicorn process as a non-root user (uid 10001) with no--reload, and--reloadstays in the separatedevtarget.scripts/ci/build-server-image.shbuilds it forlinux/amd64. The task definition ininfrastructure/modules/service/ecs.tfsetsreadonlyRootFilesystem = trueand mounts a writable/tmp.
Blocker 1: the in-memory database
The fix. server/README.md forbids changes to app/internal/, so the fix goes outside it:
- Add a database module outside
app/internal/. It builds the SQLAlchemy engine fromDB_HOST,DB_PORT,DB_NAME,DB_USERandDB_PASSWORD, with TLS required. - Make
__main__.pyimport that module instead ofinternal/db.py. - Replace
create_alland the seed inserts inlifespanwith migrations. On a shared database, the fixed-ID seed insert fails as soon as a second task starts.
Aurora is provisioned in every environment for this fix, and the task definition already passes the DB_* variables.
Placeholders
Replace these before the first plan:
| File | Setting | Current value |
|---|---|---|
envs/*/terraform.tfvars |
domain_name |
dev.app.example.com, staging.app.example.com, app.example.com |
envs/*/terraform.tfvars |
route53_zone_id |
Z00000000PLACEHOLDER |
envs/*/terraform.tfvars |
alarm_email |
null, so no subscription |
bootstrap -var |
github_repository |
OWNER/REPO |
openai_model is gpt-6-luna in every environment, because the application’s own fallback (gpt-3.5-turbo-1106) is deprecated. The task definition always sets OPENAI_MODEL, so the fallback is never used.
Environment settings
These are starting values, not measurements.
| Setting | dev | staging | prod |
|---|---|---|---|
min_tasks / max_tasks |
1 / 2 | 2 / 4 | 3 / 12 |
single_nat_gateway |
true | true | false |
db_instance_count |
1 | 2 | 2 |
db_backup_retention_days |
1 | 7 | 35 |
db_deletion_protection |
false | true | true |
use_fargate_spot |
true | false | false |
log_retention_days |
30 | 90 | 365 |
trace_sample_ratio |
1.0 | 1.0 | 0.1 |
vpc_cidr |
10.10.0.0/16 |
10.20.0.0/16 |
10.30.0.0/16 |
Capacity
Production starts at 3 to 12 Fargate tasks of 0.5 vCPU and 1 GB. These are starting values, not load-test results.
One socket, one call at a time
Each open editor tab holds one WebSocket, and each 500 ms debounced edit sends one message. The handler is async and serial per connection, so each socket has at most one OpenAI call in flight. Later edits wait in a queue.
Scale by adding tasks
The /document and /save routes are sync and run in Starlette’s thread pool. Each task runs one uvicorn process, so we scale by adding tasks.
Weak scaling signals
Autoscaling tracks CPU (60%) and ALB requests per target. Both are weak signals for this load: the work is mostly I/O wait, and the ALB counts a WebSocket only once, at the upgrade. A better signal is in-flight suggestions per task. The server now counts them (suggestions.in_flight, see Signals), but only locally: the AWS collector does not send it to CloudWatch, and autoscaling on it is not built.
OpenAI is the first limit
We expect the OpenAI quota (requests and tokens per minute) to be the first limit at 10,000 users, before Fargate, because every debounced edit sends the full document to OpenAI. This is not measured.
Load-test method
Isolate one task
Stub OpenAI
Open N WebSockets that each send a document edit every few seconds. Replace OpenAI with a stub that has a fixed latency, so the test measures our service and not OpenAI.
Find the knee
Raise N until p95 latency or CPU passes the target. That N per task sets min_tasks, max_tasks, and the target for the in-flight-suggestions metric.
Known gaps
SOC 2 services not built
Not built: CloudTrail, GuardDuty, AWS Config, Security Hub, IAM Identity Center, access logs for the ALB, CloudFront and WAF, and AWS Backup copies. There is no Content-Security-Policy header.
Database password rotation
RDS rotates the managed master password, but ECS injects secrets only when a task starts. Running tasks keep the old password until they are replaced.
ALB ingress covers all of CloudFront
The ALB admits the com.amazonaws.global.cloudfront.origin-facing prefix list. That list covers all of CloudFront, not only this distribution. AWS documents a tighter source, the CloudFront-VPCOrigins-Service-SG group, but that group exists only after the VPC origin is created.
WAF body-size rule set to count
SizeRestrictions_BODY in the common rule set only counts matches, because POST /save sends whole patents larger than 8 KB. The managed rules are configured but not tuned against real traffic.
Broad Terraform roles
The plan role has ReadOnlyAccess on the account, so it can read CloudWatch logs and S3 objects, including state. It cannot read secret values. The apply role has AdministratorAccess, and its only gate is the protected GitHub Environment.
Deploy role scope
The deploy role can invalidate any CloudFront distribution in its account. Its ecs:RegisterTaskDefinition permission is scoped to task-definition/${name}-*, following the IAM service authorization reference; this has not been tested against a real deploy.
Smaller notes
- The ECR repository uses the data module’s KMS key.
- Dev runs on Fargate Spot.
- The application runs only in eu-west-2. us-east-1 holds just the CloudFront certificate and the WAF web ACL, because CloudFront requires them there.