-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathsst.config.ts
More file actions
348 lines (324 loc) · 10.7 KB
/
sst.config.ts
File metadata and controls
348 lines (324 loc) · 10.7 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// eslint-disable-next-line @typescript-eslint/triple-slash-reference
/// <reference path="./.sst/platform/config.d.ts" />
// TODO(stefanos): this is deployed in OSS Electric so
// it cannot be torn down without removing this project as well,
// slightly annyoing
const REUSABLE_VPC_ID = `vpc-044836d73fc26a218`;
export default $config({
app(input) {
return {
name: `electric-ai-chat`,
removal: input?.stage.toLocaleLowerCase() === `production` ? `retain` : `remove`,
home: `aws`,
providers: {
cloudflare: `5.42.0`,
aws: {
version: `6.76.0`,
profile: process.env.CI ? undefined : `marketing`,
},
neon: `0.6.3`,
command: `1.0.1`,
},
};
},
async run() {
const { getFileChecksum } = await import('./utils/file-hash');
const isProduction = $app.stage.toLocaleLowerCase() === `production`;
const region = `us-east-1`;
const schemaFile = `./db/schema.sql`;
const neonProjectId = new sst.Secret(`NeonProjectId`);
const subdomain = `examples.${isProduction ? `electric-sql.com` : `electric-sql.dev`}`;
const demoDomainTitle = `electric-ai-chat`;
const backendDomain = `${isProduction ? `${demoDomainTitle}-api` : `${demoDomainTitle}-api-${$app.stage}`}.${subdomain}`;
const frontendDomain = `${isProduction ? `${demoDomainTitle}` : `${demoDomainTitle}-${$app.stage}`}.${subdomain}`;
// Add schema hash to db name to blow up on schema changes
const schemaHash = getFileChecksum(schemaFile, 10);
const dbName = `${isProduction ? `ai-chat` : `ai-chat-${$app.stage}`}-${schemaHash}`;
const replicationStreamId = `${$app.stage}_ai_chat`.replace(/-/g, `_`);
// Iniitalize a database
let dbUrl: $util.Input<string>;
let pooledDbUrl: $util.Input<string>;
let roleName: $util.Input<string>;
if ($dev) {
new sst.x.DevCommand(`AiChatPostgres`, {
dev: {
title: `Postgres`,
command: `docker-compose -f ./.support/docker-compose.yml up`,
autostart: true,
},
});
dbUrl = `postgresql://postgres:password@localhost:54321/ai-chat`;
pooledDbUrl = dbUrl;
roleName = `postgres`;
} else {
const neonProject = neon.getProjectOutput({ id: neonProjectId.value });
const neonDb = createNeonDb({
projectId: neonProject.id,
branchId: neonProject.defaultBranchId,
dbName,
});
const dbConfig = {
project: neonProject,
databaseName: neonDb.dbName,
roleName: neonDb.ownerName,
};
dbUrl = getNeonConnectionString({ ...dbConfig, pooled: false });
pooledDbUrl = getNeonConnectionString({ ...dbConfig, pooled: true });
roleName = neonDb.ownerName;
}
dbUrl = $resolve([dbUrl, roleName]).apply(async ([dbUrl, roleName]) => {
console.log(`Running necessary migrations`);
const startTime = Date.now();
const timeoutMs = 30 * 1000;
while (Date.now() - startTime < timeoutMs) {
try {
await runSqlFile(dbUrl, schemaFile);
break;
} catch (err) {
if (err.message.includes(`database "${dbName}" does not exist`)) {
await new Promise(res => setTimeout(res, 2000));
continue;
}
throw err;
}
}
return dbUrl;
});
// Initialize AWS services
const provider = new aws.Provider(`AiChatProvider`, { region });
const vpc = sst.aws.Vpc.get(`AiChatVpc`, REUSABLE_VPC_ID, { provider });
const cluster = new sst.aws.Cluster(`AiChatCluster`, { forceUpgrade: `v2`, vpc }, { provider });
// Electric for syncing
const syncService = new sst.aws.Service(
`AiChatSyncService`,
{
cluster,
cpu: isProduction ? `2 vCPU` : `0.25 vCPU`,
memory: isProduction ? `4 GB` : `0.5 GB`,
image: {
context: `electric/packages/sync-service`,
},
architecture: getSystemArch(),
health: {
command: [`CMD-SHELL`, `curl -fsS http://localhost:3000/v1/health > /dev/null`],
// allow some time for system to start and recover
startPeriod: `30 seconds`,
// retry for a total of 3 minutes
interval: `30 seconds`,
retries: 6,
},
environment: {
ELECTRIC_DB_POOL_SIZE: $jsonStringify(isProduction ? 20 : 5),
ELECTRIC_REPLICATION_STREAM_ID: replicationStreamId,
DATABASE_URL: dbUrl,
ELECTRIC_QUERY_DATABASE_URL: pooledDbUrl,
ELECTRIC_INSECURE: $jsonStringify(true),
ELECTRIC_LOG_COLORS: $jsonStringify(false),
},
dev: {
directory: `electric/packages/sync-service`,
command: `iex -S mix`,
autostart: true,
},
},
{}
);
const syncServiceUrl = $dev
? `http://localhost:3000`
: $interpolate`http://${syncService.service}:3000`;
// Backend for proxying and other endpoints
const openAiKey = new sst.Secret(`OpenAiKey`);
const backendPort = 3001;
const backend = new sst.aws.Service(
`AiChatBackend`,
{
cluster,
cpu: isProduction ? `2 vCPU` : `0.25 vCPU`,
memory: isProduction ? `4 GB` : `0.5 GB`,
architecture: getSystemArch(),
image: {
context: `.`,
dockerfile: `packages/api/Dockerfile`,
tags: [`latest`],
},
loadBalancer: {
ports: [{ listen: '443/https', forward: `${backendPort}/http` }],
domain: {
name: backendDomain,
dns: sst.cloudflare.dns(),
},
health: {
[`${backendPort}/http`]: {
path: `/test`,
},
},
},
health: {
command: [`CMD-SHELL`, `curl -fsS http://localhost:${backendPort}/test > /dev/null`],
},
environment: {
DATABASE_URL: pooledDbUrl,
ELECTRIC_API_URL: syncServiceUrl,
FRONTEND_ORIGIN: new URL(`https://${frontendDomain}`).origin,
OPENAI_MODEL: 'gpt-4o-mini',
OPENAI_API_KEY: openAiKey.value,
PORT: $jsonStringify(backendPort),
},
dev: {
directory: `packages/api/`,
command: `pnpm dev:caddy`,
url: `https://localhost:${backendPort}`,
autostart: true,
},
},
{
dependsOn: [syncService],
}
);
// Frontend for actual chat app
const frontend = new sst.aws.StaticSite(
`AiChatFrontend`,
{
domain: {
name: frontendDomain,
dns: sst.cloudflare.dns(),
},
environment: {
VITE_API_URL: $dev ? `http://localhost:${backendPort}` : backend.url,
},
path: `packages/app`,
build: {
command: `pnpm --filter electric-chat-app... build`,
output: `dist`,
},
dev: {
title: `Frontend`,
directory: `packages/app`,
command: `pnpm dev`,
url: `http://localhost:5173/`,
},
},
{ provider, dependsOn: [backend] }
);
return {
dbUrl: $dev ? dbUrl : `REDACTED`,
frontend: frontend.url,
backend: backend.url,
syncService: syncServiceUrl,
};
},
});
function getNeonConnectionString({
project,
roleName,
databaseName,
pooled,
}: {
project: $util.Output<neon.GetProjectResult>;
roleName: $util.Input<string>;
databaseName: $util.Input<string>;
pooled: boolean;
}): $util.Output<string> {
const passwordOutput = neon.getBranchRolePasswordOutput({
projectId: project.id,
branchId: project.defaultBranchId,
roleName: roleName,
});
const endpoint = neon.getBranchEndpointsOutput({
projectId: project.id,
branchId: project.defaultBranchId,
});
const databaseHost = pooled
? endpoint.endpoints?.apply(endpoints =>
endpoints![0].host.replace(endpoints![0].id, endpoints![0].id + `-pooler`)
)
: project.databaseHost;
return $interpolate`postgresql://${passwordOutput.roleName}:${passwordOutput.password}@${databaseHost}/${databaseName}?sslmode=require`;
}
/**
* Uses the [Neon API](https://neon.tech/docs/manage/databases) along with
* a Pulumi Command resource and `curl` to create and delete Neon databases.
*/
function createNeonDb({
projectId,
branchId,
dbName,
}: {
projectId: $util.Input<string>;
branchId: $util.Input<string>;
dbName: $util.Input<string>;
}): $util.Output<{
dbName: string;
ownerName: string;
}> {
if (!process.env.NEON_API_KEY) {
throw new Error(`NEON_API_KEY is not set`);
}
const ownerName = `neondb_owner`;
const createCommand = `curl -f -s "https://console.neon.tech/api/v2/projects/$PROJECT_ID/branches/$BRANCH_ID/databases" \
-H 'Accept: application/json' \
-H "Authorization: Bearer $NEON_API_KEY" \
-H 'Content-Type: application/json' \
-d '{
"database": {
"name": "'$DATABASE_NAME'",
"owner_name": "${ownerName}"
}
}' \
&& echo " SUCCESS" || echo " FAILURE"`;
const updateCommand = `echo "Cannot update Neon database with this provisioning method SUCCESS"`;
const deleteCommand = `curl -f -s -X 'DELETE' \
"https://console.neon.tech/api/v2/projects/$PROJECT_ID/branches/$BRANCH_ID/databases/$DATABASE_NAME" \
-H 'Accept: application/json' \
-H "Authorization: Bearer $NEON_API_KEY" \
&& echo " SUCCESS" || echo " FAILURE"`;
const result = new command.local.Command(`neon-db-command:${dbName}`, {
create: createCommand,
update: updateCommand,
delete: deleteCommand,
environment: {
NEON_API_KEY: process.env.NEON_API_KEY,
PROJECT_ID: projectId,
BRANCH_ID: branchId,
DATABASE_NAME: dbName,
},
});
return $resolve([result.stdout, dbName]).apply(([stdout, dbName]) => {
if (stdout.endsWith(`SUCCESS`)) {
console.log(`Created Neon database ${dbName}`);
return {
dbName,
ownerName,
};
} else {
throw new Error(`Failed to create Neon database ${dbName}: ${stdout}`);
}
});
}
async function runSqlFile(connectionString: string, filePath: string) {
const { Client } = await import('pg');
const { readFileSync } = await import('fs');
const client = new Client({ connectionString });
try {
await client.connect();
let sql = readFileSync(filePath, 'utf8');
await client.query(sql);
} catch (err) {
console.error(`Failed to run SQL:`, err);
throw err;
} finally {
await client.end();
}
}
async function getSystemArch(): Promise<`x86_64` | `arm64`> {
const os = await import('node:os');
const currentArch = os.arch();
switch (currentArch) {
case `x64`:
return `x86_64`;
case `arm64`:
return `arm64`;
default:
throw new Error(`Cannot build docker image on arch ${currentArch} - must be x86_64 or arm64`);
}
}