Deployment: Infrastructure with SST
Relevant source files
The following files were used as context for generating this wiki page:The OpenCode project utilizes the Serverless Stack (SST) toolkit to define, provision, and deploy its cloud infrastructure as code. This approach allows for repeatable, version-controlled, and stage-aware deployments. The infrastructure is primarily defined in TypeScript within the infra/ directory and leverages various SST components to deploy services to Cloudflare, manage databases with PlanetScale, and integrate with third-party services like Stripe, GitHub, and Google.
The core of the deployment is centered around the Console application, a SolidStart web application, and its supporting backend services, including a dedicated authentication worker and a PlanetScale database. The configuration is stage-dependent, allowing for different setups for production, development (dev), and other environments.
Overall Architecture
The system is composed of several key services that are orchestrated by SST. The main user-facing component is the Console (a SolidStart application), which relies on a separate AuthApi for handling user authentication and a PlanetScale database for data persistence. The architecture also includes integrations for payments (Stripe), email (AWS SES), and observability (Honeycomb).
This diagram shows the high-level interaction between the major components of the deployed infrastructure.
Sources: infra/console.ts
Database
The project uses PlanetScale for its database needs. The infrastructure code defines the connection to a database named opencode within the anomalyco organization.
Sources: infra/console.ts:10-13
Staging and Branching
The database configuration is stage-aware.
- For the
productionstage, it connects to theproductionbranch of the database. - For any other stage (e.g., development), a new database branch is created, named after the stage, which forks from the
productionbranch. This ensures that development and testing environments are isolated from production data.
Sources: infra/console.ts:15-24
Credentials and Linking
Database credentials are managed using planetscale.Password. The resulting connection details (host, database name, username, password) are wrapped in an sst.Linkable("Database", ...) resource. This allows other SST components to securely access the database connection information without hardcoding secrets.
Sources: infra/console.ts:15-35
Development Studio
For local development, an sst.x.DevCommand is configured to run bun db studio from the packages/console/core directory. This command is linked to the database, providing developers with easy access to a database management UI.
Sources: infra/console.ts:37-43
Authentication Service
Authentication is handled by a dedicated Cloudflare Worker named AuthApi.
Key Components
| Component | Type | Description |
|---|---|---|
AuthApi | sst.cloudflare.Worker | The main worker handling authentication logic. It's accessible via a public URL at auth.${domain}. |
AuthStorage | sst.cloudflare.Kv | A Key-Value store used by the authentication service. |
| Secrets | sst.Secret | Manages OAuth credentials for GitHub (GITHUB_CLIENT_ID_CONSOLE, GITHUB_CLIENT_SECRET_CONSOLE) and Google (GOOGLE_CLIENT_ID). |
The AuthApi worker is linked to the database, authStorage, and the required OAuth secrets.
Sources: infra/console.ts:49-55
This diagram illustrates the dependencies of the AuthApi worker.
Sources: infra/console.ts:54
Console Application
The main frontend is a SolidStart application deployed as a Cloudflare Worker using sst.cloudflare.x.SolidStart("Console", ...).
Linked Resources
The Console application is linked to a wide array of backend resources and secrets, which are made available to it at runtime.
| Resource/Secret | Purpose |
|---|---|
bucket (ZenData) | Cloudflare R2 bucket for data storage. |
database | Link to the PlanetScale database. |
AUTH_API_URL | URL of the AuthApi worker. |
STRIPE_SECRET_KEY | Secret key for Stripe integration. |
STRIPE_WEBHOOK_SECRET | Secret for validating Stripe webhooks. |
EMAILOCTOPUS_API_KEY | API key for EmailOctopus. |
AWS_SES_ACCESS_KEY_ID / _SECRET | Credentials for AWS Simple Email Service. |
ZEN_MODELS | An array of secrets for Zen models. |
gatewayKv | A Cloudflare KV namespace. |
Sources: infra/console.ts:133-152
Environment Configuration
The SolidStart application receives its configuration through environment variables prefixed with VITE_. For example, the authentication URL is passed to the frontend build process like this:
// infra/console.ts:153-156
environment: {
VITE_AUTH_URL: auth.url.apply((url) => url!),
},
The frontend code then consumes this, for example in the main landing page component.
Sources: packages/console/app/src/routes/index.tsx
Log Processing
In production and frank stages, the Console worker is configured with a tailConsumer. This means that logs generated by the Console application are automatically sent to the LogProcessor worker for processing and forwarding to an observability platform like Honeycomb.
// infra/console.ts:161-163
tailConsumers: logProcessor ? [{ service: logProcessor.nodes.worker.scriptName }] : [],
Sources: infra/console.ts:158-166
Supporting Services
Stripe Integration
A Stripe webhook endpoint is configured to listen for a wide range of events related to checkouts, customers, and subscriptions. This allows the application to react to payment-related events in real-time.
Enabled Stripe Events:
checkout.session.completedcustomer.subscription.createdcustomer.subscription.deletedcustomer.subscription.updatedcharge.refunded- ...and many others.
Sources: infra/console.ts:61-88
Sitemap Generation
A script at packages/console/app/script/generate-sitemap.ts is used to generate a sitemap.xml file. It gathers static routes like /, /enterprise, and /zen, as well as dynamic routes from the docs, and writes the final XML to the public directory of the console app. This file is then deployed as part of the SolidStart application's static assets.
Sources: packages/console/app/script/generate-sitemap.ts
Development
The project's CONTRIBUTING.md file outlines the steps for setting up a local development environment. The primary commands are:
bun install
bun dev
These commands install dependencies and start the development server, respectively. The SST configuration also includes development-specific logic, such as injecting Cloudflare API tokens only when $dev is true, to facilitate local development and testing.