# Syncploy — Full Documentation Knowledge Base > The Zero-DoW (Denial of Wallet) Serverless Framework for AWS. Pure TypeScript framework with compile-time Denial-of-Wallet immunity. > Complete combined documentation bundle for AI coding agents and LLMs. --- ## Table of Contents 1. [Introduction & Architecture](#introduction) — *Getting Started* 2. [Installation & Tooling](#installation) — *Getting Started* 3. [5-Minute Quickstart](#quickstart) — *Getting Started* 4. [api & Lambda Ingress](#construct-api) — *Core Constructs* 5. [Auth (Amazon Cognito)](#construct-auth) — *Core Constructs* 6. [Table (DynamoDB & ElectroDB)](#construct-table) — *Core Constructs* 7. [Bucket (Amazon S3)](#construct-bucket) — *Core Constructs* 8. [Queue & DLQ (Amazon SQS)](#construct-queue) — *Core Constructs* 9. [Topic & Secret (SNS & Secrets)](#construct-topic-secret) — *Core Constructs* 10. [Safety Rails & Denial-of-Wallet Shield](#safety-rails) — *Compiler & Safety* 11. [Security, Supply Chain & Trust](#security) — *Compiler & Safety* 12. [Least-Privilege IAM Policy Generator](#least-privilege-iam) — *Compiler & Safety* 13. [Brownfield Adoption & Diff](#brownfield-adoption) — *Compiler & Safety* 14. [In-Memory Zero-Mock Testing](#testing) — *CLI & Operations* 15. [CLI Command Reference](#cli-reference) — *CLI & Operations* 16. [Case Study: EHR Annotation Platform](#ehr-backend-case-study) — *Case Studies* --- # Introduction & Architecture Syncploy is a compiler-driven **Infrastructure-from-Code (IfC)** engine and runtime SDK built exclusively for **pure AWS Serverless** in TypeScript. Instead of writing thousands of lines of handwritten CloudFormation/SAM YAML or managing multi-language imperative graphs (like AWS CDK or Pulumi), you author your application logic in standard TypeScript. The Syncploy compiler statically analyzes your Abstract Syntax Tree (AST), validates event cycles, and deterministically synthesizes 1:1 CloudFormation templates with zero stateful replacements. ```typescript import { Table, Queue, api } from "@syncploy/sdk"; // 1. Declare stateful serverless constructs export const ordersTable = new Table("orders", { partitionKey: { name: "id", type: "string" }, }); export const orderQueue = new Queue("order-processing", { visibilityTimeout: "180s", deadLetterQueue: { maxReceiveCount: 3 }, }); // 2. Declare application logic with automatic infrastructure wiring export const checkoutApi = api( { path: "/checkout", method: "POST", ingress: "function-url", concurrency: 50 }, async (req) => { const order = await req.json(); await ordersTable.put(order); await orderQueue.send({ orderId: order.id }); return Response.json({ success: true, orderId: order.id }); } ); ``` > [!NOTE] > When you run `syncploy build`, Syncploy synthesizes an `AWS::Serverless::Function`, `AWS::DynamoDB::Table`, `AWS::SQS::Queue`, and optimal IAM roles without requiring any external cloud accounts or Docker daemons. > [!IMPORTANT] > **Project Status: Public Alpha (`v0.1.x`)** > Syncploy is currently in active public alpha with the `@syncploy/sdk` package published on [npm](https://www.npmjs.com/package/@syncploy/sdk). While the underlying compiler and runtime are battle-tested in live production workloads (such as our [EHR Annotation Platform Showcase](/docs/ehr-backend-case-study)), APIs and construct ergonomics are actively evolving toward `v1.0`. Early feedback and issue reports are welcome on [GitHub](https://github.com/harsh-vardhhan/syncploy). --- ## How the Compiler Works The Syncploy compilation pipeline consists of 5 deterministic stages: 1. **AST Parsing (`ts-morph`)**: The compiler parses your TypeScript code without executing it, identifying declared cloud constructs (`Table`, `Queue`, `Bucket`, `Auth`, `api`). 2. **Intermediate Representation (IR)**: An abstract cloud topology graph is generated, mapping every compute handler to its data mutations. 3. **Safety Rails & Cycle Detection**: The compiler checks for infinite event recursion (e.g. Table Stream Worker writing back to the same Table) and Denial-of-Wallet (DoW) risks. 4. **Deterministic Synthesis**: Generates clean, human-readable CloudFormation/SAM templates matching exact logical IDs. 5. **Asset Bundling (`esbuild`)**: Compiles handlers into treeshaken, minified ESM bundles (or native container images) with sub-50ms cold start performance. --- ## Architectural Comparison | Architectural Capability | Syncploy | AWS CDK | SST (Ion) | Serverless Framework | Terraform | | --- | --- | --- | --- | --- | --- | | **Paradigm** | Infrastructure-from-Code | Imperative IaC | Imperative IaC | Declarative YAML | Declarative HCL | | **Language** | Pure TypeScript | Multi-language | TypeScript / Pulumi | YAML / JSON | HCL | | **Recursion Shield** | Yes (Compile-Time) | No | No | No | No | | **Zero-Replacement Diff** | Yes (Automated) | No (Manual diff) | No | No | Yes (Plan preview) | | **IAM Policy Generator** | Yes (`syncploy policy`) | No | No | No | No | | **Cold Start Overhead** | Sub-50ms (Pure ESM) | Standard | Standard | Standard | N/A | | **Vendor Lock-in** | Zero (1:1 CFN Ejection) | AWS proprietary | Pulumi Cloud | Serverless Dashboard | HashiCorp | --- ## Core Design Principles - **Pure AWS Serverless**: Compute and storage scale to zero ($0.00/mo idle cost). No VPC NAT gateways, no idle EC2 instances. - **Enterprise Security Standards**: Production security defaults baked-in (immutable S3 ObjectLock audit trails, strict Cognito authentication, AES-256 encryption). - **100% Ejection Freedom**: You own the output. At any time, export standard AWS SAM/CloudFormation templates and deploy via vanilla AWS CLI. --- # Installation & Tooling Syncploy is distributed as two lightweight npm packages: - **`@syncploy/sdk`**: Runtime constructs and zero-dependency cloud clients used in your application code. - **`syncploy` / `@syncploy/compiler`**: The CLI and compiler engine used during build, diff, adopt, and deployment. --- ## 1. Package Installation Using **Bun** (recommended for fastest builds): ```bash bun add @syncploy/sdk bun add -d syncploy @syncploy/compiler ``` Using **npm**: ```bash npm install @syncploy/sdk npm install --save-dev syncploy @syncploy/compiler ``` Using **pnpm**: ```bash pnpm add @syncploy/sdk pnpm add -D syncploy @syncploy/compiler ``` --- ## 2. TypeScript Configuration (`tsconfig.json`) Syncploy expects modern ECMAScript module resolution. Ensure your `tsconfig.json` includes: ```json { "compilerOptions": { "target": "ES2022", "module": "ESNext", "moduleResolution": "bundler", "strict": true, "esModuleInterop": true, "skipLibCheck": true, "types": ["node"] }, "include": ["src/**/*"] } ``` --- ## 3. Recommended Project Layout Syncploy works seamlessly with single microservices, modular apps, or monorepos: ```text my-serverless-app/ ├── package.json ├── tsconfig.json ├── .syncploy/ # Generated CloudFormation & bundled assets │ ├── template.json │ └── build/ ├── src/ │ ├── infra.ts # Cloud constructs (Table, Queue, Auth, Bucket) │ ├── api/ # Public HTTP endpoints │ │ ├── checkout.ts │ │ └── webhooks.ts │ └── workers/ # Asynchronous stream & queue workers │ ├── order-worker.ts │ └── audit-worker.ts ``` --- ## 4. `package.json` Scripts Add Syncploy workflow commands to your `package.json`: ```json { "scripts": { "build": "syncploy build", "diff": "syncploy diff . -a template.yaml --check-replacement", "policy": "syncploy policy . -s my-production-stack -o deploy-policy.json", "test": "bun test" } } ``` --- # 5-Minute Quickstart In this walkthrough, we will build a production-grade **Patient Document Annotation API** with a DynamoDB single-table, an SQS processing queue with DLQ, and an authenticated Lambda Function URL. --- ## Step 1: Initialize Project ```bash mkdir my-serverless-service && cd my-serverless-service bun init -y bun add @syncploy/sdk bun add -d syncploy @syncploy/compiler ``` --- ## Step 2: Define Infrastructure (`src/infra.ts`) Create `src/infra.ts` to declare your serverless constructs: ```typescript import { Table, Queue, Auth } from "@syncploy/sdk"; // HIPAA-compliant Cognito Authentication export const auth = new Auth("ehr-auth", { logicalId: "EhrUserPool", adminOnly: true, passwordPolicy: { minLength: 12 }, }); // Single-Table DynamoDB database with pay-per-request billing export const patientTable = new Table("patients", { logicalId: "PatientTable", partitionKey: { name: "PK", type: "string" }, sortKey: { name: "SK", type: "string" }, billingMode: "PAY_PER_REQUEST", }); // Resilient SQS queue with 3x retry DLQ export const processingQueue = new Queue("annotations", { logicalId: "AnnotationQueue", visibilityTimeout: "180s", deadLetterQueue: { maxReceiveCount: 3 }, }); ``` --- ## Step 3: Implement API Handler (`src/app.ts`) Create `src/app.ts` with your application logic: ```typescript import { api } from "@syncploy/sdk"; import { patientTable, processingQueue, auth } from "./infra"; export const uploadAnnotation = api( { path: "/annotations", method: "POST", ingress: "function-url", concurrency: 25, // DoW Protection: Caps max concurrent executions memory: 512, }, async (req) => { const body = await req.json(); // 1. Persist record to DynamoDB const record = { PK: `PATIENT#${body.patientId}`, SK: `DOC#${Date.now()}`, notes: body.notes, createdAt: new Date().toISOString(), }; await patientTable.put(record); // 2. Queue asynchronous annotation task await processingQueue.send({ patientId: body.patientId, documentId: record.SK, }); return Response.json({ status: "queued", recordId: record.SK, poolId: auth.userPoolId, // Auto-injected Cognito pool ID }); } ); ``` --- ## Step 4: Run Compilation & Safety Audit ```bash bunx syncploy build ``` Syncploy analyzes your AST, builds the Intermediate Representation, verifies that no circular recursion exists, and outputs: ```text ✔ Parsed 3 constructs in src/infra.ts ✔ Verified 0 infinite recursion cycles ✔ Verified 0 Denial-of-Wallet concurrency leaks ✔ Synthesized CloudFormation template to .syncploy/template.json ✔ Bundled uploadAnnotation Lambda asset (14.2 KB ESM) ``` > [!TIP] > Inspect the synthesized `.syncploy/template.json` to see 100% standard AWS CloudFormation with zero custom runtime dependencies. --- # `api` & Lambda Ingress The `api` construct defines serverless HTTP endpoints using standard Web Fetch API conventions (`Request` and `Response`). ```typescript import { api } from "@syncploy/sdk"; export const myEndpoint = api( { path: "/documents/:id", method: "GET", ingress: "function-url", memory: 1024, timeout: "15s", concurrency: 50, }, async (req) => { return Response.json({ message: "Hello from Syncploy!" }); } ); ``` --- ## Ingress Modes: Function URLs vs API Gateway Syncploy supports two distinct ingress modes: | Ingress Mode | Description | Latency Overhead | Pricing | Best For | | --- | --- | --- | --- | --- | | `"function-url"` | Direct AWS Lambda Function URL | **Sub-50ms** | **$0.00** (Free direct invocation) | Microservices, High-throughput APIs, Webhooks | | `"api-gateway"` | Amazon API Gateway HTTP API v2 | ~70-120ms | $1.00 / million requests | Legacy path prefix routing, custom authorizers | > [!TIP] > Function URLs eliminate the $1.00/million API Gateway toll entirely while removing cold start routing hops. --- ## Configuration Reference | Option | Type | Default | Description | | --- | --- | --- | --- | | `path` | `string` | `"/"` | URL path route | | `method` | `"GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "ANY"` | `"GET"` | HTTP method verb | | `ingress` | `"function-url" | "api-gateway"` | `"function-url"` | Ingress routing mechanism | | `memory` | `number` | `256` | Memory allocation in MB (128 to 10240) | | `timeout` | `string | number` | `"30s"` | Maximum execution duration | | `concurrency` | `number` | `undefined` | Reserved concurrency cap (prevents Denial-of-Wallet) | | `cors` | `boolean | CorsOptions` | `true` | Automatic CORS preflight headers | | `packageType` | `"Zip" | "Image"` | `"Zip"` | ESM bundle vs Native Docker container image | | `logRetentionInDays` | `number` | `14` | CloudWatch LogGroup retention in days (auto-synthesized to prevent log accumulation bills) | --- ## Automatic Denial-of-Wallet Log Rotation AWS Lambda by default creates log streams in unmanaged CloudWatch log groups that retain data indefinitely, quietly creating runaway storage bills over months and years. Syncploy automatically synthesizes an `AWS::Logs::LogGroup` with `RetentionInDays: 14` for every Lambda endpoint, SQS consumer, and DynamoDB stream worker. You can customize the retention window per endpoint via `logRetentionInDays`, with zero manual CloudFormation LogGroup declarations. --- ## Container Packaging (`packageType: "Image"`) When you require custom native binaries, PyTorch models, or LibreOffice tools inside Lambda: ```typescript export const docxConverter = api( { path: "/convert", method: "POST", packageType: "Image", dockerfile: "./Dockerfile", memory: 3008, timeout: "120s", }, async (req) => { // Executes inside your custom Docker container return Response.json({ status: "converted" }); } ); ``` --- # Auth (Amazon Cognito) The `Auth` construct synthesizes `AWS::Cognito::UserPool` and `AWS::Cognito::UserPoolClient` with **Enterprise Security** defaults. ```typescript import { Auth } from "@syncploy/sdk"; export const ehrAuth = new Auth("ehr", { logicalId: "EhrUserPool", adminOnly: true, // Enforces admin-created accounts (blocks public self-registration) signInMethod: "email", // Validates email as primary username passwordPolicy: { minLength: 12, // HIPAA compliance standard requireUppercase: true, requireLowercase: true, requireNumbers: true, requireSymbols: true, }, client: { clientLogicalId: "EhrUserPoolClient", generateSecret: false, // SPA and Web App friendly explicitAuthFlows: [ "ALLOW_USER_SRP_AUTH", "ALLOW_REFRESH_TOKEN_AUTH", "ALLOW_USER_PASSWORD_AUTH", ], preventUserExistenceErrors: true, // Prevents email enumeration attacks }, }); ``` --- ## Automatic Environment Variable Injection When an `Auth` construct is declared in your project, the Syncploy compiler **automatically injects**: - `COGNITO_USER_POOL_ID: !Ref ` - `COGNITO_CLIENT_ID: !Ref ` - `COGNITO_USER_POOL_ARN: !GetAtt .Arn` into every synthesized Lambda function environment. You never have to manually wire CloudFormation references. --- ## Runtime Accessors ```typescript import { ehrAuth } from "./infra"; // Resolved seamlessly from injected environment variables: const poolId = ehrAuth.userPoolId; const clientId = ehrAuth.userPoolClientId; const arn = ehrAuth.userPoolArn; ``` --- ## Parameters Reference | Parameter | Type | Default | Description | | --- | --- | --- | --- | | `logicalId` | `string` | `"${name}UserPool"` | CloudFormation logical ID | | `adminOnly` | `boolean` | `false` | If `true`, blocks public self-registration | | `signInMethod` | `"email" | "username" | "phone"` | `"email"` | Primary login credential attribute | | `passwordPolicy.minLength` | `number` | `12` | Minimum password character length | | `client.preventUserExistenceErrors` | `boolean` | `true` | Returns generic errors to prevent user scraping | --- # Table (DynamoDB & ElectroDB) The `Table` construct provisions an `AWS::DynamoDB::Table` optimized for Single-Table Design with pay-per-request billing, point-in-time recovery, and stream processing. ```typescript import { Table } from "@syncploy/sdk"; export const ehrTable = new Table("ehr", { logicalId: "EhrTable", partitionKey: { name: "PK", type: "string" }, sortKey: { name: "SK", type: "string" }, billingMode: "PAY_PER_REQUEST", stream: "NEW_IMAGE", indexes: { GSI1: { partitionKey: { name: "GSI1PK", type: "string" }, sortKey: { name: "GSI1SK", type: "string" }, projection: "ALL", }, }, }); ``` --- ## ElectroDB First-Class Integration Syncploy has opinionated first-class support for [ElectroDB](https://electrodb.dev). When you define ElectroDB entities, Syncploy: 1. Automatically infers the table primary key attributes. 2. Combines and unions all GSIs across all entities in your service. 3. Validates schema consistency at compile-time to prevent conflicting index declarations. ```typescript import { Entity } from "electrodb"; import { DynamoDBClient } from "@aws-sdk/client-dynamodb"; export const PatientEntity = new Entity({ model: { entity: "patient", version: "1", service: "ehr" }, attributes: { patientId: { type: "string", required: true }, clinicId: { type: "string", required: true }, fullName: { type: "string", required: true }, }, indexes: { primary: { pk: { field: "PK", composite: ["clinicId"] }, sk: { field: "SK", composite: ["patientId"] } }, byPatient: { index: "GSI1", pk: { field: "GSI1PK", composite: ["patientId"] } }, }, }); ``` > [!TIP] > Syncploy detects `PatientEntity` and automatically provisions `EhrTable` with `PK`, `SK`, and `GSI1` without duplicate manual YAML declarations! --- ## Stream Processing & Concurrency Protection Connect an asynchronous Lambda stream worker directly to changes on the table: ```typescript export const auditStreamWorker = ehrTable.stream( { batchSize: 100, startingPosition: "LATEST", concurrency: 5, // Capped to prevent shard split DDoS attacks }, async (record) => { // Process DynamoDB stream event } ); ``` --- ## Runtime Client API ```typescript // Type-safe runtime operations: await ehrTable.put({ PK: "CLINIC#1", SK: "DOC#10", status: "PENDING" }); const item = await ehrTable.get({ PK: "CLINIC#1", SK: "DOC#10" }); await ehrTable.delete({ PK: "CLINIC#1", SK: "DOC#10" }); ``` --- ## Denial-of-Wallet Immunity: No Table Scans Never invoke `.scan()` on DynamoDB tables inside HTTP request handlers or API endpoints. Full-table scans consume Read Capacity Units linearly proportional to the size of your entire table ($O(N)$ read amplification), causing astronomical bills under high query load. Syncploy's pre-flight AST analyzer detects `.scan()` calls in API endpoints and raises warning `WARN_DOW_DYNAMODB_SCAN`. Always query using partition keys, sort key conditions, or ElectroDB secondary indexes. --- # Bucket (Amazon S3) The `Bucket` construct provisions secure, enterprise-grade Amazon S3 object storage with enforced encryption and public access blocks. ```typescript import { Bucket } from "@syncploy/sdk"; // Standard medical document bucket with EventBridge notifications export const documentsBucket = new Bucket("documents", { logicalId: "DocumentsBucket", eventBridge: true, publicAccessBlock: true, }); // HIPAA compliance audit bucket with immutable Object Lock export const auditLogsBucket = new Bucket("audit-logs", { logicalId: "EhrAuditLogsBucket", accountScoped: true, objectLock: { mode: "COMPLIANCE", days: 365, }, publicAccessBlock: true, }); ``` --- ## Event Routing & Notifications Route S3 file upload events directly to an SQS queue with prefix and suffix filters: ```typescript import { annotationQueue } from "./queue"; documentsBucket.on("objectCreated", annotationQueue, { prefix: "clinical-notes/", suffix: ".pdf", }); ``` --- ## Runtime Client API ```typescript // Read and write text: await documentsBucket.putText("notes/patient-1.txt", "Blood pressure: 120/80"); const text = await documentsBucket.getText("notes/patient-1.txt"); // Read and write binary buffers: await documentsBucket.putBuffer("scans/xray.png", imageBuffer, "image/png"); const buffer = await documentsBucket.getBuffer("scans/xray.png"); // List files: const files = await documentsBucket.list({ prefix: "notes/" }); ``` --- ## Automatic Denial-of-Wallet Storage Protection When uploading large files in multi-part chunks via SDK or presigned URLs, network dropouts or abandoned client uploads leave orphan byte fragments in S3. These incomplete multipart uploads do not show up in regular bucket listings, yet AWS bills for their storage indefinitely. Syncploy automatically synthesizes an `AbortIncompleteMultipartUpload` lifecycle rule (`DaysAfterInitiation: 7`) on every declared bucket, ensuring abandoned upload fragments are purged without manual intervention. --- # Queue & DLQ (Amazon SQS) The `Queue` construct provisions resilient message queues with automatic Dead-Letter Queue (DLQ) redrive configurations. ```typescript import { Queue } from "@syncploy/sdk"; export const annotationQueue = new Queue("annotations", { visibilityTimeout: "180s", deadLetterQueue: { maxReceiveCount: 3, logicalId: "AnnotationDLQ", }, }); ``` --- ## Connecting Consumers Attach an asynchronous Lambda consumer with automatic IAM policy synthesis and batch management: ```typescript export const annotationWorker = annotationQueue.consumer( { batchSize: 10, timeout: "30s", // Must be strictly <= 1/6 visibilityTimeout concurrency: 5, // Reserved concurrency cap }, async (batch) => { for (const message of batch) { console.log("Processing message:", message.body); } } ); ``` --- ## The SQS 6x Visibility Formula > [!IMPORTANT] > AWS strongly recommends that SQS `visibilityTimeout` be at least **6 times** your consumer Lambda `timeout`. If a consumer takes 30s to process a batch and the queue visibility timeout is only 40s, a minor processing hiccup will release the message back into the queue while the original Lambda is still running, triggering duplicate executions and database collisions. Syncploy audits this at compile-time and **halts compilation** if `visibilityTimeout < consumer.timeout`. --- ## Runtime Client API ```typescript // Send a single message await annotationQueue.send({ patientId: "123", documentId: "456" }); // Send a batch of messages in one request await annotationQueue.sendBatch([ { id: "1", body: { patientId: "123" } }, { id: "2", body: { patientId: "124" } }, ]); ``` --- # Topic & Secret (SNS & Secrets) Manage pub/sub broadcast notifications with `Topic` and secure environment secrets with `Secret`. --- ## `Topic` (Amazon SNS) Provision pub/sub messaging channels with multi-destination fanout: ```typescript import { Topic, Queue } from "@syncploy/sdk"; export const alertTopic = new Topic("alerts", { logicalId: "SystemAlertsTopic", }); // Subscribe an SQS queue: export const notificationQueue = new Queue("notifications"); alertTopic.subscribe(notificationQueue); // Subscribe a Lambda handler directly: alertTopic.subscribe(async (event) => { console.log("Alert received:", event.message); }); ``` --- ## `Secret` (AWS Secrets Manager) Reference sensitive credentials without hardcoding plaintext tokens in your repository: ```typescript import { Secret, api } from "@syncploy/sdk"; export const stripeKey = new Secret("STRIPE_SECRET_KEY"); export const paymentWebhook = api({ path: "/webhook", method: "POST" }, async (req) => { // Secret is securely read from environment at runtime: const key = stripeKey.value; return Response.json({ success: true }); }); ``` > [!WARNING] > Syncploy's compiler inspects all environment variables and flags literal secrets (like `sk_live_...` or `ghp_...`) as fatal pre-flight build errors. --- ## `Budget` (AWS Budgets Financial Guardrail) Declare financial spend limits with automated CloudWatch and SNS alerts to guarantee total stack compute and storage liability never exceeds your financial appetite: ```typescript import { Budget, Topic } from "@syncploy/sdk"; export const alertTopic = new Topic("alerts", { logicalId: "SystemAlertsTopic", }); // Hard budget guardrail: alerts via SNS when monthly spend reaches 80% of $50 export const monthlyBudget = new Budget("monthly-cap", { amount: 50, // $50.00 USD monthly limit unit: "USD", threshold: 80, // Alert at 80% ($40) topic: alertTopic, filterByStack: true, // Scoped to this CloudFormation stack }); ``` Syncploy's AST compiler statically validates that: - The budget amount is a static positive integer or float (`ERR_BUDGET_INVALID_AMOUNT`). - The alert topic resolves to a declared SNS Topic construct in the stack (`ERR_BUDGET_UNRESOLVED_TOPIC`). - Stacks with high continuous compute liability ($> \$100/\text{mo}$) declare a Budget to prevent unmonitored overruns (`WARN_DOW_HIGH_BLAST_RADIUS`). --- # Safety Rails & Denial-of-Wallet Shield The primary reason teams pay a 10x–50x PaaS tax on Vercel or Render isn't developer ergonomics—it is **fear of an unbounded AWS bill**. On standard AWS (raw CDK, SAM, or Serverless Framework), concurrency defaults to account-wide limits (1,000+ instances), event streams retry for 24 continuous hours without dead-letter queues, and log groups never expire. A single accidental stream loop or public scraping flood can burn a $10,000+ credit limit overnight while CloudFormation deploys without a single warning. Syncploy embeds a **Pre-Flight Safety Engine** directly into the compiler. In $<1\text{ms}$ during Abstract Syntax Tree (AST) parsing, Syncploy models the infrastructure topology as a directed graph, verifies mathematical blast-radius bounds, detects event cycles, and halts compilation before any resources can be synthesized or deployed. --- ## 1. Why Runtime Billing Alarms Fail Most engineering teams rely on AWS CloudWatch Billing Alarms or AWS Budgets. These are **post-mortem notifications**, not preventative guardrails: | Feature | AWS CloudWatch Billing Alarm | Syncploy Pre-Flight Shield | | :--- | :--- | :--- | | **Enforcement Point** | Post-execution (15 to 45 min delay) | **Pre-flight AST compilation (<1ms)** | | **Mitigation Action** | Sends an email or SNS alert | **Aborts compilation; 0 resources synthesized** | | **Protection While Sleeping** | ❌ None. Bill continues to surge until human intervention | **✅ Total. Unbounded code cannot deploy to AWS** | | **Native Circuit Breaker** | ❌ None. AWS provides no native auto-shutdown | **✅ Mathematical. Hardware compute liabilities hard-capped** | > [!WARNING] > AWS has no native "spend circuit breaker" for Lambda. When an account-level CloudWatch billing alarm triggers, it only dispatches a notification. Without custom auto-reaper automation, runaway Lambdas and unthrottled endpoints continue processing and billing at full velocity. --- ## 2. The Mathematical Blast-Radius Guarantee Denial-of-Wallet cannot be solved with rate limiting alone—rate limiters can be bypassed, spoofed, or misconfigured. True financial immunity requires **mathematically bounding hardware liabilities in code**. Even if an unauthenticated endpoint is targeted by an unmitigated 500,000,000-request DDoS flood, its monthly compute spend cannot physically exceed the ceiling of its assigned resources: $$\text{Max Monthly Compute} = \text{Concurrency} \times \frac{\text{Memory MB}}{1024} \times 2,592,000\text{s} \times \$0.0000166667$$ ### Hardware Ceiling Reference | Reserved Concurrency | Memory Allocation | Worst-Case Sustained 24/7 Spend | Standard AWS Default (1,000 Concurrency) | | :---: | :---: | :---: | :---: | | **2** | 128 MB | **$10.80 / mo** | $5,400.00 / mo | | **5** | 256 MB | **$54.00 / mo** | $10,800.00 / mo | | **10** | 512 MB | **$216.00 / mo** | $21,600.00 / mo | | **25** | 1024 MB | **$1,080.00 / mo** | $43,200.00 / mo | Syncploy rule `WARN_DOW_UNRESERVED_CONCURRENCY` statically requires an explicit concurrency cap on all public Function URLs and API routes: ```typescript // ❌ HAZARDOUS (Standard AWS): Scales to 1,000+ account instances under DDoS export const webhook = api.post("/webhook", async (req) => { return { status: "received" }; }); // ✅ IMMUNIZED (Syncploy): Hardware-bounded to $27.00/mo under 24/7 sustained DDoS export const webhook = api.post("/webhook", { concurrency: 5, memory: "128MB" }, async (req) => { return { status: "received" }; }); ``` --- ## 3. Tarjan's SCC Infinite Loop Killer In event-driven architectures, **infinite recursion loops** are the #1 cause of five-figure cloud bills ($10,000+ overnight charges). A classic failure occurs when a Lambda consumes a DynamoDB Stream and writes back to that same table. Each write produces a new stream event, creating a continuous, self-amplifying execution avalanche: ``` ┌────────────────────────────────────────────────────────┐ │ INFINITE RECURSION CYCLE │ │ │ │ Table 'orders' ──(Stream Event)──► worker Lambda │ │ ▲ │ │ │ └───────────(table.put)──────────┘ │ └────────────────────────────────────────────────────────┘ ``` Multi-hop cycles are equally catastrophic and virtually impossible to catch in manual code review: $$\text{Table A} \longrightarrow \text{Worker 1} \longrightarrow \text{Queue B} \longrightarrow \text{Worker 2} \longrightarrow \text{Table A}$$ ### How the Compiler Traps Loops in <1ms 1. **Trigger Edges**: Compiler maps event sources (`Table.stream`, `Queue.onMessage`, `Bucket.onObjectCreated`) to consumer Lambdas. 2. **Mutation Edges**: Compiler inspects function bodies for mutation calls (`table.put`, `table.update`, `queue.send`, `bucket.put`, `topic.publish`, HTTP fetch self-calls). 3. **Graph Construction**: Constructs a directed bipartite graph $G = (V, E)$ where vertices are AWS constructs and edges are async mutations. 4. **Tarjan's SCC Algorithm**: Executes Tarjan's Strongly Connected Components algorithm in $O(V + E)$ linear time ($<1\text{ms}$). 5. **Immediate Compilation Abort**: ```text ✖ [ERR_RECURSIVE_LOOP] in src/workers/order-sync.ts: • Infinite recursion loop detected: Table 'orders' ──► Stream Consumer 'syncOrders' ──► Table 'orders' • Cycle triggers self-amplifying Lambda executions, scaling to maximum account concurrency within seconds. ✖ Compilation aborted. 0 AWS resources modified. ``` --- ## 4. The 4 Defensive Layers Syncploy's pre-flight compiler runs 26 deterministic safety rules across 4 operational layers: ### Layer 1: Compute Blast-Radius Bounding - **Strict Concurrency Ceilings**: Public Function URLs and API routes must declare explicit concurrency bounds (`WARN_DOW_UNRESERVED_CONCURRENCY`). - **Stream Throttling**: DynamoDB stream consumers are auto-bounded to prevent runaway concurrent batch workers (`WARN_DOW_STREAM_UNBOUNDED`). - **Account Quota Sentinel**: Aggregates reserved concurrency across the entire stack, warning if total $\ge 900$ to prevent starvation of critical services (`WARN_DOW_CONCURRENCY_BUDGET_EXCEEDED`). ### Layer 2: Event-Driven Recursion & Poison-Pill Shields - **Tarjan's SCC Cycle Detection**: Statically identifies 1-hop and multi-hop recursive cycles across tables, queues, and streams (`ERR_RECURSIVE_LOOP`). - **SQS 6× Timeout Invariant**: Enforces that queue `visibilityTimeout` is $\ge 6 \times$ consumer `timeout` to eliminate duplicate processing storms (`ERR_SQS_TIMEOUT_MISMATCH`, `WARN_SQS_VISIBILITY_TIMEOUT_SHORT`). - **Mandatory Dead Letter Queues (DLQs)**: Active queues and stream consumers require DLQs (`maxReceiveCount: 3`) to isolate poison-pill records instead of retrying for days (`WARN_DOW_MISSING_DLQ`). - **No Table Scans**: Detects `$O(N)$` full-table scans in request paths to prevent crawling bots from draining Read Capacity Units (`WARN_DOW_DYNAMODB_SCAN`). ### Layer 3: Storage & Log Leak Neutralization - **Automatic 14-Day CloudWatch Log Rotation**: Auto-synthesizes dedicated `AWS::Logs::LogGroup` resources with `RetentionInDays: 14` for every Lambda. Eliminates default "Never Expire" log accumulation bills (`INFO_DOW_LOG_ROTATION`). - **Automatic S3 Multipart Abort**: Attaches `AbortIncompleteMultipartUpload: { DaysAfterInitiation: 7 }` to all buckets to purge orphan upload chunks (`INFO_DOW_S3_MULTIPART_ABORT`). ### Layer 4: Financial Governance, Fraud Defense & Data Safety - **AWS Budgets Ceiling**: Validates `new Budget(...)` constructs and ensures SNS alert topics are wired to real notification targets (`WARN_DOW_BUDGET_NO_NOTIFICATION`). - **SMS International Toll Fraud Shield**: Warns against unmonitored SNS SMS subscriptions and Cognito phone verification endpoints (`WARN_DOW_SMS_TOLL_FRAUD`). - **Zero Data-Loss Replacement Gatekeeper**: `syncploy diff --check-replacement` verifies that no stateful resource (DynamoDB table, S3 bucket) will be destroyed during updates (`ERR_STATEFUL_REPLACEMENT`). --- ## 5. Diagnostic Suppression & CI/CD Auditing In rare cases where recursion is intentionally bounded by application logic (such as a finite state-machine transition in DynamoDB), diagnostics can be suppressed with an explicit inline reason: ```typescript // syncploy-ignore ERR_RECURSIVE_LOOP reason="State machine terminates on status: 'COMPLETED'" export const stateWorker = patientTable.stream({ batchSize: 10 }, async (record) => { if (record.status !== "COMPLETED") { await patientTable.put({ ...record, status: "COMPLETED" }); } }); ``` Every suppressed diagnostic is verified during `syncploy build` and printed as an explicit audit trail in your CI/CD pipeline: ```text [AUDIT] 1 diagnostic suppressed: • ERR_RECURSIVE_LOOP in src/workers/stateWorker.ts: State machine terminates on status: 'COMPLETED' ``` --- # Security, Supply Chain & Trust Guarantees In modern software development, engineering teams and enterprise security reviewers are rightfully vigilant about third-party frameworks, CLI tools, and npm packages. Supply chain attacks, rogue `postinstall` scripts, and silent telemetry tracking are rampant. Syncploy is engineered from the ground up under a strict **Zero-Trust Architecture**. It operates as an offline static compiler that respects developer privacy, emits 100% transparent AWS CloudFormation, and guarantees zero supply-chain vulnerabilities. --- ## 🛡️ The 5 Pillars of Syncploy Security | Pillar | Guarantee | Technical Proof | | :--- | :--- | :--- | | **1. Zero Telemetry & 100% Offline** | Never phones home; collects no user code, AST metrics, or environment variables. | Verified in CI via static regex audit; 0 network calls in compiler/runtime. | | **2. Zero Lifecycle Scripts** | Cannot execute arbitrary shell commands upon `npm install` or `bun add`. | Audited in CI; zero `postinstall`, `preinstall`, or `install` scripts in any package. | | **3. Zero Credential Interception** | AWS credentials and cloud traffic never pass through Syncploy servers. | Local-only AST analysis; handler code deploys directly to your own AWS account. | | **4. Official AWS SDK Only** | No risky open-source micro-packages or unverified transitive dependencies. | Runtime client imports exclusively official `@aws-sdk/*` libraries maintained by Amazon. | | **5. Continuous CI Security Auditing** | Every pull request and release is cryptographically and structurally audited. | Automated GitHub Actions assert `publint`, secret leak scanning, and SHA-256 integrity. | --- ## 1. 100% Offline Compilation (Zero Telemetry) Many modern developer tools collect anonymized telemetry, tracking CLI usage, repository names, and code structures. **Syncploy collects zero telemetry:** - The AST analysis engine runs entirely in-memory on your local CPU in $<1\text{ms}$. - No usage data, repository metadata, or synthesized template structures are ever transmitted over the network. - You can run `bunx syncploy build` in a completely air-gapped, isolated network environment with zero internet access. --- ## 2. Zero Lifecycle Scripts (Malware Immunity) The vast majority of npm supply chain attacks rely on npm lifecycle hooks—specifically `postinstall` and `preinstall` scripts—to execute malicious shell payloads, scan environment variables, or install hidden backdoors upon package installation. `@syncploy/sdk`, `@syncploy/compiler`, and the `syncploy` CLI contain **zero lifecycle scripts**. When your CI pipeline or local machine executes: ```bash bun add @syncploy/sdk # or: npm install @syncploy/sdk ``` The package manager strictly unpacks TypeScript and JavaScript distribution files. It does not spawn a child shell, does not execute arbitrary binaries, and cannot tamper with your local filesystem. This invariant is verified on every pull request via our automated CI test: ```bash bun run audit:security # Asserts: Zero dangerous lifecycle hooks found across all packages. ``` --- ## 3. Zero Credential Exposure & Zero Traffic Interception Traditional PaaS providers and deployment platforms require you to hand over administrative AWS IAM credentials, OAuth tokens, or route production HTTP traffic through their proprietary edge proxies. Syncploy is **not an intermediary service**: - **Direct CloudFormation Output:** Syncploy compiles application code down to standard, 1:1 vanilla AWS CloudFormation JSON templates (`.syncploy/template.json`). - **Standard AWS SDK Transport:** At runtime, Syncploy constructs invoke AWS services using direct, official HTTPS endpoints (`dynamodb..amazonaws.com`, `sqs..amazonaws.com`). - **No Third-Party Relay:** Your application data and credentials communicate directly between your users and your AWS account with zero middleware in between. --- ## 4. Curated, Enterprise-Grade Dependencies Supply chain vulnerabilities typically enter through deep, neglected dependency trees. The `@syncploy/sdk` runtime package intentionally minimizes external dependencies: - **Zero utility bloat:** No `lodash`, `left-pad`, or unmaintained helper libraries. - **Strictly Official AWS Clients:** - `@aws-sdk/client-dynamodb` - `@aws-sdk/client-s3` - `@aws-sdk/client-sns` - `@aws-sdk/client-sqs` - `@aws-sdk/lib-dynamodb` All dependencies are maintained directly by the official Amazon Web Services engineering team, carrying enterprise-grade security and vulnerability response SLAs. --- ## 5. Automated CI Supply-Chain Gatekeepers Every commit pushed to the Syncploy repository triggers an automated security gatekeeper workflow in GitHub Actions: 1. **Lifecycle Script Verification:** Recursively inspects every `package.json` to block the introduction of any installation hooks. 2. **Credential Leak Scanning:** Scans all source trees and distribution files for AWS Access Key patterns (`AKIA...`), session tokens, and PEM private keys. 3. **Export Integrity Audits (`publint`):** Validates that all ESM `exports` and TypeScript declaration mappings strictly follow Node.js and web standards. 4. **Tarball Content Validation (`npm pack --dry-run`):** Guarantees that internal test suites, environment variables (`.env`), and secret keys never leak into published npm releases. 5. **Cryptographic Release Hashes:** Every official release generates a deterministic SHA-256 hash published directly to GitHub Release assets. --- ## 🔗 Verification Resources - **GitHub Actions Security Pipeline:** [github.com/harsh-vardhhan/syncploy/actions](https://github.com/harsh-vardhhan/syncploy/actions) - **Latest Release Assets & SHA-256:** [github.com/harsh-vardhhan/syncploy/releases](https://github.com/harsh-vardhhan/syncploy/releases) - **Live AI Specification:** [syncploy.com/llms.txt](https://www.syncploy.com/llms.txt) - **Responsible Disclosure:** If you discover a potential vulnerability, please email `harsh.vardhan.611@gmail.com` for coordinated disclosure. --- # Least-Privilege IAM Policy Generator Configuring IAM deployment permissions for CI/CD runners (like GitHub Actions) is one of the most frustrating parts of AWS engineering: - Granting `AdministratorAccess` violates compliance standards (HIPAA, SOC2, ISO27001). - Crafting policies by hand leads to hours of trial-and-error `403 AccessDenied` errors. Syncploy solves this with the `syncploy policy` command. --- ## How It Works Because Syncploy compiles your TypeScript application into an exact CloudFormation template, it knows **every single AWS action and resource ARN** your deployment will ever touch. ```bash syncploy policy . -s ehr-annotation-backend -o deploy-policy.json ``` --- ## Key Synthesis Features 1. **Dead Service Pruning**: If your code doesn't use API Gateway or CloudFront, permissions for those services are never generated. 2. **Strict ARN Scoping**: Scopes resource permissions down to your exact account, region, and stack name (`arn:aws:cognito-idp:ap-south-1::userpool/*`). 3. **Shadow Action Resolution**: Injects all CloudFormation background actions (such as `cognito-idp:TagResource`, `s3:PutBucketPolicy`) that AWS requires CloudFormation to invoke under the hood. --- ## Example Generated Policy ```json { "Version": "2012-10-17", "Statement": [ { "Sid": "CognitoUserPoolManagement", "Effect": "Allow", "Action": [ "cognito-idp:CreateUserPool", "cognito-idp:DescribeUserPool", "cognito-idp:TagResource", "cognito-idp:ListTagsForResource" ], "Resource": "arn:aws:cognito-idp:ap-south-1::userpool/*" } ] } ``` --- # Brownfield Adoption & Diff Migrating existing production applications to an Infrastructure-from-Code framework is historically terrifying: if a framework alters a table's logical ID or physical name, CloudFormation will delete the existing table and recreate an empty one, wiping out production data. Syncploy guarantees **Zero Stateful Replacements**. --- ## Step 1: Automatic Adoption (`syncploy adopt`) Generate type-safe TypeScript infrastructure directly from your existing SAM or CloudFormation template: ```bash syncploy adopt template.yaml -c samconfig.toml -o src/infra.ts ``` Syncploy automatically: - Preserves all logical IDs (e.g. `logicalId: "EhrTable"`). - Matches exact partition and sort key schemas. - Preserves parameter overrides from `samconfig.toml`. --- ## Step 2: Pre-Flight Verification (`syncploy diff`) Before applying changes, verify 100% equivalence: ```bash syncploy diff . -a template.yaml --check-replacement ``` Output preview: ```text 🔍 Comparing synthesized template against template.yaml... Infrastructure Diff Preview: = [IN_SYNC] EhrTable (AWS::DynamoDB::Table) = [IN_SYNC] EhrUserPool (AWS::Cognito::UserPool) = [IN_SYNC] EhrUserPoolClient (AWS::Cognito::UserPoolClient) ~ [UPDATE] EhrBackendFunction (AWS::Serverless::Function -> AWS::Lambda::Function) • Migrated from Serverless macro to pure CloudFormation function Diff Summary: 0 to replace | 0 data-loss risks | 100% template equivalence verified ``` > [!IMPORTANT] > The `--check-replacement` flag exits with error code 1 if any stateful resource (Table, Bucket, UserPool) would be replaced, making it an ideal pre-merge CI/CD gatekeeper. --- # In-Memory Zero-Mock Testing Testing serverless applications typically requires either complex Docker setups (like LocalStack) or brittle mock objects (`aws-sdk-mock`). Syncploy includes an **in-memory simulated runtime harness** directly inside `@syncploy/sdk/testing`. --- ## Testing API Handlers ```typescript import { test, expect } from "bun:test"; import { uploadAnnotation } from "../src/app"; import { patientTable, processingQueue } from "../src/infra"; test("creates patient annotation and enqueues task", async () => { // Reset in-memory state: patientTable.clear(); processingQueue.clear(); // Invoke the API handler directly with a Fetch Request: const request = new Request("https://api.internal/annotations", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ patientId: "P-100", notes: "Normal ECG" }), }); const response = await uploadAnnotation(request); const data = await response.json(); expect(response.status).toBe(200); expect(data.status).toBe("queued"); // Verify state persisted in-memory: const items = await patientTable.query({ PK: "PATIENT#P-100" }); expect(items.length).toBe(1); // Verify queue received the message: const messages = processingQueue.getInFlightMessages(); expect(messages.length).toBe(1); expect(messages[0].body.patientId).toBe("P-100"); }); ``` > [!TIP] > In-memory tests run in under **5 milliseconds** with zero external network calls or cloud credentials. --- # CLI Command Reference The `syncploy` CLI is the central operational tool for compilation, diffing, adoption, and policy generation. --- ## 1. `syncploy build` Compiles your TypeScript application into CloudFormation templates and bundles Lambda assets. ```bash syncploy build [dir] [options] ``` ### Options - `[dir]`: Root directory of your TypeScript project (default: `.`). - `-o, --out `: Output directory for synthesized templates and assets (default: `.syncploy`). - `--minify`: Minifies bundled JavaScript assets with `esbuild`. - `--sourcemap`: Generates source maps for Lambda error stack traces. --- ## 2. `syncploy diff` Compares synthesized CloudFormation templates against an existing template or deployed AWS stack. ```bash syncploy diff [dir] [options] ``` ### Options - `-a, --against `: Path to existing template (e.g. `template.yaml` or `template.json`). - `--check-replacement`: **Critical CI flag.** Returns exit code 1 if any stateful resource would be replaced. - `--json`: Formats diff preview as structured JSON. --- ## 3. `syncploy adopt` Adopts an existing AWS SAM or CloudFormation template into clean TypeScript code. ```bash syncploy adopt [options] ``` ### Options - `-o, --out `: Path to write generated TypeScript file (default: `src/infra.ts`). - `-c, --config `: Path to `samconfig.toml` to automatically adopt stack parameters. --- ## 4. `syncploy policy` Synthesizes a minimal, least-privilege IAM deployment policy for CI/CD runners. ```bash syncploy policy [dir] [options] ``` ### Options - `-s, --stack-name `: Target CloudFormation stack name (**required**). - `-r, --region `: AWS region (default: `ap-south-1`). - `-c, --compare `: Audits an existing policy JSON file against optimal compiler policy. - `-o, --out `: Writes policy JSON to disk (e.g. `deploy-policy.json`). --- # Case Study: EHR Annotation Platform Migration > **11 AWS Services • Single-Table Clinical DynamoDB • 7-Year WORM Compliance • Proven in Live** Syncploy has been proven in live healthcare infrastructure with the **[EHR Annotation Platform](https://ehr-backend-frontend.vercel.app/)** ([GitHub: harsh-vardhhan/EHR-backend](https://github.com/harsh-vardhhan/EHR-backend))—a HIPAA-compliant Electronic Health Record clinical annotation platform and OMOP Common Data Model (CDM) lakehouse—was fully migrated from a 600+ line legacy AWS SAM template to Syncploy Infrastructure-from-Code with **zero downtime, zero stateful resource recreation, and zero data loss**. > [!NOTE] > The complete codebase, TypeScript infrastructure definitions, and GitHub Actions deployment pipelines are open-source and verifiable at [harsh-vardhhan/EHR-backend](https://github.com/harsh-vardhhan/EHR-backend). --- ## 🌐 Live Verification & Deployment The migrated system is actively handling live clinical workloads in AWS Region `ap-south-1` (Mumbai): - **Live Web Application**: [`https://ehr-backend-frontend.vercel.app/`](https://ehr-backend-frontend.vercel.app/) - **CloudFormation Stack**: `ehr-annotation-backend` - **Active Lambda Endpoint**: [`https://ofxtuegfgi6kqg4wfzfyuqu6be0mubmk.lambda-url.ap-south-1.on.aws/`](https://ofxtuegfgi6kqg4wfzfyuqu6be0mubmk.lambda-url.ap-south-1.on.aws/) - **Live Verified Endpoints**: - `GET /` → `HTTP 200 OK` (`{"status":"ok","framework":"elysia"}`) - `GET /documents` → `HTTP 200 OK` (retrieves clinical encounters from DynamoDB `EhrTable`) - `GET /documents/doc-001` → `HTTP 200 OK` (retrieves S3 document text, 10 named clinical entities, and relationship graph) --- ## 🏗️ Interactive Cloud Architecture Flowchart The entire 11-service cloud topology is declared in TypeScript, statically analyzed by Syncploy, and synthesized 1:1 into CloudFormation: ```mermaid flowchart TD subgraph Ingress["1. Ingress & Authentication"] Client["Clinician Web Portal"] Cognito["Amazon Cognito UserPool
EhrUserPool (SRP Invariants)"] end subgraph Compute["2. Elysia.js Backend Compute"] FUrl["AWS Lambda Function URL
EhrBackendFunction (Bun/Elysia)"] KillSwitch["DDoS Circuit Breaker
EhrLambdaKillSwitchFunction"] end subgraph Storage["3. Stateful Clinical Storage (0-Replacement)"] Dynamo[("DynamoDB Single-Table
EhrTable (PK, SK, GSIs)")] S3Docs[("S3 Medical Documents
DocumentsBucket (SSE-S3)")] end subgraph AsyncPipeline["4. Asynchronous Clinical Extraction"] SQS["SQS Processing Queue
EhrAnnotationQueue"] DLQ["SQS Dead Letter Queue
EhrAnnotationDLQ"] Worker["Clinical NER Lambda
EhrAnnotationWorkerFunction"] SageMaker["AWS SageMaker Endpoint
GLiNER ReLex Model"] end subgraph CompliancePipeline["5. HIPAA Audit & OMOP Data Lake"] DDBStream["DynamoDB Streams
(NEW_IMAGE)"] FirehoseAudit["Kinesis Firehose
EhrAuditDeliveryStream"] FirehoseOmop["Kinesis Firehose
EhrOmopDeliveryStream"] S3Audit[("S3 HIPAA Audit Vault
EhrAuditLogsBucket (ObjectLock WORM)")] S3Omop[("OMOP CDM Data Lake
Parquet Clinical Lakehouse")] end subgraph SafetyRails["6. Denial-of-Wallet Shield"] CWAlarm["CloudWatch Metric Math
EhrLambdaTrafficAlarm (>2000 req/min)"] SNSTopic["SNS Alert Topic
EhrKillSwitchTopic"] end Client -->|1. Authenticate SRP| Cognito Client -->|2. HTTPS REST| FUrl FUrl -->|3. Query Patient Encounters| Dynamo FUrl -->|4. Store Clinical Documents| S3Docs S3Docs -->|5. ObjectCreated Event| SQS SQS -->|6. Batch Poll 180s Timeout| Worker SQS -.->|Failed 3x Retries| DLQ Worker -->|7. Infer Named Entities| SageMaker Worker -->|8. Save Entity Graph| Dynamo Dynamo -->|9. Mutation Stream| DDBStream DDBStream -->|10. Stream Ingestion| FirehoseAudit DDBStream -->|11. Extract OMOP Entities| FirehoseOmop FirehoseAudit -->|12. 900s GZIP Buffer| S3Audit FirehoseOmop -->|13. 900s GZIP Buffer| S3Omop FUrl -.->|Track Invocations + Throttles| CWAlarm CWAlarm -->|Trigger on DDoS Spikes| SNSTopic SNSTopic -->|Trigger Emergency Shutdown| KillSwitch KillSwitch -->|Set Concurrency = 0| FUrl ``` --- ## ⚡ The Stakes: Why Healthcare IaC Cannot Afford Drift In clinical medicine, infrastructure errors aren't just inconveniences—they carry legal and patient-safety ramifications under **HIPAA §164.312 Technical Safeguards**: 1. **Stateful Table Destruction**: The `EhrTable` holds composite partition keys (`PK: PATIENT#`, `SK: ENCOUNTER#`) and secondary clinical indexes. Accidental table replacement causes irreversible loss of patient medical histories. 2. **Compliance WORM Deletion**: The `EhrAuditLogsBucket` is locked in `COMPLIANCE` mode with a mandatory retention policy. Attempting to modify or replace this bucket triggers CloudFormation update errors and regulatory audits. 3. **Denial-of-Wallet (DoW) Runaway**: A burst of medical documents processed through SageMaker GPU endpoints without strict queue visibility timeouts and concurrency bounds can easily cost thousands of dollars in minutes. --- ## 🛡️ The 3 Disasters Prevented by Syncploy Compiler Safety Rails ### Disaster 1: The SAM Macro Replacement Trap In AWS SAM, writing `AWS::Serverless::Function` or `AWS::Serverless::SimpleTable` invokes a hidden CloudFormation transformation macro (`AWS::Serverless-2016-10-31`). During stack updates, the macro can subtly alter logical names or default properties, triggering a **`Replacement: True`** change. **How Syncploy Saved It:** Syncploy's pre-flight diff engine matches existing resources against the live CloudFormation ChangeSet: ```bash syncploy diff --against template.yaml --check-replacement ``` ```text Resource Changes: ~ [UPDATE] EhrBackendFunction (AWS::Serverless::Function -> AWS::Lambda::Function) ~ [UPDATE] EhrAnnotationWorkerFunctionRole (AWS::IAM::Role) = [EQUAL] EhrTable (AWS::DynamoDB::Table) = [EQUAL] DocumentsBucket (AWS::S3::Bucket) = [EQUAL] EhrAuditLogsBucket (AWS::S3::Bucket) Diff Summary: + 0 to create | ~ 3 to update | ! 0 to replace | - 0 to delete | 9 unchanged ✔ Pre-flight safety check PASSED: Zero stateful resources will be replaced. ``` The `--check-replacement` flag guarantees that if any stateful store would be replaced, CI/CD aborts immediately before any cloud changes occur. --- ### Disaster 2: The SQS Consumer Timeout Deadlock AWS mandates that an SQS queue's `visibilityTimeout` must be at least **6 times** the consuming Lambda's timeout. In the legacy SAM template, developer edits had drifted `visibilityTimeout` down to 120s while the worker timeout was 120s—causing duplicate message processing and conflicting SageMaker extractions. **How Syncploy Caught It at Compile-Time:** When compiling `packages/workers/src/infra.ts`, Syncploy's static validator caught the issue immediately: ```text [WARN_SQS_VISIBILITY_TIMEOUT_SHORT] in packages/shared/src/infra.ts • Queue 'ehr_annotation' visibilityTimeout (180s) is close to consumer timeout (120s). AWS recommends visibilityTimeout >= 6x consumer timeout to prevent duplicate invocations. ``` The developer fixed it with one line in TypeScript: `visibilityTimeout: "180s"`. --- ### Disaster 3: The Wildcard CI/CD Security Leak Under SAM, developers routinely attach `AdministratorAccess` or broad wildcard policies (`s3:*`, `dynamodb:*`, `iam:*`) to GitHub Actions runners because manually calculating CloudFormation IAM requirements is notoriously complex. **How Syncploy Automated Least-Privilege:** By running `syncploy policy`, the compiler inspected the synthesized AST and emitted a precise, stack-scoped `deploy-policy.json` for GitHub Actions: ```bash syncploy policy --stack-name ehr-annotation-backend --region ap-south-1 --out deploy-policy.json ``` It synthesized only the exact actions required: - `iam:UpdateAssumeRolePolicy` - `iam:UpdateRole` - `sagemaker:InvokeEndpoint` - `firehose:PutRecordBatch` - Stack-scoped `cloudformation:*` --- ## 🛠️ Infrastructure-from-Code: The TypeScript Declarations Here is how the clinical infrastructure is defined with 100% type safety and zero YAML: ```typescript // packages/shared/src/infra.ts import { Table, Bucket, Queue, Topic, Alarm, DeliveryStream, Auth } from "@syncploy/sdk"; // 1. Single-Table DynamoDB with zero stateful replacement export const ehrTable = new Table("ehr", { logicalId: "EhrTable", tableName: process.env.EHR_TABLE_NAME || "ehr-table", deletionPolicy: "Retain", partitionKey: { name: "PK", type: "string" }, sortKey: { name: "SK", type: "string" }, billingMode: "PAY_PER_REQUEST", stream: "NEW_IMAGE", indexes: { SKIndex: { partitionKey: { name: "SK", type: "string" }, sortKey: { name: "PK", type: "string" }, projection: "ALL" }, GSI1Index: { partitionKey: { name: "GSI1PK", type: "string" }, sortKey: { name: "GSI1SK", type: "string" }, projection: "ALL" }, }, }); // 2. S3 Bucket with HIPAA Object Lock (COMPLIANCE mode, account-scoped) export const ehrAuditLogsBucket = new Bucket("audit_logs", { logicalId: "EhrAuditLogsBucket", bucketName: "${AWS::StackName}-audit-logs-${AWS::AccountId}", deletionPolicy: "Retain", objectLock: { mode: "COMPLIANCE", days: 1 }, publicAccessBlock: true, accountScoped: true, }); // 3. SQS Annotation Queue with Dead Letter Queue export const ehrAnnotationQueue = new Queue("ehr_annotation", { logicalId: "EhrAnnotationQueue", visibilityTimeout: "180s", deadLetterQueue: { maxReceiveCount: 3, logicalId: "EhrAnnotationDLQ", }, }); // 4. Automatic EventBridge routing from S3 document uploads to SQS documentsBucket.on("objectCreated", ehrAnnotationQueue, { prefix: "documents/", ruleLogicalId: "EhrAnnotationBusRule", queuePolicyLogicalId: "EhrAnnotationQueuePolicy", }); ``` ```typescript // packages/api/src/infra.ts import { api } from "@syncploy/sdk"; import { app } from "./app"; // 5. High-Performance Lambda Function URL Ingress export const ehrBackendFunction = api( { logicalId: "EhrBackendFunction", roleLogicalId: "EhrBackendFunctionRole", permissionLogicalId: "EhrBackendFunctionUrlPublicPermissions", path: "/", ingress: "function-url", container: { dockerfile: "Dockerfile", context: "." }, timeout: "30s", concurrency: 2, cors: { allowOrigins: ["https://ehr-backend-frontend.vercel.app", "http://localhost:3000"], allowHeaders: ["Content-Type", "Authorization", "X-Api-Key"], allowMethods: ["*"], }, environment: { PORT: "3000", API_KEY: process.env.API_KEY || "", }, }, async (_req, ctx) => app.handle(ctx.request) ); ``` --- ## 📊 Results & Operational Metrics | Architectural Capability | Legacy AWS SAM | Syncploy (IfC) | Improvement | | :--- | :--- | :--- | :--- | | **Lines of Configuration** | 600+ lines YAML | ~300 lines TypeScript | **45% reduction** | | **Type Checking** | None (Untyped YAML) | 100% `tsc --noEmit` | **Zero config drift** | | **Stateful Replacements** | High manual risk | `syncploy diff --check-replacement` | **Guaranteed 0 data loss** | | **CI/CD IAM Credentials** | Wildcard `AdministratorAccess` | Auto-synthesized `deploy-policy.json` | **Least-privilege compliant** | | **Event Wiring Boilerplate** | ~60 lines IAM/QueuePolicy YAML | 1 line: `documentsBucket.on(...)` | **100% compiler-inferred** | | **API Ingress Overhead** | API Gateway hourly cost | Native Lambda Function URL | **$0.00 idle cost, <50ms compute latency** | --- ## 🎯 Conclusion: The Future of Serverless IaC The EHR Annotation Platform migration proves that production teams do not need to choose between the safety of CloudFormation and the agility of TypeScript. Syncploy delivers: 1. **Zero-replacement migration for existing brownfield stacks.** 2. **Compile-time safety rails against infinite loops, timeout mismatches, and DoW spikes.** 3. **1:1 standard CloudFormation ejection with zero vendor lock-in.** ---