@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 111 lines 3.7 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { ServerHost } from "alchemy/Runtime/Process";
3 import * as Effect from "effect/Effect";
4 import { HttpServerRequest } from "effect/http/HttpServerRequest";
5 import * as HttpServerResponse from "effect/http/HttpServerResponse";
6 import * as Ref from "effect/Ref";
7 import * as Schedule from "effect/Schedule";
8
9 /**
10 * Alchemy-managed EC2 key pair granting SSH access to the instance. Exported so
11 * the test can resolve it in the same deploy and read its (redacted) private
12 * key — yielding the same logical id returns the same resource.
13 */
14 export const keyPair = AWS.EC2.KeyPair("Ec2E2EKeyPair", {
15 keyType: "ed25519",
16 });
17
18 /**
19 * End-to-end fixture for a hosted `AWS.EC2.Instance`: a long-running server.
20 *
21 * The props Effect provisions the networking (a public-subnet VPC) and the
22 * instance's security group, then launches the instance into it. The program
23 * Effect registers a `ServerHost.run` background loop (the #706 pattern) and
24 * returns a `{ fetch }` handler that the instance's Bun HTTP server serves on
25 * `port`. `/ticks` reports the loop counter so the test can prove the
26 * background loop runs inside the deployed instance.
27 */
28 export default class TestInstance extends AWS.EC2.Instance<TestInstance>()(
29 "Ec2E2EInstance",
30 Effect.gen(function* () {
31 // This composition is re-executed inside the deployed instance's bundle:
32 // the AMI lookup is an `Output` resolved at plan/deploy time only, and
33 // resource yields resolve to references at runtime, so no runtime guard
34 // is needed.
35 const network = yield* AWS.EC2.Network("Ec2E2ENetwork", {
36 cidrBlock: "10.81.0.0/16",
37 availabilityZones: 1,
38 });
39 const securityGroup = yield* AWS.EC2.SecurityGroup("Ec2E2ESg", {
40 vpcId: network.vpcId,
41 description: "alchemy ec2 instance e2e",
42 ingress: [
43 {
44 ipProtocol: "tcp",
45 fromPort: 3000,
46 toPort: 3000,
47 cidrIpv4: "0.0.0.0/0",
48 description: "app",
49 },
50 {
51 ipProtocol: "tcp",
52 fromPort: 22,
53 toPort: 22,
54 cidrIpv4: "0.0.0.0/0",
55 description: "ssh",
56 },
57 ],
58 egress: [
59 {
60 ipProtocol: "-1",
61 cidrIpv4: "0.0.0.0/0",
62 description: "all outbound",
63 },
64 ],
65 });
66
67 // An Alchemy-managed EC2 key pair grants SSH access to the instance.
68 const key = yield* keyPair;
69
70 return {
71 main: import.meta.filename,
72 imageId: AWS.EC2.amazonLinux2023(),
73 instanceType: "t3.small",
74 subnetId: network.publicSubnetIds[0],
75 securityGroupIds: [securityGroup.groupId],
76 associatePublicIpAddress: true,
77 port: 3000,
78 keyName: key.keyName,
79 // SSM access so the instance is manageable via Session Manager.
80 roleManagedPolicyArns: ["arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"],
81 };
82 }),
83 Effect.gen(function* () {
84 const host = yield* ServerHost;
85 const ticks = yield* Ref.make(0);
86
87 // Long-running background loop (the `host.run` pattern from #706).
88 yield* host.run(
89 Ref.update(ticks, (n) => n + 1).pipe(
90 Effect.repeat(Schedule.spaced("1 second")),
91 Effect.asVoid,
92 ),
93 );
94
95 return {
96 fetch: Effect.gen(function* () {
97 const request = yield* HttpServerRequest;
98 const url = new URL(request.url, "http://instance");
99 if (url.pathname === "/health") {
100 return yield* HttpServerResponse.json({ ok: true });
101 }
102 if (url.pathname === "/ticks") {
103 return yield* HttpServerResponse.json({
104 ticks: yield* Ref.get(ticks),
105 });
106 }
107 return HttpServerResponse.text("hello from ec2 instance");
108 }),
109 };
110 }),
111 ) {}