The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as CloudControl from "@alchemy.run/aws/CloudControl"; |
| 2 | import * as Lambda from "@alchemy.run/aws/Lambda"; |
| 3 | import type * as cloudcontrol from "@distilled.cloud/aws/cloudcontrol"; |
| 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 * as Redacted from "effect/Redacted"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | import path from "pathe"; |
| 12 | |
| 13 | const main = path.resolve(import.meta.dirname, "handler.ts"); |
| 14 | |
| 15 | const SSM_PARAMETER = "AWS::SSM::Parameter"; |
| 16 | |
| 17 | // Deterministic parameter names unique to this suite (distinct from |
| 18 | // Resource.test.ts's parameter). |
| 19 | export const FIXTURE_PARAM = "/alchemy-test/cloudcontrol/bindings/fixture"; |
| 20 | export const RUNTIME_PARAM = "/alchemy-test/cloudcontrol/bindings/runtime"; |
| 21 | |
| 22 | const plain = (value: string | Redacted.Redacted<string> | undefined): string | undefined => |
| 23 | value === undefined || typeof value === "string" ? value : Redacted.value(value); |
| 24 | |
| 25 | const readValue = ( |
| 26 | description: cloudcontrol.ResourceDescription | undefined, |
| 27 | ): string | undefined => { |
| 28 | const raw = plain(description?.Properties); |
| 29 | if (raw === undefined) return undefined; |
| 30 | return (JSON.parse(raw) as { Value?: string }).Value; |
| 31 | }; |
| 32 | |
| 33 | export class CloudControlTestFunction extends Lambda.Function<Lambda.Function>()( |
| 34 | "CloudControlTestFunction", |
| 35 | ) {} |
| 36 | |
| 37 | export default CloudControlTestFunction.make( |
| 38 | { |
| 39 | main, |
| 40 | functionUrl: true, |
| 41 | // The runtime lifecycle routes poll async Cloud Control operations. |
| 42 | timeout: Duration.seconds(120), |
| 43 | }, |
| 44 | Effect.gen(function* () { |
| 45 | // A Cloud Control-managed resource deployed with the stack, read back by |
| 46 | // the GetResource / ListResources routes. |
| 47 | yield* CloudControl.Resource("BindingsParam", { |
| 48 | typeName: SSM_PARAMETER, |
| 49 | desiredState: { |
| 50 | Name: FIXTURE_PARAM, |
| 51 | Type: "String", |
| 52 | Value: "fixture", |
| 53 | }, |
| 54 | }); |
| 55 | |
| 56 | // Cloud Control invokes the AWS::SSM::Parameter handlers with the |
| 57 | // caller's credentials, so the host also needs the handlers' underlying |
| 58 | // SSM permissions (see the type schema's `handlers` section). |
| 59 | const ssmHandlerPolicy = [ |
| 60 | { |
| 61 | Effect: "Allow" as const, |
| 62 | Action: [ |
| 63 | "ssm:PutParameter", |
| 64 | "ssm:DeleteParameter", |
| 65 | "ssm:GetParameter", |
| 66 | "ssm:GetParameters", |
| 67 | "ssm:DescribeParameters", |
| 68 | "ssm:AddTagsToResource", |
| 69 | "ssm:RemoveTagsFromResource", |
| 70 | "ssm:ListTagsForResource", |
| 71 | "ssm:LabelParameterVersion", |
| 72 | "ssm:UnlabelParameterVersion", |
| 73 | ], |
| 74 | Resource: ["*"], |
| 75 | }, |
| 76 | ]; |
| 77 | |
| 78 | const getResource = yield* CloudControl.GetResource({ |
| 79 | handlerPolicyStatements: ssmHandlerPolicy, |
| 80 | }); |
| 81 | const listResources = yield* CloudControl.ListResources({ |
| 82 | handlerPolicyStatements: ssmHandlerPolicy, |
| 83 | }); |
| 84 | const createResource = yield* CloudControl.CreateResource({ |
| 85 | handlerPolicyStatements: ssmHandlerPolicy, |
| 86 | }); |
| 87 | const updateResource = yield* CloudControl.UpdateResource({ |
| 88 | handlerPolicyStatements: ssmHandlerPolicy, |
| 89 | }); |
| 90 | const deleteResource = yield* CloudControl.DeleteResource({ |
| 91 | handlerPolicyStatements: ssmHandlerPolicy, |
| 92 | }); |
| 93 | const getResourceRequestStatus = yield* CloudControl.GetResourceRequestStatus(); |
| 94 | const listResourceRequests = yield* CloudControl.ListResourceRequests(); |
| 95 | const cancelResourceRequest = yield* CloudControl.CancelResourceRequest(); |
| 96 | |
| 97 | const bound = { |
| 98 | getResource, |
| 99 | listResources, |
| 100 | createResource, |
| 101 | updateResource, |
| 102 | deleteResource, |
| 103 | getResourceRequestStatus, |
| 104 | listResourceRequests, |
| 105 | cancelResourceRequest, |
| 106 | }; |
| 107 | |
| 108 | /** Poll a request token until the operation settles (bounded ~80s). */ |
| 109 | const waitSettled = Effect.fn(function* (requestToken: string) { |
| 110 | return yield* getResourceRequestStatus({ |
| 111 | RequestToken: requestToken, |
| 112 | }).pipe( |
| 113 | Effect.repeat({ |
| 114 | schedule: Schedule.spaced("2 seconds"), |
| 115 | until: (r): boolean => { |
| 116 | const status = r.ProgressEvent?.OperationStatus; |
| 117 | return ( |
| 118 | status !== "PENDING" && status !== "IN_PROGRESS" && status !== "CANCEL_IN_PROGRESS" |
| 119 | ); |
| 120 | }, |
| 121 | times: 40, |
| 122 | }), |
| 123 | ); |
| 124 | }); |
| 125 | |
| 126 | /** |
| 127 | * Best-effort delete of the runtime parameter so the create route is |
| 128 | * idempotent across retried invocations. |
| 129 | */ |
| 130 | const cleanupRuntimeParam = deleteResource({ |
| 131 | TypeName: SSM_PARAMETER, |
| 132 | Identifier: RUNTIME_PARAM, |
| 133 | }).pipe( |
| 134 | Effect.flatMap((r) => |
| 135 | r.ProgressEvent?.RequestToken !== undefined |
| 136 | ? waitSettled(r.ProgressEvent.RequestToken) |
| 137 | : Effect.succeed(undefined), |
| 138 | ), |
| 139 | Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(undefined)), |
| 140 | ); |
| 141 | |
| 142 | return { |
| 143 | fetch: Effect.gen(function* () { |
| 144 | const request = yield* HttpServerRequest; |
| 145 | const url = new URL(request.originalUrl); |
| 146 | const pathname = url.pathname; |
| 147 | |
| 148 | if (request.method === "GET" && pathname === "/bindings") { |
| 149 | return yield* HttpServerResponse.json({ |
| 150 | bound: Object.keys(bound), |
| 151 | }); |
| 152 | } |
| 153 | |
| 154 | if (request.method === "GET" && pathname === "/fixture") { |
| 155 | const result = yield* getResource({ |
| 156 | TypeName: SSM_PARAMETER, |
| 157 | Identifier: FIXTURE_PARAM, |
| 158 | }); |
| 159 | return yield* HttpServerResponse.json({ |
| 160 | identifier: result.ResourceDescription?.Identifier ?? null, |
| 161 | value: readValue(result.ResourceDescription) ?? null, |
| 162 | }); |
| 163 | } |
| 164 | |
| 165 | if (request.method === "GET" && pathname === "/list") { |
| 166 | // Bounded page walk looking for the fixture parameter. |
| 167 | let nextToken: string | undefined; |
| 168 | let found = false; |
| 169 | let count = 0; |
| 170 | for (let page = 0; page < 5 && !found; page++) { |
| 171 | const result: cloudcontrol.ListResourcesOutput = yield* listResources({ |
| 172 | TypeName: SSM_PARAMETER, |
| 173 | MaxResults: 100, |
| 174 | NextToken: nextToken, |
| 175 | }); |
| 176 | const descriptions = result.ResourceDescriptions ?? []; |
| 177 | count += descriptions.length; |
| 178 | found = descriptions.some((description) => description.Identifier === FIXTURE_PARAM); |
| 179 | nextToken = result.NextToken; |
| 180 | if (nextToken === undefined) break; |
| 181 | } |
| 182 | return yield* HttpServerResponse.json({ count, found }); |
| 183 | } |
| 184 | |
| 185 | if (request.method === "GET" && pathname === "/requests") { |
| 186 | const result = yield* listResourceRequests({ MaxResults: 20 }); |
| 187 | return yield* HttpServerResponse.json({ |
| 188 | count: (result.ResourceRequestStatusSummaries ?? []).length, |
| 189 | }); |
| 190 | } |
| 191 | |
| 192 | if (request.method === "GET" && pathname === "/status-not-found") { |
| 193 | // A token that never existed proves the grant end-to-end via the |
| 194 | // typed error (an IAM gap would surface AccessDeniedException). |
| 195 | const result = yield* getResourceRequestStatus({ |
| 196 | RequestToken: "00000000-0000-0000-0000-000000000000", |
| 197 | }).pipe( |
| 198 | Effect.map(() => ({ found: true })), |
| 199 | Effect.catchTag("RequestTokenNotFoundException", () => |
| 200 | Effect.succeed({ found: false }), |
| 201 | ), |
| 202 | ); |
| 203 | return yield* HttpServerResponse.json(result); |
| 204 | } |
| 205 | |
| 206 | if (request.method === "POST" && pathname === "/cancel-not-found") { |
| 207 | const tag = yield* cancelResourceRequest({ |
| 208 | RequestToken: "00000000-0000-0000-0000-000000000000", |
| 209 | }).pipe( |
| 210 | Effect.map(() => "Cancelled"), |
| 211 | Effect.catchTag( |
| 212 | ["RequestTokenNotFoundException", "ConcurrentModificationException"], |
| 213 | (e) => Effect.succeed(e._tag), |
| 214 | ), |
| 215 | ); |
| 216 | return yield* HttpServerResponse.json({ tag }); |
| 217 | } |
| 218 | |
| 219 | if (request.method === "POST" && pathname === "/runtime-create") { |
| 220 | const body = (yield* request.json) as unknown as { value: string }; |
| 221 | yield* cleanupRuntimeParam; |
| 222 | const created = yield* createResource({ |
| 223 | TypeName: SSM_PARAMETER, |
| 224 | DesiredState: JSON.stringify({ |
| 225 | Name: RUNTIME_PARAM, |
| 226 | Type: "String", |
| 227 | Value: body.value, |
| 228 | }), |
| 229 | }); |
| 230 | const settled = yield* waitSettled(created.ProgressEvent!.RequestToken!); |
| 231 | const result = yield* getResource({ |
| 232 | TypeName: SSM_PARAMETER, |
| 233 | Identifier: RUNTIME_PARAM, |
| 234 | }); |
| 235 | return yield* HttpServerResponse.json({ |
| 236 | status: settled.ProgressEvent?.OperationStatus ?? null, |
| 237 | value: readValue(result.ResourceDescription) ?? null, |
| 238 | }); |
| 239 | } |
| 240 | |
| 241 | if (request.method === "POST" && pathname === "/runtime-update") { |
| 242 | const body = (yield* request.json) as unknown as { value: string }; |
| 243 | const updated = yield* updateResource({ |
| 244 | TypeName: SSM_PARAMETER, |
| 245 | Identifier: RUNTIME_PARAM, |
| 246 | PatchDocument: JSON.stringify([{ op: "replace", path: "/Value", value: body.value }]), |
| 247 | }); |
| 248 | const settled = yield* waitSettled(updated.ProgressEvent!.RequestToken!); |
| 249 | const result = yield* getResource({ |
| 250 | TypeName: SSM_PARAMETER, |
| 251 | Identifier: RUNTIME_PARAM, |
| 252 | }); |
| 253 | return yield* HttpServerResponse.json({ |
| 254 | status: settled.ProgressEvent?.OperationStatus ?? null, |
| 255 | value: readValue(result.ResourceDescription) ?? null, |
| 256 | }); |
| 257 | } |
| 258 | |
| 259 | if (request.method === "POST" && pathname === "/runtime-delete") { |
| 260 | const deleted = yield* deleteResource({ |
| 261 | TypeName: SSM_PARAMETER, |
| 262 | Identifier: RUNTIME_PARAM, |
| 263 | }); |
| 264 | const settled = yield* waitSettled(deleted.ProgressEvent!.RequestToken!); |
| 265 | // Bounded read-until-gone to absorb eventual consistency. |
| 266 | const gone = yield* getResource({ |
| 267 | TypeName: SSM_PARAMETER, |
| 268 | Identifier: RUNTIME_PARAM, |
| 269 | }).pipe( |
| 270 | Effect.map(() => false), |
| 271 | Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(true)), |
| 272 | Effect.repeat({ |
| 273 | schedule: Schedule.spaced("1 second"), |
| 274 | until: (isGone): boolean => isGone, |
| 275 | times: 5, |
| 276 | }), |
| 277 | ); |
| 278 | return yield* HttpServerResponse.json({ |
| 279 | status: settled.ProgressEvent?.OperationStatus ?? null, |
| 280 | gone, |
| 281 | }); |
| 282 | } |
| 283 | |
| 284 | return yield* HttpServerResponse.json( |
| 285 | { error: "Not found", method: request.method, pathname }, |
| 286 | { status: 404 }, |
| 287 | ); |
| 288 | }).pipe(Effect.orDie), |
| 289 | }; |
| 290 | }).pipe( |
| 291 | Effect.provide( |
| 292 | Layer.mergeAll( |
| 293 | CloudControl.GetResourceHttp, |
| 294 | CloudControl.ListResourcesHttp, |
| 295 | CloudControl.CreateResourceHttp, |
| 296 | CloudControl.UpdateResourceHttp, |
| 297 | CloudControl.DeleteResourceHttp, |
| 298 | CloudControl.GetResourceRequestStatusHttp, |
| 299 | CloudControl.ListResourceRequestsHttp, |
| 300 | CloudControl.CancelResourceRequestHttp, |
| 301 | ), |
| 302 | ), |
| 303 | ), |
| 304 | ); |