| 1 | import * as Lambda from "@/AWS/Lambda"; |
| 2 | import { Bucket } from "@/AWS/S3"; |
| 3 | import * as Synthetics from "@/AWS/Synthetics"; |
| 4 | import * as Output from "@/Output"; |
| 5 | import * as Duration from "effect/Duration"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as Layer from "effect/Layer"; |
| 8 | import * as Stream from "effect/Stream"; |
| 9 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 10 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 11 | import path from "pathe"; |
| 12 | |
| 13 | const main = path.resolve(import.meta.dirname, "handler.ts"); |
| 14 | |
| 15 | // Trivial heartbeat script — a single successful step, no page load. |
| 16 | const HEARTBEAT_SCRIPT = ` |
| 17 | const synthetics = require("Synthetics"); |
| 18 | exports.handler = async function () { |
| 19 | return await synthetics.executeStep("heartbeat", async function () {}); |
| 20 | }; |
| 21 | `; |
| 22 | |
| 23 | export class SyntheticsBindingsFunction extends Lambda.Function<Lambda.Function>()( |
| 24 | "SyntheticsBindingsFunction", |
| 25 | ) {} |
| 26 | |
| 27 | export default SyntheticsBindingsFunction.make( |
| 28 | { |
| 29 | main, |
| 30 | functionUrl: true, |
| 31 | timeout: Duration.seconds(120), |
| 32 | }, |
| 33 | Effect.gen(function* () { |
| 34 | const bucket = yield* Bucket("SynthBindingsArtifacts", { |
| 35 | forceDestroy: true, |
| 36 | }); |
| 37 | // `rate(0 minute)` = run exactly once when started, so /start does not |
| 38 | // leave a continuously-running canary behind. |
| 39 | const canary = yield* Synthetics.Canary("BindingsCanary", { |
| 40 | script: HEARTBEAT_SCRIPT, |
| 41 | artifactS3Location: Output.interpolate`s3://${bucket.bucketName}/bindings`, |
| 42 | schedule: { expression: "rate(0 minute)" }, |
| 43 | runConfig: { timeout: "60 seconds" }, |
| 44 | successRetentionPeriod: "1 day", |
| 45 | failureRetentionPeriod: "1 day", |
| 46 | }); |
| 47 | |
| 48 | // Event source: subscribe the host to this canary's status/run events. |
| 49 | // The deploy proves the EventBridge rule + invoke permission wiring. |
| 50 | yield* Synthetics.consumeCanaryEvents( |
| 51 | { canaryNames: [canary.canaryName] }, |
| 52 | (events) => |
| 53 | Stream.runForEach(events, (event) => |
| 54 | Effect.log( |
| 55 | `synthetics event: ${event["detail-type"]} for ${event.detail["canary-name"]}`, |
| 56 | ), |
| 57 | ), |
| 58 | ); |
| 59 | |
| 60 | const bound = { |
| 61 | getCanary: yield* Synthetics.GetCanary(canary), |
| 62 | getCanaryRuns: yield* Synthetics.GetCanaryRuns(canary), |
| 63 | startCanary: yield* Synthetics.StartCanary(canary), |
| 64 | stopCanary: yield* Synthetics.StopCanary(canary), |
| 65 | describeCanariesLastRun: yield* Synthetics.DescribeCanariesLastRun(), |
| 66 | }; |
| 67 | |
| 68 | return { |
| 69 | fetch: Effect.gen(function* () { |
| 70 | const request = yield* HttpServerRequest; |
| 71 | const url = new URL(request.originalUrl); |
| 72 | const pathname = url.pathname; |
| 73 | |
| 74 | if (request.method === "GET" && pathname === "/bindings") { |
| 75 | return yield* HttpServerResponse.json({ |
| 76 | bound: Object.keys(bound), |
| 77 | }); |
| 78 | } |
| 79 | |
| 80 | // Canary-scoped read — the canary name is injected. |
| 81 | if (request.method === "GET" && pathname === "/canary") { |
| 82 | const result = yield* bound.getCanary().pipe( |
| 83 | Effect.map((r) => ({ |
| 84 | tag: "ok", |
| 85 | state: r.Canary?.Status?.State, |
| 86 | })), |
| 87 | Effect.catch((e) => |
| 88 | Effect.succeed({ tag: e._tag, state: undefined }), |
| 89 | ), |
| 90 | ); |
| 91 | return yield* HttpServerResponse.json(result); |
| 92 | } |
| 93 | |
| 94 | if (request.method === "GET" && pathname === "/runs") { |
| 95 | const result = yield* bound.getCanaryRuns({ MaxResults: 10 }).pipe( |
| 96 | Effect.map((r) => ({ |
| 97 | tag: "ok", |
| 98 | count: (r.CanaryRuns ?? []).length, |
| 99 | })), |
| 100 | Effect.catch((e) => |
| 101 | Effect.succeed({ tag: e._tag, count: undefined }), |
| 102 | ), |
| 103 | ); |
| 104 | return yield* HttpServerResponse.json(result); |
| 105 | } |
| 106 | |
| 107 | // Account-level read across every canary. |
| 108 | if (request.method === "GET" && pathname === "/last-run") { |
| 109 | const result = yield* bound.describeCanariesLastRun().pipe( |
| 110 | Effect.map((r) => ({ |
| 111 | tag: "ok", |
| 112 | count: (r.CanariesLastRun ?? []).length, |
| 113 | })), |
| 114 | Effect.catch((e) => |
| 115 | Effect.succeed({ tag: e._tag, count: undefined }), |
| 116 | ), |
| 117 | ); |
| 118 | return yield* HttpServerResponse.json(result); |
| 119 | } |
| 120 | |
| 121 | // Control plane: start the (READY) canary / stop it. A typed |
| 122 | // ConflictException still proves the grant round-tripped. |
| 123 | if (request.method === "GET" && pathname === "/start") { |
| 124 | const tag = yield* bound.startCanary().pipe( |
| 125 | Effect.map(() => "ok"), |
| 126 | Effect.catch((e) => Effect.succeed(e._tag)), |
| 127 | ); |
| 128 | return yield* HttpServerResponse.json({ tag }); |
| 129 | } |
| 130 | |
| 131 | if (request.method === "GET" && pathname === "/stop") { |
| 132 | const tag = yield* bound.stopCanary().pipe( |
| 133 | Effect.map(() => "ok"), |
| 134 | Effect.catch((e) => Effect.succeed(e._tag)), |
| 135 | ); |
| 136 | return yield* HttpServerResponse.json({ tag }); |
| 137 | } |
| 138 | |
| 139 | return yield* HttpServerResponse.json( |
| 140 | { error: "Not found", method: request.method, pathname }, |
| 141 | { status: 404 }, |
| 142 | ); |
| 143 | }).pipe(Effect.orDie), |
| 144 | }; |
| 145 | }).pipe( |
| 146 | Effect.provide( |
| 147 | Layer.mergeAll( |
| 148 | Lambda.EventSource, |
| 149 | Synthetics.GetCanaryHttp, |
| 150 | Synthetics.GetCanaryRunsHttp, |
| 151 | Synthetics.StartCanaryHttp, |
| 152 | Synthetics.StopCanaryHttp, |
| 153 | Synthetics.DescribeCanariesLastRunHttp, |
| 154 | ), |
| 155 | ), |
| 156 | ), |
| 157 | ); |