---
title: Architecture
description: How the patent reviewer runs on AWS, and why each part is there.
sidebar:
  icon: network
---

The app runs in **eu-west-2 (London)**, the region nearest the team. CloudFront with WAF serves the static client from S3. It also passes `/api/*` and `/ws` through a VPC origin to an internal ALB, which sits in front of Fargate tasks in three Availability Zones. Aurora PostgreSQL Serverless v2 sits in isolated data subnets, ready for the move off SQLite (see [Blocker 1](/assumptions#blocker-1-the-in-memory-database)).

Each environment (dev, staging, prod) has its own AWS account. A one-time `bootstrap` stack in each account creates the Terraform state bucket and the GitHub OIDC roles.

## Request flow

```mermaid
flowchart TB
  user[Browser] -->|HTTPS and WSS| cf[CloudFront + WAF]
  cf -->|default behaviour| s3[(S3 site bucket, OAC, SSE-KMS)]
  cf -->|/api/* and /ws, VPC origin| alb
  subgraph vpc["VPC in eu-west-2, 3 AZs"]
    alb[Internal ALB, private subnets] --> tasks[ECS Fargate tasks, private subnets]
    tasks -->|TLS 5432| db[(Aurora PostgreSQL Serverless v2, data subnets)]
    tasks --> vpce[VPC endpoints: ECR, Logs, Secrets Manager, S3]
    tasks --> nat[NAT gateway per AZ, public subnets]
  end
  nat -->|HTTPS| openai[OpenAI API]
  sm[Secrets Manager] -.->|injected at task start| tasks
  tasks -->|"OTLP to the otel-collector sidecar in each task"| cw[CloudWatch metric Patent/Server, EMF]
  tasks -->|traces, same sidecar| xray[X-Ray]
  cw --> alarms[CloudWatch alarms]
  alarms --> sns[SNS topic, KMS]
  gh[GitHub Actions via OIDC] -->|push image| ecr[ECR]
  gh -->|update service| tasks
  gh -->|sync client| s3
```

## The layers

**[Edge](/modules#edge)**

CloudFront, WAF, S3 site bucket and DNS. The only public entry point.

**[Service](/modules#service)**

Internal ALB and Fargate tasks in private subnets, with autoscaling and deploy alarms.

**[Data](/modules#data)**

Aurora PostgreSQL in data subnets with no internet route, plus the OpenAI secret.

**[Network](/modules#network)**

Three subnet tiers across three AZs, NAT for OpenAI, VPC endpoints, flow logs.

**[Observability](/modules#observability)**

The SNS alarm topic and the KMS key for every log group.

**[Bootstrap](/modules#bootstrap)**

State bucket and GitHub OIDC roles, applied once per account.

## Why this design

Each row is a design target, the choice that answers it, and where that choice lives in `infrastructure/`.

**Design load: 1,000 to 10,000 concurrent users**

Fargate behind an internal ALB. Prod runs 3 to 12 tasks, with target tracking on CPU (60%) and on ALB requests per target. Aurora Serverless v2 runs between 0.5 and 4 ACU. These are starting values, not measurements; see [Capacity](/assumptions#capacity).

`modules/service/ecs.tf`, `modules/service/autoscaling.tf`, `modules/data/main.tf`, `envs/prod/terraform.tfvars`

**Target: 99.9% availability**

The stack spans three AZs, and prod has one NAT gateway in each. Tasks spread across the private subnets with AZ rebalancing, and the Aurora writer and reader run in different AZs. Rolling deploys keep 100% of capacity. The circuit breaker and the 5xx alarm roll back a bad deploy. The ALB health-checks `/healthz`. [Blocker 1](/assumptions#blocker-1-the-in-memory-database) still applies.

`modules/network/main.tf`, `modules/service/ecs.tf`, `modules/service/alarms.tf`, `modules/data/main.tf`

**Low latency in the US, Europe and Asia**

CloudFront (`PriceClass_All`) serves the static client from edge locations. API and WebSocket traffic enters at the nearest edge and travels to the London ALB through a VPC origin. The application itself runs in one region. eu-west-2 is the primary region because it is the region nearest the team.

`modules/edge/main.tf`, `envs/*/terraform.tfvars`

**SOC 2 technical controls**

SOC 2 audits the organisation's controls, so this makes no compliance claim. The Terraform supplies these technical controls:

| Criterion | Control in the code |
|---|---|
| CC6.1 | Customer-managed KMS keys with rotation for every data store. Least-privilege roles: the task role can only write telemetry (log streams in the metrics log group, X-Ray segments in this region), and the execution role can read only two secrets. Private subnets. |
| CC6.6 | WAF managed rules and a rate limit. An internal ALB reachable only through the CloudFront VPC origin. No public IPs except NAT. |
| CC6.7 | TLS 1.2+ at CloudFront and the ALB, `rds.force_ssl`, and TLS-only bucket policies. |
| CC6.8 | ECR scan on push, and immutable tags. |
| CC7.2 | CloudWatch alarms to SNS, including a burn-rate alarm on AI suggestion failures that also rolls back a release. Application audit events for document reads and saves. VPC flow logs. See [Observability](/overview). |
| CC8.1 | Everything in code. Apply and deploy run only from a protected GitHub Environment, and plans run on pull requests. |
| A1.2 | Aurora backups (35 days in prod), deletion protection, multi-AZ instances, and versioned state and site buckets. |

What is not built is listed under [Known gaps](/assumptions#known-gaps).

**Several deploys a day**

GitHub OIDC, so there are no long-lived keys. The deploy role is scoped to fixed resource names. App deploys register a new task definition revision without running Terraform. Immutable ECR tags make a rollback exact.

`bootstrap/iam.tf`, `modules/service/ecs.tf`, `modules/service/ecr.tf`

**Little to operate**

Managed services only: Fargate (no hosts), Aurora Serverless v2, ALB, CloudFront, AWS managed WAF rule groups, and an RDS-managed master password. Five plain modules, the same `main.tf` in every environment, and S3-native state locking with no DynamoDB table.

`modules/*`, `envs/*/main.tf`, `envs/*/backend.tf`

**A protected OpenAI key**

The key lives in a Secrets Manager secret encrypted with a customer-managed key. Terraform creates the secret empty; the value is set out of band, and ECS injects it when a task starts. Only the execution role can read it. Each environment has its own key, in its own account. Egress to OpenAI leaves through the fixed IPs of the NAT gateways (`nat_public_ips`).

`modules/data/main.tf`, `modules/service/iam.tf`, `modules/service/ecs.tf`, `modules/network/main.tf`

**Protected document data**

Aurora sits in data subnets with no route to the internet, and its security group admits only the task security group. Storage, Performance Insights, logs and secrets are encrypted with customer-managed keys. The database requires TLS and has IAM database authentication enabled. When deletion protection is on, a final snapshot is taken.

`modules/data/main.tf`, `modules/network/main.tf`, `modules/service/security_groups.tf`

## Deploy strategy

:::info[Rolling, not blue/green]
ECS rolling deploys (100% minimum healthy, 200% maximum) never drop capacity. The circuit breaker rolls back a deploy whose tasks never become healthy. The 5xx-ratio alarm rolls back a deploy whose tasks pass health checks but fail requests.

Blue/green, through CodeDeploy or native ECS blue/green, adds a second target group and listener rules, and its test traffic only helps if there are tests to run against it. For a small team, the rolling model covers the main failure modes with less to operate. The cost: during a deploy both versions serve traffic, and a long-lived WebSocket stays on the old task until it reconnects.
:::

## Growing to more regions

When US customers need it, the next step is a separate stack in us-east-1: a copy of an `envs/` directory with a new region and domain, and its own data. Asia gets a stack only when a customer, or the latency data, asks for one.
