Skip to content

Instantly share code, notes, and snippets.

@erikmunson
Last active January 11, 2025 19:15
Show Gist options
  • Save erikmunson/60fc05e477fd469f381f84d0af781b75 to your computer and use it in GitHub Desktop.
Save erikmunson/60fc05e477fd469f381f84d0af781b75 to your computer and use it in GitHub Desktop.
Zero SST test/prototype example
/// <reference path="./.sst/platform/config.d.ts" />
const BASE_DOMAIN_CERT_ARN =
'<cert ARN in AWS for your domain>'
const buildServiceDomainName = (slug: string) => {
// build whatever domain you like
const baseDomain = `${$app.stage}.mydomain.com`
return `${slug}.${baseDomain}`
}
const buildServiceDomainConfig = ({ slug }: { slug: string }) =>
({
name: buildServiceDomainName(slug),
dns: false,
cert: BASE_DOMAIN_CERT_ARN,
}) as const
const buildZeroSchema = () => {
// I happen to use yarn, replace yarn exec with npx or pnpm exec
// or whatever your equivalent package manager command is
execSync(`yarn exec zero-build-schema -p "path/to/your/schema.mts"`)
return fs.readFileSync('zero-schema.json', 'utf-8')
}
export default $config({
app(input) {
return {
name: 'zero-sst-test',
removal: input?.stage === 'prod' ? 'retain' : 'remove',
home: 'aws',
}
},
async run() {
// If you are not using a custom publication, you can remove this
// and the ZERO_SHARD_PUBLICATIONS environment variable from the
// zero-cache service config. Read the configuration docs on
// ZERO_SHARD_PUBLICATIONS for more:
// https://zero-docs-preview.vercel.app/docs/zero-cache-config
const publications = ['zero_main']
// Set the values for each of these with the `sst secret set` CLI
// command. Docs on how to use SST secrets here:
// https://sst.dev/docs/component/secret
//
// If you are planning to have multiple stages (e.g. dev and prod),
// make sure to read about fallbacks and stage-specific secrets
// and set your secrets for the appropriate stages.
const zeroUpstreamPgConnection = new sst.Secret(
'ZeroCacheUpstreamConnection'
)
const zeroInternalPgConnection = new sst.Secret(
'ZeroCachePostgresConnection'
)
const zeroJwtSecret = new sst.Secret('ZeroCacheJwt')
const vpc = new sst.aws.Vpc('Main', { bastion: true })
const cluster = new sst.aws.Cluster('ZeroCacheCluster', {
vpc,
})
cluster.addService('ZeroCacheService', {
cpu: '2 vCPU',
memory: '4 GB',
scaling: {
min: 1,
max: 1,
},
// Replace the tag in the image with whatever version you like
image: 'registry.hub.docker.com/rocicorp/zero:0.8.2024121002-29e93d',
environment: {
// you may want or need different configuration settings for your
// zero-cache, read the docs on configuration for more:
// https://zero-docs-preview.vercel.app/docs/zero-cache-config
ZERO_REPLICA_FILE: 'sync-replica.db',
ZERO_SHARD_PUBLICATIONS: publications.join(','),
ZERO_UPSTREAM_DB: zeroUpstreamPgConnection.value,
ZERO_CVR_DB: zeroInternalPgConnection.value,
ZERO_CHANGE_DB: zeroInternalPgConnection.value,
ZERO_JWT_SECRET: zeroJwtSecret.value,
// this builds and stringifies the zero schema from your project
// and includes it directly in the server.
ZERO_SCHEMA_JSON: buildZeroSchema(),
ZERO_AUTO_RESET: 'true',
ZERO_LOG_LEVEL: 'debug',
},
// I happen to be using TLS/HTTPS and a custom domain + cert,
// check out the SST docs for how to set up HTTPS.
// Or if you are just testing things out and want to stick
// to HTTP and avoid a custom domain, use this config instead:
//
// loadBalancer: { ports: [{ listen: '80/http', forward: '4848/http' }] },
//
//
loadBalancer: {
domain: buildServiceDomainConfig({ slug: 'zero' }),
ports: [{ listen: '443/https', forward: '4848/http' }],
},
loadBalancer: {
ports: [{ listen: '80/http', forward: '4848/http' }],
},
transform: {
service: (service) => {
// The cache is a single-node service for now, so to avoid
// multiple nodes fighting each other let's force ECS to
// shut down the old node before starting a new node.
// The default behavior is to add a second, new node
// before shutting down the old one to prevent downtime.
// see this article section for a general overview of
// how this works:
// https://nathanpeck.com/speeding-up-amazon-ecs-container-deployments/#deploy-steps
service.deploymentMaximumPercent = 100
service.deploymentMinimumHealthyPercent = 0
},
},
})
},
})
@elledienne
Copy link

Thanks for sharing! I'd also add max_slot_wal_keep_size to ensure the WAL size doesn't blow and fill the entire DB storage

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment