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

main
ts 131 lines 4.57 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import {
3 AutoScalingGroup,
4 CompleteLifecycleAction,
5 CompleteLifecycleActionHttp,
6 consumeLifecycleActions,
7 LaunchTemplate,
8 } from "@alchemy.run/aws/AutoScaling";
9 import { amazonLinux2023 } from "@alchemy.run/aws/EC2";
10 import * as Output from "alchemy/Output";
11 import * as Context from "effect/Context";
12 import * as Duration from "effect/Duration";
13 import * as Effect from "effect/Effect";
14 import { HttpServerRequest } from "effect/http/HttpServerRequest";
15 import * as HttpServerResponse from "effect/http/HttpServerResponse";
16 import * as Layer from "effect/Layer";
17 import * as Stream from "effect/Stream";
18 import { getAutoScalingTestSubnetId } from "../TestNetwork.ts";
19
20 export const lifecycleFleetAsgName = "alchemy-test-lifecycle-e2e-asg";
21 export const lifecycleHookName = "alchemy-test-lifecycle-e2e-hook";
22
23 export class LifecycleTestFunction extends AWS.Lambda.Function<AWS.Lambda.Function>()(
24 "LifecycleTestFunction",
25 ) {}
26
27 /**
28 * Shared fleet for the lifecycle-hook E2E: a launch template + Auto Scaling
29 * Group sized to zero (no instances launch by default). Subnet/AMI are
30 * `Output`s resolved at deploy time only, so this composition is safe to
31 * re-execute inside the deployed Lambda without runtime guards.
32 */
33 export class LifecycleFleet extends Context.Service<LifecycleFleet, { group: AutoScalingGroup }>()(
34 "AutoScalingLifecycleFleet",
35 ) {}
36
37 export const LifecycleFleetLive = Layer.effect(
38 LifecycleFleet,
39 Effect.gen(function* () {
40 const imageId = amazonLinux2023();
41 const subnetId = Output.fromEffect(getAutoScalingTestSubnetId.pipe(Effect.orDie));
42
43 const template = yield* LaunchTemplate("LifecycleTemplate", {
44 imageId,
45 instanceType: "t3.micro",
46 });
47 const group = yield* AutoScalingGroup("LifecycleGroup", {
48 autoScalingGroupName: lifecycleFleetAsgName,
49 launchTemplate: template,
50 subnetIds: [subnetId],
51 minSize: 0,
52 maxSize: 0,
53 desiredCapacity: 0,
54 });
55 return { group };
56 }),
57 );
58
59 export default LifecycleTestFunction.make(
60 {
61 main: import.meta.url,
62 functionUrl: true,
63 // /complete-bogus makes a live Auto Scaling call; a cold start plus SDK
64 // round-trip can exceed Lambda's 3s default and surface as a 502.
65 timeout: Duration.seconds(30),
66 },
67 Effect.gen(function* () {
68 const { group } = yield* LifecycleFleet;
69 const lifecycle = yield* CompleteLifecycleAction(group);
70
71 // Register the launch lifecycle hook + EventBridge subscription. Instances
72 // that enter the wait state are drained by signalling CONTINUE.
73 yield* consumeLifecycleActions(
74 group,
75 {
76 lifecycleHookName,
77 lifecycleTransition: "LAUNCHING",
78 heartbeatTimeout: "300 seconds",
79 defaultResult: "ABANDON",
80 },
81 (events) =>
82 Stream.runForEach(events, (event) =>
83 lifecycle
84 .complete({
85 LifecycleHookName: event.detail.LifecycleHookName,
86 LifecycleActionToken: event.detail.LifecycleActionToken,
87 LifecycleActionResult: "CONTINUE",
88 })
89 .pipe(Effect.orDie),
90 ),
91 );
92
93 return {
94 fetch: Effect.gen(function* () {
95 const request = yield* HttpServerRequest;
96 const pathname = new URL(request.originalUrl).pathname;
97
98 if (request.method === "GET" && pathname === "/health") {
99 return yield* HttpServerResponse.json({ ok: true });
100 }
101
102 // Exercise the CompleteLifecycleAction binding directly with a
103 // non-existent action token: AWS rejects it (typed `ValidationError`)
104 // rather than AccessDenied, proving the IAM policy the binding attached
105 // is correct. Returns the error tag so the test can assert wiring.
106 if (request.method === "POST" && pathname === "/complete-bogus") {
107 const result = yield* lifecycle
108 .complete({
109 LifecycleHookName: lifecycleHookName,
110 LifecycleActionToken: "00000000-0000-0000-0000-000000000000",
111 LifecycleActionResult: "CONTINUE",
112 })
113 .pipe(Effect.result);
114 return yield* HttpServerResponse.json({
115 ok: result._tag === "Success",
116 tag: result._tag === "Failure" ? result.failure._tag : "Success",
117 });
118 }
119
120 return yield* HttpServerResponse.json(
121 { error: "Not found", method: request.method, pathname },
122 { status: 404 },
123 );
124 }).pipe(Effect.orDie),
125 };
126 }).pipe(
127 Effect.provide(
128 Layer.mergeAll(AWS.Lambda.EventSource, CompleteLifecycleActionHttp, LifecycleFleetLive),
129 ),
130 ),
131 );