Platform
Give each stage its own backend without paying for its own network. Convex.Platform owns the VPC, ECS cluster, load balancer with a wildcard certificate, database server and five buckets. Each Convex.Backend adds its own service, hosts, instance secret, admin key, database, user and bucket prefix.
examples/aws-convex-self-hosted is a working copy of this page.
- alice
- own database
- pr_11
- own database
Deploy the platform
Section titled “Deploy the platform”import * as AWS from "@alchemy.run/aws";import * as Convex from "@alchemy.run/convex/aws";import * as Alchemy from "alchemy";import { Stage } from "alchemy/Stage";import * as Config from "effect/Config";import * as Effect from "effect/Effect";
export class Platform extends Alchemy.Stack<Platform, { convex: Convex.PlatformHandle }>()( "ConvexPlatform",) {}
export default Platform.make( { providers: Convex.providers(), state: AWS.state() }, Effect.gen(function* () { const stage = yield* Stage; const convex = yield* Convex.Platform("Convex", { domain: yield* Config.String("CONVEX_DOMAIN"), db: { engine: "postgres", instanceClass: "db.t4g.micro", storage: 20 }, deletionProtection: stage === "prod", }); return { convex }; }),);CONVEX_DOMAIN=previews.example.com pnpm alchemy deploy stacks/platform.ts --stage devCONVEX_DOMAIN=previews.example.com bun alchemy deploy stacks/platform.ts --stage devCONVEX_DOMAIN=previews.example.com npx alchemy deploy stacks/platform.ts --stage devCONVEX_DOMAIN=previews.example.com yarn alchemy deploy stacks/platform.ts --stage devAdd a stage
Section titled “Add a stage”import * as AWS from "@alchemy.run/aws";import * as Convex from "@alchemy.run/convex/aws";import * as Alchemy from "alchemy";import { Stage } from "alchemy/Stage";import * as Effect from "effect/Effect";import { Platform } from "./stacks/platform.ts";
export default Alchemy.Stack( "ConvexApp", { providers: Convex.providers(), state: AWS.state() }, Effect.gen(function* () { const stage = yield* Stage; const { convex } = yield* Platform.stage[stage === "prod" ? "prod" : "dev"]!;
const backend = yield* Convex.Backend("Backend", { platform: convex, image: "ghcr.io/convex/convex-backend:latest@sha256:<digest>", });
yield* Convex.Functions("Functions", { backend, rootDir: "." });
return { url: backend.url }; }),);pnpm alchemy deploy --stage alicebun alchemy deploy --stage alicenpx alchemy deploy --stage aliceyarn alchemy deploy --stage aliceFind a stage’s hosts
Section titled “Find a stage’s hosts”| Stage | Convex API | HTTP actions |
|---|---|---|
alice |
alice.cloud.previews.example.com |
alice.site.previews.example.com |
dev_sam |
dev-sam.cloud.previews.example.com |
dev-sam.site.previews.example.com |
prod |
cloud.example.com |
site.example.com |
A backend’s hosts are [<stage>.]cloud.<domain> and [<stage>.]site.<domain>, and the production backend leaves the stage out. The stage is always one label in front of a fixed name, so the platform’s certificate (*.<domain>, *.cloud.<domain>, *.site.<domain>) covers every stage and a new stage never waits for one. Characters a hostname can’t hold become hyphens, and a stage can’t be named cloud or site.
Run previews on SQLite
Section titled “Run previews on SQLite”A preview doesn’t need the platform’s database server. With db: { engine: "sqlite" } a stage keeps its data in a file on its task’s own disk, so it needs no database and no volume and comes up in minutes. A platform whose stages all do this can leave out db and run no RDS at all:
const convex = yield* Convex.Platform("Convex", { domain });const backend = yield* Convex.Backend("Backend", { platform: convex, image, db: { engine: "sqlite" },});The data starts empty whenever a new task does, which includes a deploy that changes the backend; Convex.Functions pushes your functions and variables again when that happens. A production backend refuses SQLite. Destroying a SQLite stage deletes its bucket prefix.
Mark the production backend
Section titled “Mark the production backend”The production backend takes the bare hosts, protects its database, and refuses SQLite. That’s the prod stage unless you say otherwise:
const backend = yield* Convex.Backend("Backend", { platform: convex, image, production: stage === "production",});Give each app its own platform
Section titled “Give each app its own platform”Hosts come from the stage name alone, so a platform serves one app. A second app’s alice stage would claim alice.cloud.previews.example.com too: traffic would reach only one of them, and destroying either would delete the other’s DNS records.
Two AWS accounts shows production, staging, developer and pull-request stages across a production and a development account.
Use another DNS host
Section titled “Use another DNS host”Pass the same adapter to the platform and to each stage. An adapter can’t cross stacks, so a stage doesn’t inherit it:
const convex = yield* Convex.Platform("Convex", { domain, db, dns: Cloudflare.DNS.Adapter() });const backend = yield* Convex.Backend("Backend", { platform: convex, image, dns: Cloudflare.DNS.Adapter(),});Keep stages apart
Section titled “Keep stages apart”A stage connects to its own database as its own user. It reads and writes its own objects under <instance>-<uuid>/ in the shared buckets, and the IAM pattern matches the UUID exactly, so stage pr can’t reach pr-42-<uuid>/.
Destroying a stage drops its database and user and deletes its prefix. Other stages keep running.
Remove a platform
Section titled “Remove a platform”Destroy the stages first. A platform with deletionProtection on refuses to go until you set it to false and deploy.
A platform runs a NAT gateway, a load balancer and a database all the time, whether or not a stage is deployed. Each stage adds one Fargate task.