The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Lambda from "@alchemy.run/aws/Lambda"; |
| 2 | import * as OpenSearch from "@alchemy.run/aws/OpenSearch"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 5 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 6 | import * as Layer from "effect/Layer"; |
| 7 | import * as Stream from "effect/Stream"; |
| 8 | import path from "pathe"; |
| 9 | |
| 10 | const main = path.resolve(import.meta.dirname, "handler.ts"); |
| 11 | |
| 12 | // A well-formed-but-nonexistent domain name — the probe routes drive the |
| 13 | // bindings against it so the fixture exercises the IAM grants and the typed |
| 14 | // error decode at zero cost (no domain is ever created; domains take 15-25 |
| 15 | // minutes to provision and bill per instance-hour). |
| 16 | const NONEXISTENT_DOMAIN = "alchemy-nonexistent-os-probe"; |
| 17 | |
| 18 | export class OpenSearchBindingsTestFunction extends Lambda.Function<Lambda.Function>()( |
| 19 | "OpenSearchBindingsTestFunction", |
| 20 | ) {} |
| 21 | |
| 22 | /** |
| 23 | * Account-level binding fixture: no OpenSearch domain is ever created. List |
| 24 | * routes prove each grant and response decode against real (possibly empty) |
| 25 | * account/catalog data; probe routes drive domain-addressed operations |
| 26 | * against a nonexistent domain and must surface the service's typed |
| 27 | * `ResourceNotFoundException` — an IAM gap would surface AccessDeniedException |
| 28 | * and fail the route with an opaque 500. |
| 29 | */ |
| 30 | export default OpenSearchBindingsTestFunction.make( |
| 31 | { |
| 32 | main, |
| 33 | functionUrl: true, |
| 34 | }, |
| 35 | Effect.gen(function* () { |
| 36 | // Event source: subscribe the host to OpenSearch Service notifications |
| 37 | // flowing through the account's default bus. The deploy proves the |
| 38 | // EventBridge rule + invoke permission wiring. |
| 39 | yield* OpenSearch.consumeDomainEvents( |
| 40 | { kinds: ["cluster-status", "software-update"] }, |
| 41 | (events) => |
| 42 | Stream.runForEach(events, (event) => |
| 43 | Effect.log(`opensearch event: ${event["detail-type"]} -> ${event.resources.join(", ")}`), |
| 44 | ), |
| 45 | ); |
| 46 | |
| 47 | const describeDomain = yield* OpenSearch.DescribeDomain(); |
| 48 | const describeDomains = yield* OpenSearch.DescribeDomains(); |
| 49 | const describeDomainConfig = yield* OpenSearch.DescribeDomainConfig(); |
| 50 | const describeDomainHealth = yield* OpenSearch.DescribeDomainHealth(); |
| 51 | const describeDomainNodes = yield* OpenSearch.DescribeDomainNodes(); |
| 52 | const describeDomainChangeProgress = yield* OpenSearch.DescribeDomainChangeProgress(); |
| 53 | const listDomainNames = yield* OpenSearch.ListDomainNames(); |
| 54 | const describeDomainAutoTunes = yield* OpenSearch.DescribeDomainAutoTunes(); |
| 55 | const listScheduledActions = yield* OpenSearch.ListScheduledActions(); |
| 56 | const startDomainMaintenance = yield* OpenSearch.StartDomainMaintenance(); |
| 57 | const getDomainMaintenanceStatus = yield* OpenSearch.GetDomainMaintenanceStatus(); |
| 58 | const listDomainMaintenances = yield* OpenSearch.ListDomainMaintenances(); |
| 59 | const startServiceSoftwareUpdate = yield* OpenSearch.StartServiceSoftwareUpdate(); |
| 60 | const cancelServiceSoftwareUpdate = yield* OpenSearch.CancelServiceSoftwareUpdate(); |
| 61 | const getUpgradeStatus = yield* OpenSearch.GetUpgradeStatus(); |
| 62 | const getUpgradeHistory = yield* OpenSearch.GetUpgradeHistory(); |
| 63 | const getCompatibleVersions = yield* OpenSearch.GetCompatibleVersions(); |
| 64 | const listVersions = yield* OpenSearch.ListVersions(); |
| 65 | const listInstanceTypeDetails = yield* OpenSearch.ListInstanceTypeDetails(); |
| 66 | |
| 67 | const bound = { |
| 68 | describeDomain, |
| 69 | describeDomains, |
| 70 | describeDomainConfig, |
| 71 | describeDomainHealth, |
| 72 | describeDomainNodes, |
| 73 | describeDomainChangeProgress, |
| 74 | listDomainNames, |
| 75 | describeDomainAutoTunes, |
| 76 | listScheduledActions, |
| 77 | startDomainMaintenance, |
| 78 | getDomainMaintenanceStatus, |
| 79 | listDomainMaintenances, |
| 80 | startServiceSoftwareUpdate, |
| 81 | cancelServiceSoftwareUpdate, |
| 82 | getUpgradeStatus, |
| 83 | getUpgradeHistory, |
| 84 | getCompatibleVersions, |
| 85 | listVersions, |
| 86 | listInstanceTypeDetails, |
| 87 | }; |
| 88 | |
| 89 | return { |
| 90 | fetch: Effect.gen(function* () { |
| 91 | const request = yield* HttpServerRequest; |
| 92 | const url = new URL(request.originalUrl); |
| 93 | const pathname = url.pathname; |
| 94 | |
| 95 | if (request.method === "GET" && pathname === "/bindings") { |
| 96 | return yield* HttpServerResponse.json({ |
| 97 | bound: Object.keys(bound), |
| 98 | }); |
| 99 | } |
| 100 | |
| 101 | if (request.method === "GET" && pathname === "/domain") { |
| 102 | // A nonexistent domain must surface the typed not-found tag. |
| 103 | const tag = yield* describeDomain({ |
| 104 | DomainName: NONEXISTENT_DOMAIN, |
| 105 | }).pipe( |
| 106 | Effect.map(() => "Found"), |
| 107 | Effect.catchTag("ResourceNotFoundException", (e) => Effect.succeed(e._tag)), |
| 108 | ); |
| 109 | return yield* HttpServerResponse.json({ tag }); |
| 110 | } |
| 111 | |
| 112 | if (request.method === "GET" && pathname === "/domains-batch") { |
| 113 | // The batch describe returns an empty status list (no not-found |
| 114 | // error) for unknown names — proves the grant and the decode. |
| 115 | const result = yield* describeDomains({ |
| 116 | DomainNames: [NONEXISTENT_DOMAIN], |
| 117 | }); |
| 118 | return yield* HttpServerResponse.json({ |
| 119 | count: (result.DomainStatusList ?? []).length, |
| 120 | }); |
| 121 | } |
| 122 | |
| 123 | if (request.method === "GET" && pathname === "/domain-config") { |
| 124 | const tag = yield* describeDomainConfig({ |
| 125 | DomainName: NONEXISTENT_DOMAIN, |
| 126 | }).pipe( |
| 127 | Effect.map(() => "Found"), |
| 128 | Effect.catchTag("ResourceNotFoundException", (e) => Effect.succeed(e._tag)), |
| 129 | ); |
| 130 | return yield* HttpServerResponse.json({ tag }); |
| 131 | } |
| 132 | |
| 133 | if (request.method === "GET" && pathname === "/domain-health") { |
| 134 | // OpenSearch reports a missing domain on this operation as the |
| 135 | // typed `BaseException` ("Domain not found: …"), not |
| 136 | // ResourceNotFoundException — both are typed tags; catch both and |
| 137 | // surface whichever fired. |
| 138 | const tag = yield* describeDomainHealth({ |
| 139 | DomainName: NONEXISTENT_DOMAIN, |
| 140 | }).pipe( |
| 141 | Effect.map(() => "Found"), |
| 142 | Effect.catchTag(["ResourceNotFoundException", "BaseException"], (e) => |
| 143 | Effect.succeed(e._tag), |
| 144 | ), |
| 145 | ); |
| 146 | return yield* HttpServerResponse.json({ tag }); |
| 147 | } |
| 148 | |
| 149 | if (request.method === "GET" && pathname === "/domain-nodes") { |
| 150 | // Missing domain surfaces as the typed `BaseException` here (see |
| 151 | // /domain-health). |
| 152 | const tag = yield* describeDomainNodes({ |
| 153 | DomainName: NONEXISTENT_DOMAIN, |
| 154 | }).pipe( |
| 155 | Effect.map(() => "Found"), |
| 156 | Effect.catchTag(["ResourceNotFoundException", "BaseException"], (e) => |
| 157 | Effect.succeed(e._tag), |
| 158 | ), |
| 159 | ); |
| 160 | return yield* HttpServerResponse.json({ tag }); |
| 161 | } |
| 162 | |
| 163 | if (request.method === "GET" && pathname === "/change-progress") { |
| 164 | // A domain with no in-flight change (or no domain at all) surfaces |
| 165 | // as the typed `BaseException` ("No progress information found"). |
| 166 | const tag = yield* describeDomainChangeProgress({ |
| 167 | DomainName: NONEXISTENT_DOMAIN, |
| 168 | }).pipe( |
| 169 | Effect.map(() => "Found"), |
| 170 | Effect.catchTag(["ResourceNotFoundException", "BaseException"], (e) => |
| 171 | Effect.succeed(e._tag), |
| 172 | ), |
| 173 | ); |
| 174 | return yield* HttpServerResponse.json({ tag }); |
| 175 | } |
| 176 | |
| 177 | if (request.method === "GET" && pathname === "/domain-names") { |
| 178 | // Listing the account's domains succeeds (possibly empty) — |
| 179 | // proves the grant and the schema decode. |
| 180 | const result = yield* listDomainNames(); |
| 181 | return yield* HttpServerResponse.json({ |
| 182 | count: (result.DomainNames ?? []).length, |
| 183 | }); |
| 184 | } |
| 185 | |
| 186 | if (request.method === "GET" && pathname === "/auto-tunes") { |
| 187 | const tag = yield* describeDomainAutoTunes({ |
| 188 | DomainName: NONEXISTENT_DOMAIN, |
| 189 | }).pipe( |
| 190 | Effect.map(() => "Found"), |
| 191 | Effect.catchTag("ResourceNotFoundException", (e) => Effect.succeed(e._tag)), |
| 192 | ); |
| 193 | return yield* HttpServerResponse.json({ tag }); |
| 194 | } |
| 195 | |
| 196 | if (request.method === "GET" && pathname === "/scheduled-actions") { |
| 197 | const tag = yield* listScheduledActions({ |
| 198 | DomainName: NONEXISTENT_DOMAIN, |
| 199 | }).pipe( |
| 200 | Effect.map(() => "Found"), |
| 201 | Effect.catchTag("ResourceNotFoundException", (e) => Effect.succeed(e._tag)), |
| 202 | ); |
| 203 | return yield* HttpServerResponse.json({ tag }); |
| 204 | } |
| 205 | |
| 206 | if (request.method === "GET" && pathname === "/start-maintenance-probe") { |
| 207 | // Starting maintenance on a nonexistent domain must surface the |
| 208 | // typed not-found tag — proves the write-side grant without ever |
| 209 | // rebooting anything. |
| 210 | const tag = yield* startDomainMaintenance({ |
| 211 | DomainName: NONEXISTENT_DOMAIN, |
| 212 | Action: "REBOOT_NODE", |
| 213 | }).pipe( |
| 214 | Effect.map(() => "Started"), |
| 215 | Effect.catchTag("ResourceNotFoundException", (e) => Effect.succeed(e._tag)), |
| 216 | ); |
| 217 | return yield* HttpServerResponse.json({ tag }); |
| 218 | } |
| 219 | |
| 220 | if (request.method === "GET" && pathname === "/maintenance-status-probe") { |
| 221 | // Missing domain surfaces as the typed `BaseException` here (see |
| 222 | // /domain-health). |
| 223 | const tag = yield* getDomainMaintenanceStatus({ |
| 224 | DomainName: NONEXISTENT_DOMAIN, |
| 225 | MaintenanceId: "nonexistent-maintenance-id", |
| 226 | }).pipe( |
| 227 | Effect.map(() => "Found"), |
| 228 | Effect.catchTag(["ResourceNotFoundException", "BaseException"], (e) => |
| 229 | Effect.succeed(e._tag), |
| 230 | ), |
| 231 | ); |
| 232 | return yield* HttpServerResponse.json({ tag }); |
| 233 | } |
| 234 | |
| 235 | if (request.method === "GET" && pathname === "/maintenances") { |
| 236 | // Missing domain surfaces as the typed `BaseException` here (see |
| 237 | // /domain-health). |
| 238 | const tag = yield* listDomainMaintenances({ |
| 239 | DomainName: NONEXISTENT_DOMAIN, |
| 240 | }).pipe( |
| 241 | Effect.map(() => "Found"), |
| 242 | Effect.catchTag(["ResourceNotFoundException", "BaseException"], (e) => |
| 243 | Effect.succeed(e._tag), |
| 244 | ), |
| 245 | ); |
| 246 | return yield* HttpServerResponse.json({ tag }); |
| 247 | } |
| 248 | |
| 249 | if (request.method === "GET" && pathname === "/software-update-probe") { |
| 250 | const tag = yield* startServiceSoftwareUpdate({ |
| 251 | DomainName: NONEXISTENT_DOMAIN, |
| 252 | }).pipe( |
| 253 | Effect.map(() => "Started"), |
| 254 | Effect.catchTag("ResourceNotFoundException", (e) => Effect.succeed(e._tag)), |
| 255 | ); |
| 256 | return yield* HttpServerResponse.json({ tag }); |
| 257 | } |
| 258 | |
| 259 | if (request.method === "GET" && pathname === "/cancel-software-update-probe") { |
| 260 | const tag = yield* cancelServiceSoftwareUpdate({ |
| 261 | DomainName: NONEXISTENT_DOMAIN, |
| 262 | }).pipe( |
| 263 | Effect.map(() => "Cancelled"), |
| 264 | Effect.catchTag("ResourceNotFoundException", (e) => Effect.succeed(e._tag)), |
| 265 | ); |
| 266 | return yield* HttpServerResponse.json({ tag }); |
| 267 | } |
| 268 | |
| 269 | if (request.method === "GET" && pathname === "/upgrade-status") { |
| 270 | const tag = yield* getUpgradeStatus({ |
| 271 | DomainName: NONEXISTENT_DOMAIN, |
| 272 | }).pipe( |
| 273 | Effect.map(() => "Found"), |
| 274 | Effect.catchTag("ResourceNotFoundException", (e) => Effect.succeed(e._tag)), |
| 275 | ); |
| 276 | return yield* HttpServerResponse.json({ tag }); |
| 277 | } |
| 278 | |
| 279 | if (request.method === "GET" && pathname === "/upgrade-history") { |
| 280 | const tag = yield* getUpgradeHistory({ |
| 281 | DomainName: NONEXISTENT_DOMAIN, |
| 282 | }).pipe( |
| 283 | Effect.map(() => "Found"), |
| 284 | Effect.catchTag("ResourceNotFoundException", (e) => Effect.succeed(e._tag)), |
| 285 | ); |
| 286 | return yield* HttpServerResponse.json({ tag }); |
| 287 | } |
| 288 | |
| 289 | if (request.method === "GET" && pathname === "/compatible-versions") { |
| 290 | // Without a DomainName the call maps every supported version to |
| 291 | // its upgrade targets — proves the grant and the decode. |
| 292 | const result = yield* getCompatibleVersions(); |
| 293 | return yield* HttpServerResponse.json({ |
| 294 | count: (result.CompatibleVersions ?? []).length, |
| 295 | }); |
| 296 | } |
| 297 | |
| 298 | if (request.method === "GET" && pathname === "/versions") { |
| 299 | // The engine-version catalog is never empty. |
| 300 | const result = yield* listVersions(); |
| 301 | return yield* HttpServerResponse.json({ |
| 302 | count: (result.Versions ?? []).length, |
| 303 | }); |
| 304 | } |
| 305 | |
| 306 | if (request.method === "GET" && pathname === "/instance-types") { |
| 307 | // The instance-type catalog for a current engine version is never |
| 308 | // empty. |
| 309 | const result = yield* listInstanceTypeDetails({ |
| 310 | EngineVersion: "OpenSearch_2.19", |
| 311 | }); |
| 312 | return yield* HttpServerResponse.json({ |
| 313 | count: (result.InstanceTypeDetails ?? []).length, |
| 314 | }); |
| 315 | } |
| 316 | |
| 317 | return yield* HttpServerResponse.json( |
| 318 | { error: "Not found", method: request.method, pathname }, |
| 319 | { status: 404 }, |
| 320 | ); |
| 321 | }).pipe(Effect.orDie), |
| 322 | }; |
| 323 | }).pipe( |
| 324 | Effect.provide( |
| 325 | Layer.mergeAll( |
| 326 | Lambda.EventSource, |
| 327 | OpenSearch.DescribeDomainHttp, |
| 328 | OpenSearch.DescribeDomainsHttp, |
| 329 | OpenSearch.DescribeDomainConfigHttp, |
| 330 | OpenSearch.DescribeDomainHealthHttp, |
| 331 | OpenSearch.DescribeDomainNodesHttp, |
| 332 | OpenSearch.DescribeDomainChangeProgressHttp, |
| 333 | OpenSearch.ListDomainNamesHttp, |
| 334 | OpenSearch.DescribeDomainAutoTunesHttp, |
| 335 | OpenSearch.ListScheduledActionsHttp, |
| 336 | OpenSearch.StartDomainMaintenanceHttp, |
| 337 | OpenSearch.GetDomainMaintenanceStatusHttp, |
| 338 | OpenSearch.ListDomainMaintenancesHttp, |
| 339 | OpenSearch.StartServiceSoftwareUpdateHttp, |
| 340 | OpenSearch.CancelServiceSoftwareUpdateHttp, |
| 341 | OpenSearch.GetUpgradeStatusHttp, |
| 342 | OpenSearch.GetUpgradeHistoryHttp, |
| 343 | OpenSearch.GetCompatibleVersionsHttp, |
| 344 | OpenSearch.ListVersionsHttp, |
| 345 | OpenSearch.ListInstanceTypeDetailsHttp, |
| 346 | ), |
| 347 | ), |
| 348 | ), |
| 349 | ); |