-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsst.config.ts
168 lines (158 loc) · 4.55 KB
/
sst.config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* eslint-disable */
/// <reference path="./.sst/platform/config.d.ts" />
import { execSync } from "child_process";
export default $config({
app(input) {
return {
name: "hello-zero-solid",
removal: input?.stage === "production" ? "retain" : "remove",
home: "aws",
region: process.env.AWS_REGION || "us-east-1",
};
},
async run() {
const zeroVersion = execSync(
"npm list @rocicorp/zero | grep @rocicorp/zero | cut -f 3 -d @"
)
.toString()
.trim();
// S3 Bucket
const replicationBucket = new sst.aws.Bucket(`replication-bucket`);
// VPC Configuration
const vpc = new sst.aws.Vpc(`vpc`, {
az: 2,
nat: "ec2",
});
// ECS Cluster
const cluster = new sst.aws.Cluster(`cluster`, {
vpc,
});
const conn = new sst.Secret("PostgresConnectionString");
const zeroAuthSecret = new sst.Secret("ZeroAuthSecret");
// Common environment variables
const commonEnv = {
ZERO_UPSTREAM_DB: conn.value,
ZERO_CVR_DB: conn.value,
ZERO_CHANGE_DB: conn.value,
ZERO_AUTH_SECRET: zeroAuthSecret.value,
ZERO_REPLICA_FILE: "sync-replica.db",
ZERO_LITESTREAM_BACKUP_URL: $interpolate`s3://${replicationBucket.name}/backup`,
ZERO_IMAGE_URL: `rocicorp/zero:${zeroVersion}`,
ZERO_CVR_MAX_CONNS: "10",
ZERO_UPSTREAM_MAX_CONNS: "10",
};
// Replication Manager Service
const replicationManager = new sst.aws.Service(`replication-manager`, {
cluster,
cpu: "0.5 vCPU",
memory: "1 GB",
architecture: "arm64",
image: commonEnv.ZERO_IMAGE_URL,
link: [replicationBucket],
health: {
command: ["CMD-SHELL", "curl -f http://localhost:4849/ || exit 1"],
interval: "5 seconds",
retries: 3,
startPeriod: "300 seconds",
},
environment: {
...commonEnv,
ZERO_CHANGE_MAX_CONNS: "3",
ZERO_NUM_SYNC_WORKERS: "0",
},
loadBalancer: {
public: false,
ports: [
{
listen: "80/http",
forward: "4849/http",
},
],
},
transform: {
loadBalancer: {
idleTimeout: 3600,
},
target: {
healthCheck: {
enabled: true,
path: "/keepalive",
protocol: "HTTP",
interval: 5,
healthyThreshold: 2,
timeout: 3,
},
},
},
});
// View Syncer Service
const viewSyncer = new sst.aws.Service(`view-syncer`, {
cluster,
cpu: "1 vCPU",
memory: "2 GB",
architecture: "arm64",
image: commonEnv.ZERO_IMAGE_URL,
link: [replicationBucket],
health: {
command: ["CMD-SHELL", "curl -f http://localhost:4848/ || exit 1"],
interval: "5 seconds",
retries: 3,
startPeriod: "300 seconds",
},
environment: {
...commonEnv,
ZERO_CHANGE_STREAMER_URI: replicationManager.url,
},
logging: {
retention: "1 month",
},
loadBalancer: {
public: true,
rules: [{ listen: "80/http", forward: "4848/http" }],
},
transform: {
target: {
healthCheck: {
enabled: true,
path: "/keepalive",
protocol: "HTTP",
interval: 5,
healthyThreshold: 2,
timeout: 3,
},
stickiness: {
enabled: true,
type: "lb_cookie",
cookieDuration: 120,
},
loadBalancingAlgorithmType: "least_outstanding_requests",
},
},
});
// Permissions deployment
// We build the permission update as part of the build command in
// package.json and deploy it with this lambda. This prevents the
// CI/CD env from needing access to database.
// If you are willing to expose your database to CI, then a simpler
// option is possible. See:
// https://github.com/rocicorp/hello-zero/blob/main/sst.config.ts#L142
const permissionsDeployer = new sst.aws.Function(
"zero-permissions-deployer",
{
handler: "./functions/permissions.deploy",
vpc,
environment: { ["ZERO_UPSTREAM_DB"]: conn.value },
copyFiles: [{ from: ".permissions.sql", to: ".permissions.sql" }],
}
);
new aws.lambda.Invocation(
"invoke-zero-permissions-deployer",
{
// Invoke the Lambda on every deploy.
input: Date.now().toString(),
functionName: permissionsDeployer.name,
},
{ dependsOn: viewSyncer }
);
},
});