@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 106 lines 4.02 KB
Raw
1 import { fileURLToPath } from "node:url";
2 import * as AWS from "@alchemy.run/aws";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as EC2 from "@distilled.cloud/aws/ec2";
6 import * as Lambda from "@distilled.cloud/aws/lambda";
7 import * as Effect from "effect/Effect";
8 import * as Schedule from "effect/Schedule";
9
10 const { test } = Test.make({ providers: AWS.providers() });
11
12 const handlerPath = fileURLToPath(new URL("./timeout-handler.ts", import.meta.url));
13
14 // Repro for https://github.com/xofromthemoon/alchemy-aws-demo — a VPC-attached
15 // Lambda leaves Hyperplane ENIs in its subnets/security group after
16 // DeleteFunction. AWS releases them asynchronously (up to ~20 minutes), which
17 // used to outlive the Subnet/SecurityGroup DependencyViolation retry budgets
18 // and fail the destroy, forcing users to run destroy multiple times. The
19 // delete paths now reap detached Lambda ENIs explicitly so a single destroy
20 // converges. Slow (minutes of AWS-side ENI release), so FAST-gated.
21 test.provider.skipIf(!!process.env.FAST)(
22 "destroy converges in one pass with a VPC-attached function",
23 (stack) =>
24 Effect.gen(function* () {
25 yield* stack.destroy();
26
27 const program = Effect.gen(function* () {
28 const network = yield* AWS.EC2.Network("Network", {
29 cidrBlock: "10.42.0.0/16",
30 availabilityZones: 2,
31 nat: "none",
32 });
33
34 const sg = yield* AWS.EC2.SecurityGroup("FunctionSecurityGroup", {
35 vpcId: network.vpcId,
36 description: "VPC lambda destroy repro",
37 ingress: [
38 {
39 ipProtocol: "tcp",
40 fromPort: 443,
41 toPort: 443,
42 cidrIpv4: "10.42.0.0/16",
43 description: "intra-vpc https",
44 },
45 ],
46 });
47
48 const fn = yield* AWS.Lambda.Function("VpcFn", {
49 main: handlerPath,
50 handler: "handler",
51 isExternal: true,
52 functionUrl: false,
53 vpc: {
54 subnetIds: network.privateSubnetIds,
55 securityGroupIds: [sg.groupId],
56 },
57 });
58
59 return {
60 vpcId: network.vpcId,
61 privateSubnetIds: network.privateSubnetIds,
62 groupId: sg.groupId,
63 functionName: fn.functionName,
64 };
65 });
66
67 const deployed = yield* stack.deploy(program);
68 const configuration = yield* Lambda.getFunctionConfiguration({
69 FunctionName: deployed.functionName,
70 });
71 expect(configuration.State).toBe("Active");
72 expect(configuration.LastUpdateStatus).toBe("Successful");
73 expect(configuration.VpcConfig?.VpcId).toBe(deployed.vpcId);
74
75 // The function's Hyperplane ENI(s) materialize in the private subnets
76 // shortly after the function goes Active — poll (describe is
77 // eventually consistent right after deploy). Note: attached Hyperplane
78 // ENIs report InterfaceType "lambda"; once detached they flip to plain
79 // "interface" and only the description identifies them.
80 const isLambdaEni = (eni: EC2.NetworkInterface): boolean =>
81 eni.InterfaceType === "lambda" ||
82 (eni.Description?.startsWith("AWS Lambda VPC ENI") ?? false);
83 const before = yield* EC2.describeNetworkInterfaces({
84 Filters: [{ Name: "vpc-id", Values: [deployed.vpcId] }],
85 }).pipe(
86 Effect.repeat({
87 schedule: Schedule.spaced("5 seconds"),
88 until: (page) => (page.NetworkInterfaces ?? []).some(isLambdaEni),
89 times: 36,
90 }),
91 );
92 expect((before.NetworkInterfaces ?? []).some(isLambdaEni)).toBe(true);
93
94 // One destroy must fully tear down: function, SG, subnets, VPC.
95 yield* stack.destroy();
96
97 const vpcs = yield* EC2.describeVpcs({
98 VpcIds: [deployed.vpcId],
99 }).pipe(Effect.catchTag("InvalidVpcID.NotFound", () => Effect.succeed({ Vpcs: [] })));
100 expect(vpcs.Vpcs ?? []).toHaveLength(0);
101 }),
102 {
103 tags: ["provider:aws", "provider:aws:ec2", "provider:aws:lambda", "live"],
104 timeout: 30 * 60 * 1000,
105 },
106 );