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

main
ts 222 lines 8.04 KB
Raw
1 import * as EMR from "@alchemy.run/aws/EMR";
2 import * as IAM from "@alchemy.run/aws/IAM";
3 import * as Lambda from "@alchemy.run/aws/Lambda";
4 import * as Duration from "effect/Duration";
5 import * as Effect from "effect/Effect";
6 import { HttpServerRequest } from "effect/http/HttpServerRequest";
7 import * as HttpServerResponse from "effect/http/HttpServerResponse";
8 import * as Layer from "effect/Layer";
9 import path from "pathe";
10
11 const main = path.resolve(import.meta.dirname, "slow-handler.ts");
12
13 export class EmrSlowTestFunction extends Lambda.Function<Lambda.Function>()(
14 "EmrSlowTestFunction",
15 ) {}
16
17 /**
18 * Cluster-scoped binding fixture: deploys a real single-node Spark cluster
19 * (~10-15 minutes to WAITING, billed per instance-hour — skipped under
20 * `--fast`) and a Lambda bound to it with the steps data plane, the
21 * cluster-inspection reads, and the managed scaling policy ops.
22 *
23 * The cluster launches into an EMR-chosen subnet of the account's default
24 * VPC (no explicit `ec2SubnetId`), keeps itself alive between steps, and
25 * carries a 2-hour auto-termination policy as a cost safety net.
26 */
27 export default EmrSlowTestFunction.make(
28 {
29 main,
30 functionUrl: true,
31 timeout: Duration.seconds(60),
32 },
33 Effect.gen(function* () {
34 // EMR service role (legacy managed policy — the v2 policy requires
35 // tag-scoped resources).
36 const serviceRole = yield* IAM.Role("EmrBindingsServiceRole", {
37 assumeRolePolicyDocument: {
38 Version: "2012-10-17",
39 Statement: [
40 {
41 Effect: "Allow",
42 Principal: { Service: ["elasticmapreduce.amazonaws.com"] },
43 Action: ["sts:AssumeRole"],
44 },
45 ],
46 },
47 managedPolicyArns: ["arn:aws:iam::aws:policy/service-role/AmazonElasticMapReduceRole"],
48 });
49 // EC2 instance role + profile (the job-flow role).
50 const ec2Role = yield* IAM.Role("EmrBindingsEc2Role", {
51 assumeRolePolicyDocument: {
52 Version: "2012-10-17",
53 Statement: [
54 {
55 Effect: "Allow",
56 Principal: { Service: ["ec2.amazonaws.com"] },
57 Action: ["sts:AssumeRole"],
58 },
59 ],
60 },
61 managedPolicyArns: ["arn:aws:iam::aws:policy/service-role/AmazonElasticMapReduceforEC2Role"],
62 });
63 const instanceProfile = yield* IAM.InstanceProfile("EmrBindingsProfile", {
64 roleName: ec2Role.roleName,
65 });
66
67 // Single-node (master-only) Spark cluster — the cheapest live cluster
68 // that can still run steps.
69 const cluster = yield* EMR.Cluster("BindingsCluster", {
70 releaseLabel: "emr-7.5.0",
71 applications: ["Spark"],
72 serviceRole: serviceRole.roleName,
73 jobFlowRole: instanceProfile.instanceProfileName,
74 instances: {
75 masterInstanceType: "m5.xlarge",
76 coreInstanceCount: 0,
77 keepJobFlowAliveWhenNoSteps: true,
78 },
79 autoTerminationPolicy: { idleTimeout: "2 hours" },
80 tags: { fixture: "emr-bindings" },
81 });
82
83 const addSteps = yield* EMR.AddJobFlowSteps(cluster);
84 const describeStep = yield* EMR.DescribeStep(cluster);
85 const listSteps = yield* EMR.ListSteps(cluster);
86 const cancelSteps = yield* EMR.CancelSteps(cluster);
87 const listInstances = yield* EMR.ListInstances(cluster);
88 const listInstanceGroups = yield* EMR.ListInstanceGroups(cluster);
89 const listBootstrapActions = yield* EMR.ListBootstrapActions(cluster);
90 const getScalingPolicy = yield* EMR.GetManagedScalingPolicy(cluster);
91 const putScalingPolicy = yield* EMR.PutManagedScalingPolicy(cluster);
92 const removeScalingPolicy = yield* EMR.RemoveManagedScalingPolicy(cluster);
93
94 return {
95 fetch: Effect.gen(function* () {
96 const request = yield* HttpServerRequest;
97 const url = new URL(request.originalUrl);
98 const pathname = url.pathname;
99
100 if (request.method === "GET" && pathname === "/bindings") {
101 return yield* HttpServerResponse.json({ bound: 10 });
102 }
103
104 // Submit a trivial shell step — proves AddJobFlowSteps and the
105 // JobFlowId injection.
106 if (request.method === "GET" && pathname === "/steps/add") {
107 const { StepIds } = yield* addSteps({
108 Steps: [
109 {
110 Name: "alchemy-echo",
111 ActionOnFailure: "CONTINUE",
112 HadoopJarStep: {
113 Jar: "command-runner.jar",
114 Args: ["bash", "-c", "echo alchemy"],
115 },
116 },
117 ],
118 });
119 return yield* HttpServerResponse.json({ stepId: StepIds?.[0] });
120 }
121
122 if (request.method === "GET" && pathname === "/step") {
123 const { Step } = yield* describeStep({
124 StepId: url.searchParams.get("id")!,
125 });
126 return yield* HttpServerResponse.json({
127 state: Step?.Status?.State,
128 name: Step?.Name,
129 });
130 }
131
132 if (request.method === "GET" && pathname === "/steps") {
133 const { Steps } = yield* listSteps();
134 return yield* HttpServerResponse.json({
135 count: (Steps ?? []).length,
136 });
137 }
138
139 // Cancellation of a completed step surfaces a per-step error reason
140 // rather than failing the call — either shape proves the grant.
141 if (request.method === "GET" && pathname === "/steps/cancel") {
142 const { CancelStepsInfoList } = yield* cancelSteps({
143 StepIds: [url.searchParams.get("id")!],
144 });
145 return yield* HttpServerResponse.json({
146 status: CancelStepsInfoList?.[0]?.Status,
147 });
148 }
149
150 if (request.method === "GET" && pathname === "/instances") {
151 const { Instances } = yield* listInstances();
152 return yield* HttpServerResponse.json({
153 count: (Instances ?? []).length,
154 });
155 }
156
157 if (request.method === "GET" && pathname === "/groups") {
158 const { InstanceGroups } = yield* listInstanceGroups();
159 return yield* HttpServerResponse.json({
160 types: (InstanceGroups ?? []).map((g) => g.InstanceGroupType),
161 });
162 }
163
164 if (request.method === "GET" && pathname === "/bootstrap") {
165 const { BootstrapActions } = yield* listBootstrapActions();
166 return yield* HttpServerResponse.json({
167 count: (BootstrapActions ?? []).length,
168 });
169 }
170
171 // Managed scaling put -> get -> remove round trip. A master-only
172 // cluster may reject the put with a typed validation tag — either
173 // outcome proves the grants and the ClusterId injection.
174 if (request.method === "GET" && pathname === "/scaling") {
175 const putResult = yield* putScalingPolicy({
176 ManagedScalingPolicy: {
177 ComputeLimits: {
178 UnitType: "Instances",
179 MinimumCapacityUnits: 1,
180 MaximumCapacityUnits: 4,
181 },
182 },
183 }).pipe(
184 Effect.map(() => ({ ok: true as const })),
185 Effect.catchTag("ValidationException", (e) =>
186 Effect.succeed({ ok: false as const, tag: e._tag }),
187 ),
188 );
189 if (!putResult.ok) {
190 return yield* HttpServerResponse.json(putResult);
191 }
192 const { ManagedScalingPolicy } = yield* getScalingPolicy();
193 yield* removeScalingPolicy();
194 return yield* HttpServerResponse.json({
195 ok: true,
196 max: ManagedScalingPolicy?.ComputeLimits?.MaximumCapacityUnits,
197 });
198 }
199
200 return yield* HttpServerResponse.json(
201 { error: "Not found", method: request.method, pathname },
202 { status: 404 },
203 );
204 }).pipe(Effect.orDie),
205 };
206 }).pipe(
207 Effect.provide(
208 Layer.mergeAll(
209 EMR.AddJobFlowStepsHttp,
210 EMR.DescribeStepHttp,
211 EMR.ListStepsHttp,
212 EMR.CancelStepsHttp,
213 EMR.ListInstancesHttp,
214 EMR.ListInstanceGroupsHttp,
215 EMR.ListBootstrapActionsHttp,
216 EMR.GetManagedScalingPolicyHttp,
217 EMR.PutManagedScalingPolicyHttp,
218 EMR.RemoveManagedScalingPolicyHttp,
219 ),
220 ),
221 ),
222 );