-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsst.config.ts
136 lines (122 loc) · 3.35 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
/// <reference path="./.sst/platform/config.d.ts" />
import {execSync} from "node:child_process";
export default $config({
app(input) {
return {
name: "a2025BlogBackendSyncBenchmarks",
removal: input?.stage === "production" ? "retain" : "remove",
home: "aws",
providers: {
aws: {
profile: `marketing`,
},
neon: "0.6.3",
command: `1.0.1`,
},
};
},
async run() {
// env var checks
const {getNeonConnectionString, createNeonDb} = await import("./neon");
// Create a db in Neon
const project = neon.getProjectOutput({id: `square-flower-52864146`});
const dbName = `user_benchmark_${$app.stage.replace(/-/g, `_`)}`;
const branchId = `br-blue-morning-a4ywuv4l`;
type NeonConnOptions = Parameters<typeof getNeonConnectionString>[0];
let dbOpts: NeonConnOptions = {
project,
branchId,
roleName: "",
databaseName: "",
pooled: false,
};
const {dbName: resultingDbName, ownerName} = createNeonDb({
projectId: project.id,
branchId,
dbName,
});
dbOpts.roleName = ownerName;
dbOpts.databaseName = resultingDbName;
const dbUrl = getNeonConnectionString(dbOpts);
const postgres = new sst.Linkable(`postgres`, {
properties: {
url: dbUrl,
},
});
dbUrl.apply((url) => {
applyMigrations(url);
});
const electricUrlLink = new sst.Linkable("ElectricUrl", {
properties: {
url: process.env.ELECTRIC_URL,
sourceId: process.env.ELECTRIC_SOURCE_ID,
sourceSecret: process.env.ELECTRIC_SOURCE_SECRET,
},
});
const vpc = sst.aws.Vpc.get(
"examples-infra-shared-examplesInfraVpcShared",
"vpc-044836d73fc26a218",
);
const cluster = sst.aws.Cluster.get(
"examples-infra-shared-examplesInfraClusterSharedCluster",
{
vpc,
id: `arn:aws:ecs:us-east-1:904233135193:cluster/examples-infra-shared-examplesInfraClusterSharedCluster`,
},
);
const redisBenchmark = cluster.addService("redis-benchmark", {
link: [postgres],
dev: {
command: `npx tsx ./node/index.ts`,
url: `http://localhost:4005`,
},
memory: `8 GB`,
cpu: `4 vCPU`,
containers: [
{
name: `benchmark-script`,
image: {
dockerfile: `./node/Dockerfile.redis`,
},
environment: {
SOURCE_ID: process.env.SOURCE_ID,
SOURCE_SECRET: process.env.SOURCE_SECRET,
NO_COLOR: "1",
FORCE_COLOR: "0",
},
cpu: `2 vCPU`,
memory: `4 GB`,
},
{
name: `redis`,
image: {
dockerfile: `./node/Dockerfile.redis-server`,
},
cpu: `2 vCPU`,
memory: `4 GB`,
},
],
});
//add lambda
const node = new sst.aws.Function("FastifyApi", {
url: true,
link: [electricUrlLink],
handler: "server/lambda.handler",
timeout: "3 minutes",
memory: "1024 MB",
});
return {
node: node.url,
dbUrl: postgres.properties.url,
};
},
});
function applyMigrations(uri: string) {
console.log(`apply migrations to `, uri);
execSync(`npx pg-migrations apply --directory ./db/migrations`, {
env: {
...process.env,
DATABASE_URL: uri,
},
});
}