The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as DMS from "@alchemy.run/aws/DMS"; |
| 2 | import * as Lambda from "@alchemy.run/aws/Lambda"; |
| 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 Redacted from "effect/Redacted"; |
| 8 | import * as Stream from "effect/Stream"; |
| 9 | import path from "pathe"; |
| 10 | |
| 11 | const main = path.resolve(import.meta.dirname, "handler.ts"); |
| 12 | |
| 13 | export class DmsTestFunction extends Lambda.Function<Lambda.Function>()("DmsTestFunction") {} |
| 14 | |
| 15 | export default DmsTestFunction.make( |
| 16 | { |
| 17 | main, |
| 18 | functionUrl: true, |
| 19 | }, |
| 20 | Effect.gen(function* () { |
| 21 | // A DMS endpoint is metadata-only (free, fast) — the bindings exercise |
| 22 | // ARN injection + IAM grants without a live database or replication |
| 23 | // instance. (Instance-scoped bindings — TestConnection, RefreshSchemas, |
| 24 | // RebootReplicationInstance, DescribeReplicationInstanceTaskLogs — need |
| 25 | // a replication instance, which belongs in the slow lane.) |
| 26 | const endpoint = yield* DMS.Endpoint("BindingsSource", { |
| 27 | endpointType: "source", |
| 28 | engineName: "mysql", |
| 29 | serverName: "bindings-source-db.example.com", |
| 30 | port: 3306, |
| 31 | username: "admin", |
| 32 | password: Redacted.make("correct-horse-battery-staple"), |
| 33 | databaseName: "app", |
| 34 | }); |
| 35 | |
| 36 | // --- endpoint-scoped bindings --- |
| 37 | const describeSchemas = yield* DMS.DescribeSchemas(endpoint); |
| 38 | const refreshStatus = yield* DMS.DescribeRefreshSchemasStatus(endpoint); |
| 39 | |
| 40 | // --- account-level bindings --- |
| 41 | const describeConnections = yield* DMS.DescribeConnections(); |
| 42 | const describeEvents = yield* DMS.DescribeEvents(); |
| 43 | const endpointSettings = yield* DMS.DescribeEndpointSettings(); |
| 44 | const orderable = yield* DMS.DescribeOrderableReplicationInstances(); |
| 45 | |
| 46 | // --- task-orchestration bindings (account-level: task/config ARNs are |
| 47 | // runtime data — DMS generates the ids, so no resource to bind) --- |
| 48 | const describeReplicationTasks = yield* DMS.DescribeReplicationTasks(); |
| 49 | const startReplicationTask = yield* DMS.StartReplicationTask(); |
| 50 | const stopReplicationTask = yield* DMS.StopReplicationTask(); |
| 51 | const describeTableStatistics = yield* DMS.DescribeTableStatistics(); |
| 52 | const reloadTables = yield* DMS.ReloadTables(); |
| 53 | const describeReplications = yield* DMS.DescribeReplications(); |
| 54 | const startReplication = yield* DMS.StartReplication(); |
| 55 | const stopReplication = yield* DMS.StopReplication(); |
| 56 | |
| 57 | // Deploy-time: creates the EventBridge rule (default bus, source |
| 58 | // aws.dms) targeting this Function. Runtime firing needs a replication |
| 59 | // instance/task lifecycle; the test verifies the rule deploys. |
| 60 | yield* DMS.consumeReplicationEvents({ kinds: ["task-state"] }, (events) => |
| 61 | Stream.runForEach(events, (event) => |
| 62 | Effect.log(`dms replication event: ${event.detail.sourceId} -> ${event.detail.eventType}`), |
| 63 | ), |
| 64 | ); |
| 65 | |
| 66 | const bound = { |
| 67 | describeSchemas, |
| 68 | refreshStatus, |
| 69 | describeConnections, |
| 70 | describeEvents, |
| 71 | endpointSettings, |
| 72 | orderable, |
| 73 | describeReplicationTasks, |
| 74 | startReplicationTask, |
| 75 | stopReplicationTask, |
| 76 | describeTableStatistics, |
| 77 | reloadTables, |
| 78 | describeReplications, |
| 79 | startReplication, |
| 80 | stopReplication, |
| 81 | }; |
| 82 | |
| 83 | const EndpointArn = yield* endpoint.endpointArn; |
| 84 | |
| 85 | return { |
| 86 | fetch: Effect.gen(function* () { |
| 87 | const request = yield* HttpServerRequest; |
| 88 | const url = new URL(request.originalUrl); |
| 89 | const pathname = url.pathname; |
| 90 | |
| 91 | if (request.method === "GET" && pathname === "/bindings") { |
| 92 | return yield* HttpServerResponse.json({ |
| 93 | bound: Object.keys(bound), |
| 94 | }); |
| 95 | } |
| 96 | |
| 97 | if (request.method === "GET" && pathname === "/schemas") { |
| 98 | // The fixture endpoint has never had a schema refresh (that needs |
| 99 | // a replication instance), so DMS answers with a typed fault — |
| 100 | // which still proves ARN injection + the IAM grant end-to-end (a |
| 101 | // grant gap would surface AccessDeniedException and 500 the |
| 102 | // route). |
| 103 | const result = yield* describeSchemas().pipe( |
| 104 | Effect.map((response) => ({ |
| 105 | schemas: response.Schemas ?? [], |
| 106 | fault: null as string | null, |
| 107 | })), |
| 108 | Effect.catchTag(["ResourceNotFoundFault", "InvalidResourceStateFault"], (error) => |
| 109 | Effect.succeed({ schemas: [], fault: error._tag as string }), |
| 110 | ), |
| 111 | ); |
| 112 | return yield* HttpServerResponse.json(result); |
| 113 | } |
| 114 | |
| 115 | if (request.method === "GET" && pathname === "/refresh-status") { |
| 116 | const result = yield* refreshStatus().pipe( |
| 117 | Effect.map((response) => ({ |
| 118 | status: response.RefreshSchemasStatus?.Status ?? null, |
| 119 | fault: null as string | null, |
| 120 | })), |
| 121 | Effect.catchTag(["ResourceNotFoundFault", "InvalidResourceStateFault"], (error) => |
| 122 | Effect.succeed({ status: null, fault: error._tag as string }), |
| 123 | ), |
| 124 | ); |
| 125 | return yield* HttpServerResponse.json(result); |
| 126 | } |
| 127 | |
| 128 | if (request.method === "GET" && pathname === "/connections") { |
| 129 | // No connection test has ever run against the fixture endpoint; |
| 130 | // DMS answers a no-match filter with ResourceNotFoundFault. |
| 131 | const endpointArn = yield* EndpointArn; |
| 132 | const result = yield* describeConnections({ |
| 133 | Filters: [{ Name: "endpoint-arn", Values: [endpointArn] }], |
| 134 | }).pipe( |
| 135 | Effect.map((response) => response.Connections ?? []), |
| 136 | Effect.catchTag("ResourceNotFoundFault", () => Effect.succeed([])), |
| 137 | ); |
| 138 | return yield* HttpServerResponse.json({ count: result.length }); |
| 139 | } |
| 140 | |
| 141 | if (request.method === "GET" && pathname === "/events") { |
| 142 | const result = yield* describeEvents({ |
| 143 | SourceType: "replication-instance", |
| 144 | Duration: 60, |
| 145 | }); |
| 146 | return yield* HttpServerResponse.json({ |
| 147 | count: (result.Events ?? []).length, |
| 148 | }); |
| 149 | } |
| 150 | |
| 151 | if (request.method === "GET" && pathname === "/endpoint-settings") { |
| 152 | const result = yield* endpointSettings({ EngineName: "mysql" }); |
| 153 | return yield* HttpServerResponse.json({ |
| 154 | count: (result.EndpointSettings ?? []).length, |
| 155 | }); |
| 156 | } |
| 157 | |
| 158 | if (request.method === "GET" && pathname === "/orderable") { |
| 159 | const result = yield* orderable(); |
| 160 | return yield* HttpServerResponse.json({ |
| 161 | count: (result.OrderableReplicationInstances ?? []).length, |
| 162 | }); |
| 163 | } |
| 164 | |
| 165 | // Well-formed-but-nonexistent task/config ARNs in this account — |
| 166 | // derived from the fixture endpoint's ARN (same region + account). |
| 167 | // Driving the task-orchestration bindings against them proves ARN |
| 168 | // plumbing + IAM grants + typed error decoding without paying for a |
| 169 | // replication instance: DMS answers with a typed fault (a grant gap |
| 170 | // would surface AccessDeniedException and 500 the route instead). |
| 171 | const arnPrefix = (yield* EndpointArn).split(":").slice(0, 5).join(":"); |
| 172 | const NONEXISTENT_TASK_ARN = `${arnPrefix}:task:AAAAAAAAAAAAAAAAAAAAAAAAAA`; |
| 173 | const NONEXISTENT_CONFIG_ARN = `${arnPrefix}:replication-config:AAAAAAAAAAAAAAAAAAAAAAAAAA`; |
| 174 | |
| 175 | if (request.method === "GET" && pathname === "/tasks") { |
| 176 | // A no-match filter answers with ResourceNotFoundFault. |
| 177 | const result = yield* describeReplicationTasks({ |
| 178 | Filters: [ |
| 179 | { |
| 180 | Name: "replication-task-id", |
| 181 | Values: ["alchemy-nonexistent-task"], |
| 182 | }, |
| 183 | ], |
| 184 | }).pipe( |
| 185 | Effect.map((response) => ({ |
| 186 | count: (response.ReplicationTasks ?? []).length, |
| 187 | fault: null as string | null, |
| 188 | })), |
| 189 | Effect.catchTag("ResourceNotFoundFault", (error) => |
| 190 | Effect.succeed({ count: 0, fault: error._tag as string }), |
| 191 | ), |
| 192 | ); |
| 193 | return yield* HttpServerResponse.json(result); |
| 194 | } |
| 195 | |
| 196 | if (request.method === "POST" && pathname === "/task/start") { |
| 197 | const fault = yield* startReplicationTask({ |
| 198 | ReplicationTaskArn: NONEXISTENT_TASK_ARN, |
| 199 | StartReplicationTaskType: "start-replication", |
| 200 | }).pipe( |
| 201 | Effect.map(() => "Started"), |
| 202 | Effect.catchTag( |
| 203 | ["ResourceNotFoundFault", "InvalidResourceStateFault", "AccessDeniedFault"], |
| 204 | (error) => Effect.succeed(error._tag), |
| 205 | ), |
| 206 | ); |
| 207 | return yield* HttpServerResponse.json({ fault }); |
| 208 | } |
| 209 | |
| 210 | if (request.method === "POST" && pathname === "/task/stop") { |
| 211 | const fault = yield* stopReplicationTask({ |
| 212 | ReplicationTaskArn: NONEXISTENT_TASK_ARN, |
| 213 | }).pipe( |
| 214 | Effect.map(() => "Stopped"), |
| 215 | Effect.catchTag(["ResourceNotFoundFault", "InvalidResourceStateFault"], (error) => |
| 216 | Effect.succeed(error._tag), |
| 217 | ), |
| 218 | ); |
| 219 | return yield* HttpServerResponse.json({ fault }); |
| 220 | } |
| 221 | |
| 222 | if (request.method === "GET" && pathname === "/task/tables") { |
| 223 | const fault = yield* describeTableStatistics({ |
| 224 | ReplicationTaskArn: NONEXISTENT_TASK_ARN, |
| 225 | }).pipe( |
| 226 | Effect.map(() => "Found"), |
| 227 | Effect.catchTag( |
| 228 | ["ResourceNotFoundFault", "InvalidResourceStateFault", "AccessDeniedFault"], |
| 229 | (error) => Effect.succeed(error._tag), |
| 230 | ), |
| 231 | ); |
| 232 | return yield* HttpServerResponse.json({ fault }); |
| 233 | } |
| 234 | |
| 235 | if (request.method === "POST" && pathname === "/task/reload") { |
| 236 | const fault = yield* reloadTables({ |
| 237 | ReplicationTaskArn: NONEXISTENT_TASK_ARN, |
| 238 | TablesToReload: [{ SchemaName: "public", TableName: "nonexistent" }], |
| 239 | }).pipe( |
| 240 | Effect.map(() => "Reloaded"), |
| 241 | Effect.catchTag(["ResourceNotFoundFault", "InvalidResourceStateFault"], (error) => |
| 242 | Effect.succeed(error._tag), |
| 243 | ), |
| 244 | ); |
| 245 | return yield* HttpServerResponse.json({ fault }); |
| 246 | } |
| 247 | |
| 248 | if (request.method === "GET" && pathname === "/replications") { |
| 249 | const result = yield* describeReplications({ |
| 250 | Filters: [ |
| 251 | { |
| 252 | Name: "replication-config-arn", |
| 253 | Values: [NONEXISTENT_CONFIG_ARN], |
| 254 | }, |
| 255 | ], |
| 256 | }).pipe( |
| 257 | Effect.map((response) => ({ |
| 258 | count: (response.Replications ?? []).length, |
| 259 | fault: null as string | null, |
| 260 | })), |
| 261 | Effect.catchTag("ResourceNotFoundFault", (error) => |
| 262 | Effect.succeed({ count: 0, fault: error._tag as string }), |
| 263 | ), |
| 264 | ); |
| 265 | return yield* HttpServerResponse.json(result); |
| 266 | } |
| 267 | |
| 268 | if (request.method === "POST" && pathname === "/replication/start") { |
| 269 | const fault = yield* startReplication({ |
| 270 | ReplicationConfigArn: NONEXISTENT_CONFIG_ARN, |
| 271 | StartReplicationType: "start-replication", |
| 272 | }).pipe( |
| 273 | Effect.map(() => "Started"), |
| 274 | Effect.catchTag( |
| 275 | ["ResourceNotFoundFault", "InvalidResourceStateFault", "AccessDeniedFault"], |
| 276 | (error) => Effect.succeed(error._tag), |
| 277 | ), |
| 278 | ); |
| 279 | return yield* HttpServerResponse.json({ fault }); |
| 280 | } |
| 281 | |
| 282 | if (request.method === "POST" && pathname === "/replication/stop") { |
| 283 | const fault = yield* stopReplication({ |
| 284 | ReplicationConfigArn: NONEXISTENT_CONFIG_ARN, |
| 285 | }).pipe( |
| 286 | Effect.map(() => "Stopped"), |
| 287 | Effect.catchTag( |
| 288 | ["ResourceNotFoundFault", "InvalidResourceStateFault", "AccessDeniedFault"], |
| 289 | (error) => Effect.succeed(error._tag), |
| 290 | ), |
| 291 | ); |
| 292 | return yield* HttpServerResponse.json({ fault }); |
| 293 | } |
| 294 | |
| 295 | return yield* HttpServerResponse.json( |
| 296 | { error: "Not found", method: request.method, pathname }, |
| 297 | { status: 404 }, |
| 298 | ); |
| 299 | }).pipe(Effect.orDie), |
| 300 | }; |
| 301 | }).pipe( |
| 302 | Effect.provide( |
| 303 | Layer.mergeAll( |
| 304 | Lambda.EventSource, |
| 305 | DMS.DescribeSchemasHttp, |
| 306 | DMS.DescribeRefreshSchemasStatusHttp, |
| 307 | DMS.DescribeConnectionsHttp, |
| 308 | DMS.DescribeEventsHttp, |
| 309 | DMS.DescribeEndpointSettingsHttp, |
| 310 | DMS.DescribeOrderableReplicationInstancesHttp, |
| 311 | DMS.DescribeReplicationTasksHttp, |
| 312 | DMS.StartReplicationTaskHttp, |
| 313 | DMS.StopReplicationTaskHttp, |
| 314 | DMS.DescribeTableStatisticsHttp, |
| 315 | DMS.ReloadTablesHttp, |
| 316 | DMS.DescribeReplicationsHttp, |
| 317 | DMS.StartReplicationHttp, |
| 318 | DMS.StopReplicationHttp, |
| 319 | ), |
| 320 | ), |
| 321 | ), |
| 322 | ); |