The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as AWS from "@alchemy.run/aws"; |
| 2 | import { Stage } from "alchemy/Stage"; |
| 3 | import * as Context from "effect/Context"; |
| 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 | |
| 10 | // Gated (skipped under `--fast`) fixture: a two-Region Multi-Region Access Point |
| 11 | // plus a Lambda that exercises the two MRAP failover bindings — |
| 12 | // GetMultiRegionAccessPointRoutes and SubmitMultiRegionAccessPointRoutes. |
| 13 | // MRAP provisioning is asynchronous and takes 10-20 minutes, hence the gate. |
| 14 | export class S3ControlMrapBindingsFunction extends AWS.Lambda.Function<AWS.Lambda.Function>()( |
| 15 | "S3ControlMrapBindingsFunction", |
| 16 | ) {} |
| 17 | |
| 18 | export class BoundMrap extends Context.Service< |
| 19 | BoundMrap, |
| 20 | { mrap: AWS.S3Control.MultiRegionAccessPoint } |
| 21 | >()("BoundMrap") {} |
| 22 | |
| 23 | /** The testing account (bucket names are global). */ |
| 24 | export const MRAP_ACCOUNT_ID = "391965393224"; |
| 25 | |
| 26 | /** Region of the MRAP's second member bucket. */ |
| 27 | export const SECOND_REGION = "us-east-1"; |
| 28 | |
| 29 | /** |
| 30 | * The second member bucket, created out-of-band by the test: stack resources |
| 31 | * deploy to one Region, and failover controls answer 405 Method Not Allowed |
| 32 | * on a Multi-Region Access Point with a single Region. |
| 33 | */ |
| 34 | export const secondRegionBucketName = (stage: string) => |
| 35 | `alchemy-mrap-use1-${stage |
| 36 | .toLowerCase() |
| 37 | .replace(/[^a-z0-9-]/g, "-") |
| 38 | .slice(0, 24)}-${MRAP_ACCOUNT_ID}`; |
| 39 | |
| 40 | export const BoundMrapLive = Layer.effect( |
| 41 | BoundMrap, |
| 42 | Effect.gen(function* () { |
| 43 | const bucket = yield* AWS.S3.Bucket("S3ControlMrapBucket", {}); |
| 44 | const mrap = yield* AWS.S3Control.MultiRegionAccessPoint("S3ControlMrapBindingsMrap", { |
| 45 | regions: [{ bucket: bucket.bucketName }, { bucket: secondRegionBucketName(yield* Stage) }], |
| 46 | }); |
| 47 | return { mrap }; |
| 48 | }), |
| 49 | ); |
| 50 | |
| 51 | export default S3ControlMrapBindingsFunction.make( |
| 52 | { |
| 53 | main: import.meta.url, |
| 54 | functionUrl: true, |
| 55 | timeout: Duration.seconds(60), |
| 56 | }, |
| 57 | Effect.gen(function* () { |
| 58 | const { mrap } = yield* BoundMrap; |
| 59 | |
| 60 | const getRoutes = yield* AWS.S3Control.GetMultiRegionAccessPointRoutes(mrap); |
| 61 | const submitRoutes = yield* AWS.S3Control.SubmitMultiRegionAccessPointRoutes(mrap); |
| 62 | // Resolved at request time, like the bindings resolve it. |
| 63 | const mrapArn = yield* mrap.multiRegionAccessPointArn; |
| 64 | |
| 65 | return { |
| 66 | fetch: Effect.gen(function* () { |
| 67 | const request = yield* HttpServerRequest; |
| 68 | const url = new URL(request.originalUrl); |
| 69 | const pathname = url.pathname; |
| 70 | |
| 71 | if (request.method === "GET" && pathname === "/routes") { |
| 72 | const { Routes } = yield* getRoutes(); |
| 73 | return yield* HttpServerResponse.json({ |
| 74 | mrapArn: yield* mrapArn, |
| 75 | routes: (Routes ?? []).map((r) => ({ |
| 76 | region: r.Region, |
| 77 | trafficDialPercentage: r.TrafficDialPercentage, |
| 78 | })), |
| 79 | }); |
| 80 | } |
| 81 | |
| 82 | if (request.method === "GET" && pathname === "/dial-100") { |
| 83 | // Re-submit every route at 100% — an idempotent write that proves |
| 84 | // the Submit binding end-to-end without shifting real traffic. |
| 85 | const { Routes } = yield* getRoutes(); |
| 86 | yield* submitRoutes({ |
| 87 | RouteUpdates: (Routes ?? []).map((r) => ({ |
| 88 | Region: r.Region, |
| 89 | TrafficDialPercentage: 100, |
| 90 | })), |
| 91 | }); |
| 92 | const after = yield* getRoutes(); |
| 93 | return yield* HttpServerResponse.json({ |
| 94 | routes: (after.Routes ?? []).map((r) => ({ |
| 95 | region: r.Region, |
| 96 | trafficDialPercentage: r.TrafficDialPercentage, |
| 97 | })), |
| 98 | }); |
| 99 | } |
| 100 | |
| 101 | return yield* HttpServerResponse.json( |
| 102 | { error: "Not found", method: request.method, pathname }, |
| 103 | { status: 404 }, |
| 104 | ); |
| 105 | }).pipe( |
| 106 | // Surface the binding's failure to the test instead of a bare 500. |
| 107 | Effect.catch((error) => |
| 108 | HttpServerResponse.json({ error: String(error) }, { status: 500 }).pipe(Effect.orDie), |
| 109 | ), |
| 110 | ), |
| 111 | }; |
| 112 | }).pipe( |
| 113 | Effect.provide( |
| 114 | Layer.mergeAll( |
| 115 | AWS.S3Control.GetMultiRegionAccessPointRoutesHttp, |
| 116 | AWS.S3Control.SubmitMultiRegionAccessPointRoutesHttp, |
| 117 | BoundMrapLive, |
| 118 | ), |
| 119 | ), |
| 120 | ), |
| 121 | ); |