The best way to control Claude Code, Codex and more from any computer or phone

main
ts 115 lines 4.29 KB
Raw
1 import * as EC2 from "@alchemy.run/aws/EC2";
2 import * as Lambda from "@alchemy.run/aws/Lambda";
3 import * as RDS from "@alchemy.run/aws/RDS";
4 import * as SecretsManager from "@alchemy.run/aws/SecretsManager";
5 import * as Drizzle from "@alchemy.run/drizzle/Postgres";
6 import { sql } from "drizzle-orm";
7 import * as Duration from "effect/Duration";
8 import * as Effect from "effect/Effect";
9 import { HttpServerRequest } from "effect/http/HttpServerRequest";
10 import * as HttpServerResponse from "effect/http/HttpServerResponse";
11
12 /**
13 * An isolated VPC (no internet) with an Aurora PostgreSQL cluster that only
14 * the Web Function's security group may reach. IAM auth needs no AWS API
15 * call at runtime (the token is presigned locally), so the function needs
16 * nothing but the in-VPC socket.
17 */
18 export const RdsWebInfra = Effect.gen(function* () {
19 const vpc = yield* EC2.Vpc("RdsWebVpc", { cidrBlock: "10.62.0.0/16" });
20 const subnetA = yield* EC2.Subnet("RdsWebSubnetA", {
21 vpcId: vpc.vpcId,
22 cidrBlock: "10.62.0.0/24",
23 availabilityZone: "us-west-2a",
24 });
25 const subnetB = yield* EC2.Subnet("RdsWebSubnetB", {
26 vpcId: vpc.vpcId,
27 cidrBlock: "10.62.1.0/24",
28 availabilityZone: "us-west-2b",
29 });
30 const appSecurityGroup = yield* EC2.SecurityGroup("RdsWebAppSg", {
31 vpcId: vpc.vpcId,
32 description: "Web Function egress to Aurora",
33 });
34 const dbSecurityGroup = yield* EC2.SecurityGroup("RdsWebDbSg", {
35 vpcId: vpc.vpcId,
36 description: "Aurora: Postgres from the Web Function only",
37 ingress: [
38 {
39 ipProtocol: "tcp",
40 fromPort: 5432,
41 toPort: 5432,
42 referencedGroupId: appSecurityGroup.groupId,
43 },
44 ],
45 });
46 const subnetGroup = yield* RDS.DBSubnetGroup("RdsWebSubnetGroup", {
47 description: "WebFunction RDS.Connect test cluster",
48 subnetIds: [subnetA.subnetId, subnetB.subnetId],
49 });
50 const secret = yield* SecretsManager.Secret("RdsWebSecret", {
51 description: "Master credentials for the WebFunction RDS.Connect test cluster",
52 generateSecretString: {
53 secretStringTemplate: JSON.stringify({ username: "app" }),
54 generateStringKey: "password",
55 PasswordLength: 32,
56 ExcludeCharacters: "\"'@/\\",
57 },
58 });
59 const cluster = yield* RDS.DBCluster("RdsWebCluster", {
60 engine: "aurora-postgresql",
61 engineMode: "provisioned",
62 databaseName: "app",
63 dbSubnetGroupName: subnetGroup.dbSubnetGroupName,
64 vpcSecurityGroupIds: [dbSecurityGroup.groupId],
65 // The test bootstraps the IAM user through the Data API.
66 enableHttpEndpoint: true,
67 enableIAMDatabaseAuthentication: true,
68 serverlessV2ScalingConfiguration: { MinCapacity: 0.5, MaxCapacity: 1 },
69 masterUserSecretArn: secret.secretArn,
70 });
71 yield* RDS.DBInstance("RdsWebWriter", {
72 dbClusterIdentifier: cluster.dbClusterIdentifier,
73 dbInstanceClass: "db.serverless",
74 engine: "aurora-postgresql",
75 });
76 return { subnetA, subnetB, appSecurityGroup, secret, cluster };
77 });
78
79 export class RdsWebFunction extends Lambda.WebFunction<Lambda.WebFunction>()("RdsWebFunction") {}
80
81 /**
82 * No `vpc` prop: `RDS.Connect` with `subnetIds`/`securityGroupIds` requests
83 * the VPC access through the binding channel, and the Web Function turns it
84 * into its egress network connector.
85 */
86 export default RdsWebFunction.make(
87 {
88 main: import.meta.url,
89 authType: "ApplicationManaged",
90 timeout: Duration.seconds(60),
91 },
92 Effect.gen(function* () {
93 const { cluster, subnetA, subnetB, appSecurityGroup } = yield* RdsWebInfra;
94 const connect = yield* RDS.Connect(cluster, {
95 auth: "iam",
96 username: "app_iam",
97 database: "app",
98 subnetIds: [subnetA.subnetId, subnetB.subnetId],
99 securityGroupIds: [appSecurityGroup.groupId],
100 });
101 const db = yield* Drizzle.Postgres(Effect.map(connect, (info) => info.url));
102
103 return {
104 fetch: Effect.gen(function* () {
105 const request = yield* HttpServerRequest;
106 const { pathname } = new URL(request.originalUrl, "http://localhost");
107 if (pathname === "/drizzle-health") {
108 const rows = yield* db.execute<{ one: number }>(sql`select 1 as one`, "objects");
109 return yield* HttpServerResponse.json({ rows });
110 }
111 return HttpServerResponse.text("Not Found", { status: 404 });
112 }).pipe(Effect.orDie),
113 };
114 }).pipe(Effect.provide(RDS.ConnectHttp)),
115 );