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

main
ts 513 lines 19.2 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as ecr from "@distilled.cloud/aws/ecr";
5 import * as Lambda from "@distilled.cloud/aws/lambda";
6 import * as Effect from "effect/Effect";
7 import * as FileSystem from "effect/FileSystem";
8 import * as Path from "effect/Path";
9 import * as Schedule from "effect/Schedule";
10 import * as Schema from "effect/Schema";
11 import * as Stream from "effect/Stream";
12
13 const { test } = Test.make({ providers: AWS.providers() });
14
15 const fixture = `${import.meta.dirname}/fixtures/image-function`;
16 const coreFunctionName = "alchemy-test-lambda-container-image";
17 const armFunctionName = "alchemy-test-lambda-container-image-arm64";
18 const zipFunctionName = "alchemy-test-lambda-container-image-zip";
19 const externalFunctionName = "alchemy-test-lambda-container-image-external";
20 const externalRepositoryName = "alchemy-test-lambda-external-image";
21 const skipLive = !!process.env.FAST;
22 const liveTestTimeout = 240_000;
23
24 const imageFunction = (
25 context: string,
26 functionName: string,
27 architecture: AWS.Lambda.FunctionArchitecture,
28 ) =>
29 AWS.Lambda.Function("ContainerFunction", {
30 functionName,
31 image: { context, dockerfile: "Dockerfile" },
32 architecture,
33 env: {
34 IMAGE_FUNCTION_ENV: "bound",
35 },
36 functionUrl: false,
37 });
38
39 const zipFunction = (functionName: string) =>
40 AWS.Lambda.Function("ContainerFunction", {
41 functionName,
42 main: `${import.meta.dirname}/timeout-handler.ts`,
43 handler: "handler",
44 isExternal: true,
45 functionUrl: false,
46 });
47
48 // Docker builds, ECR pushes, Lambda propagation, and the provider's bounded
49 // CloudWatch Logs cleanup make these unsuitable for the default fast sweep.
50 // Each test is kept within one deletion window (~1-2 min each) and is
51 // skipped under --fast.
52 test.provider.skipIf(skipLive)(
53 "builds, invokes, updates, and destroys an image function",
54 (stack) =>
55 Effect.gen(function* () {
56 const fs = yield* FileSystem.FileSystem;
57 const path = yield* Path.Path;
58 yield* stack.destroy();
59
60 const context = yield* fs.makeTempDirectoryScoped({
61 prefix: "alchemy-lambda-image-",
62 });
63 yield* fs.copy(fixture, context, { overwrite: true });
64
65 const first = yield* stack.deploy(imageFunction(context, coreFunctionName, "x86_64"));
66 expect(first.functionName).toBe(coreFunctionName);
67 expect(first.code.image?.digest).toMatch(/^sha256:/);
68 expect(first.code.image?.imageUri).toContain(`:${first.code.hash}`);
69 expect(first.code.image!.resolvedImageUri).toBe(
70 `${first.code.image!.repositoryUri}@${first.code.image!.digest}`,
71 );
72 yield* assertRepositoryImmutable(first.code.image!.repositoryName);
73 yield* assertFunctionImage(first.functionName, "x86_64");
74 expect(yield* invoke(first.functionName, "one")).toEqual({
75 marker: "one",
76 environment: "bound",
77 event: { hello: "image" },
78 });
79
80 // Unchanged context: the engine and ECR tag are both a no-op.
81 const unchanged = yield* stack.deploy(imageFunction(context, coreFunctionName, "x86_64"));
82 expect(unchanged.code.hash).toBe(first.code.hash);
83 expect(unchanged.code.image?.digest).toBe(first.code.image?.digest);
84
85 // Repository drift alone must plan an update so reconcile can restore
86 // the content-addressing and Lambda pull contracts.
87 yield* ecr.putImageTagMutability({
88 repositoryName: unchanged.code.image!.repositoryName,
89 imageTagMutability: "MUTABLE",
90 });
91 yield* ecr.deleteRepositoryPolicy({
92 repositoryName: unchanged.code.image!.repositoryName,
93 });
94 const repositoryDriftPlan = yield* stack.plan(
95 imageFunction(context, coreFunctionName, "x86_64"),
96 );
97 expect(repositoryDriftPlan.resources["ContainerFunction"]).toMatchObject({
98 action: "update",
99 });
100 const repaired = yield* stack.deploy(imageFunction(context, coreFunctionName, "x86_64"));
101 yield* assertRepositoryImmutable(repaired.code.image!.repositoryName);
102 yield* assertRepositoryPolicy(repaired.code.image!.repositoryName);
103
104 // A context change creates a new content-addressed tag and updates the
105 // existing Lambda in place.
106 yield* fs.writeFileString(path.join(context, "marker.txt"), "two\n");
107 const updated = yield* stack.deploy(imageFunction(context, coreFunctionName, "x86_64"));
108 expect(updated.functionName).toBe(first.functionName);
109 expect(updated.code.hash).not.toBe(first.code.hash);
110 expect(updated.code.image?.digest).not.toBe(first.code.image?.digest);
111 yield* assertRepositoryImmutable(updated.code.image!.repositoryName);
112 expect((yield* invoke(updated.functionName, "two")).marker).toBe("two");
113
114 // Architecture changes update the image function. The separate arm64
115 // smoke test below performs the platform-specific build and invocation.
116 const armPlan = yield* stack.plan(imageFunction(context, coreFunctionName, "arm64"));
117 expect(armPlan.resources["ContainerFunction"]).toMatchObject({
118 action: "update",
119 });
120
121 // Package type is immutable. A fixed function name must be deleted
122 // before the Zip replacement can reuse it.
123 const zipPlan = yield* stack.plan(zipFunction(coreFunctionName));
124 expect(zipPlan.resources["ContainerFunction"]).toMatchObject({
125 action: "replace",
126 deleteFirst: true,
127 });
128
129 const repositoryName = updated.code.image!.repositoryName;
130 yield* stack.destroy();
131 yield* assertFunctionDeleted(coreFunctionName);
132 yield* assertRepositoryDeleted(repositoryName);
133 }).pipe(
134 Effect.tap(() => stack.destroy()),
135 Effect.onError(() => stack.destroy().pipe(Effect.ignore)),
136 ),
137 {
138 tags: ["provider:aws", "provider:aws:ecr", "provider:aws:lambda", "live"],
139 timeout: liveTestTimeout,
140 },
141 );
142
143 test.provider.skipIf(skipLive)(
144 "deploys an existing ECR tag, detects retagging, and applies image overrides",
145 (stack) =>
146 Effect.gen(function* () {
147 const fs = yield* FileSystem.FileSystem;
148 const path = yield* Path.Path;
149 yield* stack.destroy();
150
151 const context = yield* fs.makeTempDirectoryScoped({
152 prefix: "alchemy-lambda-external-image-",
153 });
154 yield* fs.copy(fixture, context, { overwrite: true });
155
156 const program = (uri?: string, imageConfig?: AWS.Lambda.FunctionImageConfig) =>
157 Effect.gen(function* () {
158 const repository = yield* AWS.ECR.Repository("ExternalRepository", {
159 repositoryName: externalRepositoryName,
160 });
161 const image = yield* AWS.ECR.Image("ExternalImage", {
162 repositoryUri: repository.repositoryUri,
163 context,
164 platform: "linux/amd64",
165 });
166 const func =
167 uri === undefined
168 ? undefined
169 : yield* AWS.Lambda.Function("ExternalFunction", {
170 functionName: externalFunctionName,
171 image: { uri, ...imageConfig },
172 architecture: "x86_64",
173 env: { IMAGE_FUNCTION_ENV: "external" },
174 functionUrl: false,
175 });
176 return { image, func };
177 });
178
179 const aliasTag = "lambda";
180 const pointAliasAt = Effect.fn(function* (repositoryName: string, imageTag: string) {
181 const response = yield* ecr.batchGetImage({
182 repositoryName,
183 imageIds: [{ imageTag }],
184 });
185 const image = response.images?.[0];
186 if (image?.imageManifest === undefined) {
187 return yield* Effect.fail(
188 new Error(`ECR image ${repositoryName}:${imageTag} has no manifest`),
189 );
190 }
191 yield* ecr
192 .putImage({
193 repositoryName,
194 imageManifest: image.imageManifest,
195 imageManifestMediaType: image.imageManifestMediaType,
196 imageTag: aliasTag,
197 })
198 .pipe(Effect.catchTag("ImageAlreadyExistsException", () => Effect.void));
199 });
200
201 // Materialize a private ECR image independently of the Lambda function,
202 // then pass its now-literal URI through the external-image create path.
203 const source = yield* stack.deploy(program());
204 yield* pointAliasAt(source.image.repositoryName, source.image.imageTag);
205 const aliasUri = `${source.image.repositoryUri}:${aliasTag}`;
206 const tagged = yield* stack.deploy(
207 program(aliasUri, {
208 command: ["index.alternate"],
209 entryPoint: ["/lambda-entrypoint.sh"],
210 workingDirectory: "/var/task",
211 }),
212 );
213 if (tagged.func === undefined) {
214 return yield* Effect.fail(new Error("External function was not created"));
215 }
216 expect(tagged.func.code.image).toMatchObject({
217 source: "uri",
218 imageUri: aliasUri,
219 ownsRepository: false,
220 });
221 yield* assertFunctionImage(tagged.func.functionName, "x86_64");
222 yield* assertFunctionImageConfig(tagged.func.functionName, {
223 Command: ["index.alternate"],
224 EntryPoint: ["/lambda-entrypoint.sh"],
225 WorkingDirectory: "/var/task",
226 });
227 expect(yield* invokeAlternate(tagged.func.functionName)).toBe("alternate");
228
229 // Rebuild an independently-managed image, then repoint the same alias.
230 // The URI string is unchanged, so only digest resolution can detect this.
231 yield* fs.writeFileString(path.join(context, "marker.txt"), "two\n");
232 const rebuilt = yield* stack.deploy(
233 program(aliasUri, {
234 command: ["index.alternate"],
235 entryPoint: ["/lambda-entrypoint.sh"],
236 workingDirectory: "/var/task",
237 }),
238 );
239 yield* pointAliasAt(rebuilt.image.repositoryName, rebuilt.image.imageTag);
240
241 // Omitting the overrides while updating the digest clears every override.
242 const retaggedPlan = yield* stack.plan(program(aliasUri));
243 expect(retaggedPlan.resources.ExternalFunction).toMatchObject({
244 action: "update",
245 });
246 const retagged = yield* stack.deploy(program(aliasUri));
247 if (retagged.func === undefined) {
248 return yield* Effect.fail(new Error("External function was not updated"));
249 }
250 expect(retagged.func.code.image).toMatchObject({
251 source: "uri",
252 imageUri: aliasUri,
253 digest: rebuilt.image.digest,
254 ownsRepository: false,
255 });
256 yield* assertFunctionImageConfig(retagged.func.functionName, undefined);
257 expect((yield* invoke(retagged.func.functionName, "two")).marker).toBe("two");
258
259 yield* stack.destroy();
260 yield* assertFunctionDeleted(externalFunctionName);
261 yield* assertRepositoryDeleted(externalRepositoryName);
262 }).pipe(
263 Effect.tap(() => stack.destroy()),
264 Effect.onError(() => stack.destroy().pipe(Effect.ignore)),
265 ),
266 {
267 tags: ["provider:aws", "provider:aws:ecr", "provider:aws:lambda", "live"],
268 timeout: liveTestTimeout,
269 },
270 );
271
272 test.provider.skipIf(skipLive)(
273 "builds and invokes an arm64 image function",
274 (stack) =>
275 Effect.gen(function* () {
276 const fs = yield* FileSystem.FileSystem;
277 yield* stack.destroy();
278
279 const context = yield* fs.makeTempDirectoryScoped({
280 prefix: "alchemy-lambda-image-arm64-",
281 });
282 yield* fs.copy(fixture, context, { overwrite: true });
283
284 const deployed = yield* stack.deploy(imageFunction(context, armFunctionName, "arm64"));
285 expect(deployed.functionName).toBe(armFunctionName);
286 expect(deployed.code.image?.digest).toMatch(/^sha256:/);
287 yield* assertRepositoryImmutable(deployed.code.image!.repositoryName);
288 yield* assertFunctionImage(deployed.functionName, "arm64");
289 expect(yield* invoke(deployed.functionName, "one")).toEqual({
290 marker: "one",
291 environment: "bound",
292 event: { hello: "image" },
293 });
294
295 const repositoryName = deployed.code.image!.repositoryName;
296 yield* stack.destroy();
297 yield* assertFunctionDeleted(armFunctionName);
298 yield* assertRepositoryDeleted(repositoryName);
299 }).pipe(
300 Effect.tap(() => stack.destroy()),
301 Effect.onError(() => stack.destroy().pipe(Effect.ignore)),
302 ),
303 {
304 tags: ["provider:aws", "provider:aws:ecr", "provider:aws:lambda", "live"],
305 timeout: liveTestTimeout,
306 },
307 );
308
309 test.provider.skipIf(skipLive)(
310 "plans a Zip function replacement with an image function",
311 (stack) =>
312 Effect.gen(function* () {
313 const fs = yield* FileSystem.FileSystem;
314 yield* stack.destroy();
315
316 const context = yield* fs.makeTempDirectoryScoped({
317 prefix: "alchemy-lambda-image-replacement-",
318 });
319 yield* fs.copy(fixture, context, { overwrite: true });
320
321 const deployed = yield* stack.deploy(zipFunction(zipFunctionName));
322 expect(deployed.functionName).toBe(zipFunctionName);
323 yield* assertFunctionPackageType(deployed.functionName, "Zip");
324
325 const renamedPlan = yield* stack.plan(zipFunction(`${zipFunctionName}-renamed`));
326 expect(renamedPlan.resources["ContainerFunction"]).toMatchObject({
327 action: "replace",
328 deleteFirst: false,
329 });
330
331 const imagePlan = yield* stack.plan(imageFunction(context, zipFunctionName, "x86_64"));
332 expect(imagePlan.resources["ContainerFunction"]).toMatchObject({
333 action: "replace",
334 deleteFirst: true,
335 });
336
337 yield* stack.destroy();
338 yield* assertFunctionDeleted(zipFunctionName);
339 }).pipe(
340 Effect.tap(() => stack.destroy()),
341 Effect.onError(() => stack.destroy().pipe(Effect.ignore)),
342 ),
343 {
344 tags: ["provider:aws", "provider:aws:lambda", "live"],
345 timeout: liveTestTimeout,
346 },
347 );
348
349 const ImageInvocationResponse = Schema.Struct({
350 marker: Schema.optionalKey(Schema.String),
351 environment: Schema.optionalKey(Schema.String),
352 event: Schema.Struct({ hello: Schema.String }),
353 handler: Schema.optionalKey(Schema.String),
354 });
355
356 const invokePayload = Effect.fn(function* (deployedFunctionName: string) {
357 return yield* Lambda.invoke({
358 FunctionName: deployedFunctionName,
359 Payload: JSON.stringify({ hello: "image" }),
360 }).pipe(
361 Effect.flatMap((response) =>
362 Effect.gen(function* () {
363 if (response.FunctionError) {
364 return yield* Effect.fail(
365 new Error(`Lambda invocation failed: ${response.FunctionError}`),
366 );
367 }
368 const payload = response.Payload
369 ? yield* response.Payload.pipe(Stream.decodeText(), Stream.mkString)
370 : "";
371 const json = yield* Effect.try({
372 try: () => JSON.parse(payload) as unknown,
373 catch: (cause) => new Error("Lambda returned an invalid JSON payload", { cause }),
374 });
375 return yield* Schema.decodeUnknownEffect(ImageInvocationResponse)(json);
376 }),
377 ),
378 );
379 });
380
381 const invoke = Effect.fn(function* (deployedFunctionName: string, expectedMarker: string) {
382 return yield* invokePayload(deployedFunctionName).pipe(
383 Effect.filterOrFail(
384 (response) => response.marker === expectedMarker,
385 () => new Error(`Lambda is not serving marker ${expectedMarker} yet`),
386 ),
387 Effect.retry({
388 schedule: Schedule.spaced("2 seconds"),
389 times: 10,
390 }),
391 );
392 });
393
394 const invokeAlternate = Effect.fn(function* (deployedFunctionName: string) {
395 return yield* invokePayload(deployedFunctionName).pipe(
396 Effect.filterOrFail(
397 (response) => response.handler === "alternate",
398 () => new Error("Lambda is not serving the alternate image command yet"),
399 ),
400 Effect.map((response) => response.handler),
401 Effect.retry({
402 schedule: Schedule.spaced("2 seconds"),
403 times: 10,
404 }),
405 );
406 });
407
408 const assertFunctionImage = Effect.fn(function* (
409 deployedFunctionName: string,
410 architecture: AWS.Lambda.FunctionArchitecture,
411 ) {
412 yield* Lambda.getFunction({ FunctionName: deployedFunctionName }).pipe(
413 Effect.filterOrFail(
414 (response) =>
415 response.Configuration?.State === "Active" &&
416 response.Configuration.LastUpdateStatus === "Successful" &&
417 response.Configuration?.PackageType === "Image" &&
418 response.Configuration.Architectures?.[0] === architecture &&
419 response.Code?.ResolvedImageUri?.includes("@sha256:") === true,
420 () => new Error("Lambda image configuration has not propagated yet"),
421 ),
422 Effect.retry({
423 schedule: Schedule.spaced("2 seconds"),
424 times: 10,
425 }),
426 );
427 });
428
429 const assertFunctionImageConfig = Effect.fn(function* (
430 deployedFunctionName: string,
431 expected: Lambda.ImageConfig | undefined,
432 ) {
433 yield* Lambda.getFunction({ FunctionName: deployedFunctionName }).pipe(
434 Effect.filterOrFail(
435 (response) => {
436 const observed = response.Configuration?.ImageConfigResponse?.ImageConfig;
437 if (expected === undefined) {
438 return (
439 observed?.Command === undefined &&
440 observed?.EntryPoint === undefined &&
441 observed?.WorkingDirectory === undefined
442 );
443 }
444 return (
445 JSON.stringify(observed?.Command) === JSON.stringify(expected.Command) &&
446 JSON.stringify(observed?.EntryPoint) === JSON.stringify(expected.EntryPoint) &&
447 observed?.WorkingDirectory === expected.WorkingDirectory
448 );
449 },
450 () => new Error("Lambda image overrides have not propagated yet"),
451 ),
452 Effect.retry({
453 schedule: Schedule.spaced("2 seconds"),
454 times: 10,
455 }),
456 );
457 });
458
459 const assertFunctionPackageType = Effect.fn(function* (
460 deployedFunctionName: string,
461 packageType: Lambda.PackageType,
462 ) {
463 yield* Lambda.getFunction({ FunctionName: deployedFunctionName }).pipe(
464 Effect.filterOrFail(
465 (response) => response.Configuration?.PackageType === packageType,
466 () => new Error(`Lambda package type is not ${packageType} yet`),
467 ),
468 Effect.retry({
469 schedule: Schedule.spaced("2 seconds"),
470 times: 10,
471 }),
472 );
473 });
474
475 const assertFunctionDeleted = Effect.fn(function* (deployedFunctionName: string) {
476 yield* Lambda.getFunction({ FunctionName: deployedFunctionName }).pipe(
477 Effect.flatMap(() => Effect.fail(new Error(`Function ${deployedFunctionName} still exists`))),
478 Effect.catchTag("ResourceNotFoundException", () => Effect.void),
479 Effect.retry({
480 schedule: Schedule.spaced("1 second"),
481 times: 8,
482 }),
483 );
484 });
485
486 const assertRepositoryDeleted = Effect.fn(function* (repositoryName: string) {
487 yield* ecr.describeRepositories({ repositoryNames: [repositoryName] }).pipe(
488 Effect.flatMap(() => Effect.fail(new Error(`Repository ${repositoryName} still exists`))),
489 Effect.catchTag("RepositoryNotFoundException", () => Effect.void),
490 Effect.retry({
491 schedule: Schedule.spaced("1 second"),
492 times: 8,
493 }),
494 );
495 });
496
497 const assertRepositoryImmutable = Effect.fn(function* (repositoryName: string) {
498 yield* ecr.describeRepositories({ repositoryNames: [repositoryName] }).pipe(
499 Effect.filterOrFail(
500 (response) => response.repositories?.[0]?.imageTagMutability === "IMMUTABLE",
501 () => new Error(`Repository ${repositoryName} is not immutable`),
502 ),
503 );
504 });
505
506 const assertRepositoryPolicy = Effect.fn(function* (repositoryName: string) {
507 yield* ecr.getRepositoryPolicy({ repositoryName }).pipe(
508 Effect.filterOrFail(
509 (response) => response.policyText?.includes("LambdaECRImageRetrievalPolicy") === true,
510 () => new Error(`Repository ${repositoryName} has no Lambda pull policy`),
511 ),
512 );
513 });