The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { fileURLToPath } from "node:url"; |
| 2 | import * as AWS from "@alchemy.run/aws"; |
| 3 | import { describe, expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as Core from "@alchemy.run/test/Core"; |
| 6 | import * as EC2 from "@distilled.cloud/aws/ec2"; |
| 7 | import * as efs from "@distilled.cloud/aws/efs"; |
| 8 | import * as Duration from "effect/Duration"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import * as HttpClient from "effect/http/HttpClient"; |
| 11 | import * as Schedule from "effect/Schedule"; |
| 12 | import { getDefaultVpc } from "../DefaultVpc.ts"; |
| 13 | |
| 14 | // Flagship EFS e2e: a VPC-attached Lambda mounts an EFS access point at |
| 15 | // /mnt/test, writes a file over HTTP, reads it back, and a config redeploy |
| 16 | // (fresh sandboxes) proves the data outlives the execution environment. |
| 17 | // |
| 18 | // Uses the default VPC and its DEFAULT security group on purpose: the default |
| 19 | // group's self-referencing ingress rule allows the Lambda ENI to reach the |
| 20 | // mount target's NFS port, and because we never delete the default group we |
| 21 | // avoid waiting out the ~20-minute Hyperplane-ENI release that blocks |
| 22 | // deleting a security group that was attached to a Lambda. |
| 23 | |
| 24 | const handlerPath = fileURLToPath(new URL("./efs-handler.ts", import.meta.url)); |
| 25 | |
| 26 | const testOptions = { providers: AWS.providers() }; |
| 27 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 28 | const sharedStack = Core.scratchStack(testOptions, "EFSLambdaMount"); |
| 29 | |
| 30 | let subnetId: string; |
| 31 | let securityGroupId: string; |
| 32 | let baseUrl: string; |
| 33 | let fileSystemId: string; |
| 34 | |
| 35 | // Resolve the default VPC's subnet and default security group once. Runs |
| 36 | // inside a deploy effect (the `beforeAll` hook itself has no AWS context). |
| 37 | const resolveNetwork = Effect.gen(function* () { |
| 38 | if (subnetId !== undefined) return; |
| 39 | const vpc = yield* getDefaultVpc; |
| 40 | const subnets = yield* EC2.describeSubnets({ |
| 41 | Filters: [ |
| 42 | { Name: "vpc-id", Values: [vpc.vpcId] }, |
| 43 | { Name: "default-for-az", Values: ["true"] }, |
| 44 | ], |
| 45 | }); |
| 46 | subnetId = subnets.Subnets![0]!.SubnetId!; |
| 47 | const groups = yield* EC2.describeSecurityGroups({ |
| 48 | Filters: [ |
| 49 | { Name: "vpc-id", Values: [vpc.vpcId] }, |
| 50 | { Name: "group-name", Values: ["default"] }, |
| 51 | ], |
| 52 | }); |
| 53 | securityGroupId = groups.SecurityGroups![0]!.GroupId!; |
| 54 | }); |
| 55 | |
| 56 | const infra = (marker: string) => |
| 57 | Effect.gen(function* () { |
| 58 | yield* resolveNetwork; |
| 59 | const files = yield* AWS.EFS.FileSystem("MountFiles", { throughputMode: "elastic" }); |
| 60 | const target = yield* AWS.EFS.MountTarget("MountTarget", { |
| 61 | fileSystemId: files.fileSystemId, |
| 62 | subnetId, |
| 63 | }); |
| 64 | const accessPoint = yield* AWS.EFS.AccessPoint("MountAccess", { |
| 65 | fileSystemId: files.fileSystemId, |
| 66 | posixUser: { uid: 1000, gid: 1000 }, |
| 67 | rootDirectory: { |
| 68 | path: "/lambda", |
| 69 | creationInfo: { ownerUid: 1000, ownerGid: 1000, permissions: "750" }, |
| 70 | }, |
| 71 | }); |
| 72 | const fn = yield* AWS.Lambda.Function("EfsFn", { |
| 73 | main: handlerPath, |
| 74 | handler: "handler", |
| 75 | isExternal: true, |
| 76 | functionUrl: true, |
| 77 | memorySize: 256, |
| 78 | timeout: Duration.seconds(30), |
| 79 | vpc: { subnetIds: [subnetId], securityGroupIds: [securityGroupId] }, |
| 80 | fileSystemConfigs: [ |
| 81 | // pass the AccessPoint resource itself — the provider resolves its ARN |
| 82 | { accessPoint, localMountPath: "/mnt/test" }, |
| 83 | ], |
| 84 | env: { |
| 85 | // Depend on the mount target so the function is only created once |
| 86 | // the NFS endpoint is available (Lambda rejects a file-system config |
| 87 | // whose mount targets are still creating). |
| 88 | EFS_MOUNT_TARGET: target.mountTargetId, |
| 89 | DEPLOY_MARKER: marker, |
| 90 | }, |
| 91 | }); |
| 92 | return { files, target, accessPoint, fn }; |
| 93 | }); |
| 94 | |
| 95 | // VPC-attached Lambda cold starts ride on ENI/mount provisioning: budget a |
| 96 | // generous bounded retry for the FIRST request (3s × 60 ≈ 180s), then fail |
| 97 | // fast on later ones. |
| 98 | const getJsonWithRetry = (url: string, times: number) => |
| 99 | HttpClient.get(url).pipe( |
| 100 | Effect.flatMap((response) => |
| 101 | response.status === 200 |
| 102 | ? response.json |
| 103 | : response.text.pipe( |
| 104 | Effect.flatMap((body) => |
| 105 | Effect.fail(new Error(`${url} returned ${response.status}: ${body}`)), |
| 106 | ), |
| 107 | ), |
| 108 | ), |
| 109 | Effect.retry({ schedule: Schedule.max([Schedule.fixed("3 seconds"), Schedule.recurs(times)]) }), |
| 110 | ); |
| 111 | |
| 112 | // Skipped under `--fast`: the suite's wall clock is ~6–8 minutes end to |
| 113 | // end — the beforeAll deploy alone is ~250s (file system + mount-target ENI |
| 114 | // provisioning + VPC-attached Lambda with EFS mount validation) and the |
| 115 | // afterAll teardown waits out the mount-target/ENI release (~2–4 min). That |
| 116 | // is genuinely slow platform provisioning, not a failure mode; the suite was |
| 117 | // verified green in wave 1C (commit e77b9fd83). Skipped under `--fast`. |
| 118 | describe.skipIf(!!process.env.FAST).sequential( |
| 119 | "EFS Lambda mount", |
| 120 | { |
| 121 | tags: ["provider:aws", "provider:aws:ec2", "provider:aws:efs", "provider:aws:lambda", "live"], |
| 122 | }, |
| 123 | () => { |
| 124 | beforeAll( |
| 125 | Effect.gen(function* () { |
| 126 | yield* sharedStack.destroy(); |
| 127 | |
| 128 | const deployed = yield* sharedStack.deploy(infra("first")); |
| 129 | expect(deployed.fn.functionUrl).toBeTruthy(); |
| 130 | baseUrl = deployed.fn.functionUrl!.replace(/\/+$/, ""); |
| 131 | fileSystemId = deployed.files.fileSystemId; |
| 132 | }), |
| 133 | // The one slow deploy of the suite: file system (~15s) + mount target |
| 134 | // (~90s, ENI provisioning) + VPC-attached Lambda (ENI + EFS mount |
| 135 | // validation) — observed ~250s end to end. |
| 136 | { timeout: 300_000 }, |
| 137 | ); |
| 138 | |
| 139 | afterAll(sharedStack.destroy(), { timeout: 240_000 }); |
| 140 | |
| 141 | test.provider( |
| 142 | "writes and reads a file through the mount", |
| 143 | () => |
| 144 | Effect.gen(function* () { |
| 145 | // first request: rides through URL propagation + VPC/EFS cold start |
| 146 | const mounted = (yield* getJsonWithRetry(`${baseUrl}/mount`, 60)) as { |
| 147 | mounted: boolean; |
| 148 | }; |
| 149 | expect(mounted.mounted).toBe(true); |
| 150 | |
| 151 | const written = (yield* getJsonWithRetry( |
| 152 | `${baseUrl}/write?content=hello-from-efs`, |
| 153 | 5, |
| 154 | )) as { written: string }; |
| 155 | expect(written.written).toBe("hello-from-efs"); |
| 156 | |
| 157 | const read = (yield* getJsonWithRetry(`${baseUrl}/read`, 5)) as { |
| 158 | content: string; |
| 159 | marker: string; |
| 160 | }; |
| 161 | expect(read.content).toBe("hello-from-efs"); |
| 162 | expect(read.marker).toBe("first"); |
| 163 | }), |
| 164 | { timeout: 180_000 }, |
| 165 | ); |
| 166 | |
| 167 | test.provider( |
| 168 | "file persists across a redeploy (fresh sandboxes)", |
| 169 | () => |
| 170 | Effect.gen(function* () { |
| 171 | // config-only redeploy: new env marker forces an update, which spins |
| 172 | // up fresh execution environments — the EFS file must survive. |
| 173 | const redeployed = yield* sharedStack.deploy(infra("second")); |
| 174 | expect(redeployed.files.fileSystemId).toBe(fileSystemId); |
| 175 | |
| 176 | // A drained-but-alive sandbox from the first deploy may briefly |
| 177 | // answer with the old marker — poll until a fresh (marker=second) |
| 178 | // environment serves the read. |
| 179 | const read = yield* getJsonWithRetry(`${baseUrl}/read`, 10).pipe( |
| 180 | Effect.map((r) => r as { content: string; marker: string }), |
| 181 | Effect.repeat({ |
| 182 | schedule: Schedule.fixed("3 seconds"), |
| 183 | until: (r) => r.marker === "second", |
| 184 | times: 30, |
| 185 | }), |
| 186 | ); |
| 187 | expect(read.content).toBe("hello-from-efs"); |
| 188 | expect(read.marker).toBe("second"); |
| 189 | }), |
| 190 | { timeout: 180_000 }, |
| 191 | ); |
| 192 | }, |
| 193 | ); |