The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { fileURLToPath } from "node:url"; |
| 2 | import * as AWS from "@alchemy.run/aws"; |
| 3 | import { EventSourceMapping } from "@alchemy.run/aws/Lambda"; |
| 4 | import { expect } from "@alchemy.run/test"; |
| 5 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 6 | import * as DynamoDBStreams from "@distilled.cloud/aws/dynamodb-streams"; |
| 7 | import * as Lambda from "@distilled.cloud/aws/lambda"; |
| 8 | import * as Provider from "alchemy/Provider"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | import { driftActions, driftOf, repairDrift, waitForDrift } from "../drift.ts"; |
| 12 | import EventSourceMappingFunctionLive, { |
| 13 | EventSourceMappingFunction, |
| 14 | } from "./fixtures/event-source-mapping-handler.ts"; |
| 15 | import { |
| 16 | EventSourceMappingReplaceFunction, |
| 17 | FifoQueueLive, |
| 18 | StandardQueueLive, |
| 19 | } from "./fixtures/event-source-mapping-replace-handler.ts"; |
| 20 | import { |
| 21 | EventSourceMappingTableFunction, |
| 22 | makeTableFunction, |
| 23 | type TableFunctionOptions, |
| 24 | } from "./fixtures/event-source-mapping-table-handler.ts"; |
| 25 | import EventSourceMappingVisibilityFunctionLive, { |
| 26 | EventSourceMappingVisibilityFunction, |
| 27 | } from "./fixtures/event-source-mapping-visibility-handler.ts"; |
| 28 | |
| 29 | const { test } = Test.make({ providers: AWS.providers() }); |
| 30 | |
| 31 | const timeoutHandlerPath = fileURLToPath(new URL("./timeout-handler.ts", import.meta.url)); |
| 32 | |
| 33 | // Canonical `list()` test (AWS account/region-scoped collection): deploy a |
| 34 | // minimal Lambda Function + SQS Queue + EventSourceMapping (wired via the |
| 35 | // `consumeQueueMessages(queue, ...)` event source), resolve the provider from |
| 36 | // context with the typed `Provider.findProvider(EventSourceMapping)`, call |
| 37 | // `list()`, and assert the deployed mapping appears in the exhaustively |
| 38 | // paginated result. |
| 39 | test.provider( |
| 40 | "list enumerates the deployed event source mapping", |
| 41 | (stack) => |
| 42 | Effect.gen(function* () { |
| 43 | yield* stack.destroy(); |
| 44 | |
| 45 | const fn = yield* stack.deploy( |
| 46 | EventSourceMappingFunction.pipe(Effect.provide(EventSourceMappingFunctionLive)), |
| 47 | ); |
| 48 | |
| 49 | const provider = yield* Provider.findProvider(EventSourceMapping); |
| 50 | const all = yield* provider.list(); |
| 51 | |
| 52 | expect(all.length).toBeGreaterThan(0); |
| 53 | expect(all.some((m) => m.functionArn === fn.functionArn)).toBe(true); |
| 54 | |
| 55 | const mine = all.find((m) => m.functionArn === fn.functionArn)!; |
| 56 | expect(mine.uuid).toBeTruthy(); |
| 57 | expect(mine.eventSourceMappingArn).toContain("event-source-mapping"); |
| 58 | expect(mine.state).toBeTruthy(); |
| 59 | |
| 60 | yield* stack.destroy(); |
| 61 | |
| 62 | // Out-of-band proof the destroy removed both the mapping and the host |
| 63 | // function from the cloud (bounded retry for delete propagation). |
| 64 | yield* Lambda.getEventSourceMapping({ UUID: mine.uuid }).pipe( |
| 65 | Effect.flatMap(() => |
| 66 | Effect.fail(new Error(`Event source mapping ${mine.uuid} still exists`)), |
| 67 | ), |
| 68 | Effect.catchTag("ResourceNotFoundException", () => Effect.void), |
| 69 | Effect.retry({ |
| 70 | schedule: Schedule.max([Schedule.exponential(500), Schedule.recurs(8)]), |
| 71 | }), |
| 72 | ); |
| 73 | yield* Lambda.getFunction({ FunctionName: fn.functionName }).pipe( |
| 74 | Effect.flatMap(() => Effect.fail(new Error(`Function ${fn.functionName} still exists`))), |
| 75 | Effect.catchTag("ResourceNotFoundException", () => Effect.void), |
| 76 | Effect.retry({ |
| 77 | schedule: Schedule.max([Schedule.exponential(500), Schedule.recurs(8)]), |
| 78 | }), |
| 79 | ); |
| 80 | }).pipe(Effect.onError(() => stack.destroy().pipe(Effect.ignore))), |
| 81 | { |
| 82 | tags: ["provider:aws", "provider:aws:lambda", "provider:aws:sqs", "live"], |
| 83 | timeout: 240_000, |
| 84 | }, |
| 85 | ); |
| 86 | |
| 87 | // Lambda runs a Function for `timeout + timeoutMargin`, so a 30-second Function |
| 88 | // on a queue left at SQS's 30-second visibility timeout is refused by AWS. The |
| 89 | // mapping reports it as a typed error that names the setting to change. |
| 90 | test.provider( |
| 91 | "a queue whose visibility timeout is below the function's Lambda timeout fails with a typed error", |
| 92 | (stack) => |
| 93 | Effect.gen(function* () { |
| 94 | yield* stack.destroy(); |
| 95 | |
| 96 | const error = yield* stack |
| 97 | .deploy( |
| 98 | EventSourceMappingVisibilityFunction.pipe( |
| 99 | Effect.provide(EventSourceMappingVisibilityFunctionLive), |
| 100 | ), |
| 101 | ) |
| 102 | .pipe(Effect.flip); |
| 103 | expect(error._tag).toBe("QueueVisibilityTimeoutBelowFunctionTimeout"); |
| 104 | expect(error.message).toContain("31 seconds"); |
| 105 | expect(error.message).toContain("visibilityTimeout"); |
| 106 | |
| 107 | yield* stack.destroy(); |
| 108 | }).pipe(Effect.onError(() => stack.destroy().pipe(Effect.ignore))), |
| 109 | { |
| 110 | tags: ["provider:aws", "provider:aws:lambda", "provider:aws:sqs", "live"], |
| 111 | timeout: 240_000, |
| 112 | }, |
| 113 | ); |
| 114 | |
| 115 | // Switching the queue between standard and FIFO replaces it in the same deploy |
| 116 | // that updates the mapping, so the queue's ARN is unresolved when the mapping |
| 117 | // is diffed. AWS cannot move a mapping to another source, so the mapping must |
| 118 | // be recreated on the new queue. |
| 119 | test.provider( |
| 120 | "moves the mapping to a replaced queue", |
| 121 | (stack) => |
| 122 | Effect.gen(function* () { |
| 123 | yield* stack.destroy(); |
| 124 | |
| 125 | const mappingsOf = (functionName: string) => |
| 126 | Lambda.listEventSourceMappings({ FunctionName: functionName }).pipe( |
| 127 | Effect.map((page) => page.EventSourceMappings ?? []), |
| 128 | ); |
| 129 | |
| 130 | const standard = yield* stack.deploy( |
| 131 | EventSourceMappingReplaceFunction.pipe(Effect.provide(StandardQueueLive)), |
| 132 | ); |
| 133 | const [before] = yield* mappingsOf(standard.functionName); |
| 134 | expect(before?.EventSourceArn?.endsWith(".fifo")).toBe(false); |
| 135 | |
| 136 | const fifo = yield* stack.deploy( |
| 137 | EventSourceMappingReplaceFunction.pipe(Effect.provide(FifoQueueLive)), |
| 138 | ); |
| 139 | const after = yield* mappingsOf(fifo.functionName); |
| 140 | |
| 141 | expect(after.length).toBe(1); |
| 142 | expect(after[0]!.EventSourceArn?.endsWith(".fifo")).toBe(true); |
| 143 | expect(after[0]!.UUID).not.toEqual(before!.UUID); |
| 144 | |
| 145 | yield* stack.destroy(); |
| 146 | }).pipe(Effect.onError(() => stack.destroy().pipe(Effect.ignore))), |
| 147 | { |
| 148 | tags: ["provider:aws", "provider:aws:lambda", "provider:aws:sqs", "live"], |
| 149 | timeout: 240_000, |
| 150 | }, |
| 151 | ); |
| 152 | |
| 153 | // Retagging a Table updates it in place, so its stream ARN is unresolved when |
| 154 | // the mapping is diffed but does not change. The mapping must stay in place: |
| 155 | // recreating it would skip (LATEST) or replay (TRIM_HORIZON) stream records. |
| 156 | test.provider( |
| 157 | "keeps the mapping when its table is updated in place", |
| 158 | (stack) => |
| 159 | Effect.gen(function* () { |
| 160 | yield* stack.destroy(); |
| 161 | |
| 162 | const mappingsOf = (functionName: string) => |
| 163 | Lambda.listEventSourceMappings({ FunctionName: functionName }).pipe( |
| 164 | Effect.map((page) => page.EventSourceMappings ?? []), |
| 165 | ); |
| 166 | |
| 167 | const v1 = yield* stack.deploy( |
| 168 | EventSourceMappingTableFunction.pipe( |
| 169 | Effect.provide(makeTableFunction({ tags: { version: "1" } })), |
| 170 | ), |
| 171 | ); |
| 172 | const before = yield* mappingsOf(v1.functionName); |
| 173 | expect(before.length).toBe(1); |
| 174 | |
| 175 | const v2 = yield* stack.deploy( |
| 176 | EventSourceMappingTableFunction.pipe( |
| 177 | Effect.provide(makeTableFunction({ tags: { version: "2" } })), |
| 178 | ), |
| 179 | ); |
| 180 | const after = yield* mappingsOf(v2.functionName); |
| 181 | |
| 182 | expect(after.length).toBe(1); |
| 183 | expect(after[0]!.UUID).toEqual(before[0]!.UUID); |
| 184 | expect(after[0]!.EventSourceArn).toEqual(before[0]!.EventSourceArn); |
| 185 | |
| 186 | yield* stack.destroy(); |
| 187 | }).pipe(Effect.onError(() => stack.destroy().pipe(Effect.ignore))), |
| 188 | { |
| 189 | tags: ["provider:aws", "provider:aws:lambda", "provider:aws:dynamodb", "live"], |
| 190 | timeout: 240_000, |
| 191 | }, |
| 192 | ); |
| 193 | |
| 194 | const deployTableFunction = (stack: Test.ScratchStack, options: TableFunctionOptions) => |
| 195 | stack.deploy(EventSourceMappingTableFunction.pipe(Effect.provide(makeTableFunction(options)))); |
| 196 | |
| 197 | const tableMappingsOf = (functionName: string) => |
| 198 | Lambda.listEventSourceMappings({ FunctionName: functionName }).pipe( |
| 199 | Effect.map((page) => page.EventSourceMappings ?? []), |
| 200 | ); |
| 201 | |
| 202 | const streamStatusOf = (streamArn: string) => |
| 203 | DynamoDBStreams.describeStream({ StreamArn: streamArn }).pipe( |
| 204 | Effect.map((stream) => stream.StreamDescription?.StreamStatus), |
| 205 | ); |
| 206 | |
| 207 | // `startingPosition` cannot be updated in place, so changing it replaces the |
| 208 | // mapping. The replacement reads the same stream as the mapping it replaces. |
| 209 | test.provider( |
| 210 | "replaces the mapping when its startingPosition changes", |
| 211 | (stack) => |
| 212 | Effect.gen(function* () { |
| 213 | yield* stack.destroy(); |
| 214 | |
| 215 | const v1 = yield* deployTableFunction(stack, { startingPosition: "LATEST" }); |
| 216 | const before = yield* tableMappingsOf(v1.functionName); |
| 217 | expect(before.length).toBe(1); |
| 218 | |
| 219 | const v2 = yield* deployTableFunction(stack, { startingPosition: "TRIM_HORIZON" }); |
| 220 | const after = yield* tableMappingsOf(v2.functionName); |
| 221 | |
| 222 | expect(after.length).toBe(1); |
| 223 | expect(after[0]!.StartingPosition).toBe("TRIM_HORIZON"); |
| 224 | expect(after[0]!.EventSourceArn).toEqual(before[0]!.EventSourceArn); |
| 225 | |
| 226 | yield* stack.destroy(); |
| 227 | }).pipe(Effect.onError(() => stack.destroy().pipe(Effect.ignore))), |
| 228 | { |
| 229 | tags: ["provider:aws", "provider:aws:lambda", "provider:aws:dynamodb", "live"], |
| 230 | timeout: 240_000, |
| 231 | }, |
| 232 | ); |
| 233 | |
| 234 | // Updating the Table in the same deploy leaves its stream ARN unresolved when |
| 235 | // the mapping is diffed; the new `startingPosition` must still be applied. |
| 236 | test.provider( |
| 237 | "applies a new startingPosition while its table is updated", |
| 238 | (stack) => |
| 239 | Effect.gen(function* () { |
| 240 | yield* stack.destroy(); |
| 241 | |
| 242 | const v1 = yield* deployTableFunction(stack, { |
| 243 | tags: { version: "1" }, |
| 244 | startingPosition: "LATEST", |
| 245 | }); |
| 246 | expect((yield* tableMappingsOf(v1.functionName)).length).toBe(1); |
| 247 | |
| 248 | const v2 = yield* deployTableFunction(stack, { |
| 249 | tags: { version: "2" }, |
| 250 | startingPosition: "TRIM_HORIZON", |
| 251 | }); |
| 252 | const after = yield* tableMappingsOf(v2.functionName); |
| 253 | |
| 254 | expect(after.length).toBe(1); |
| 255 | expect(after[0]!.StartingPosition).toBe("TRIM_HORIZON"); |
| 256 | |
| 257 | yield* stack.destroy(); |
| 258 | }).pipe(Effect.onError(() => stack.destroy().pipe(Effect.ignore))), |
| 259 | { |
| 260 | tags: ["provider:aws", "provider:aws:lambda", "provider:aws:dynamodb", "live"], |
| 261 | timeout: 240_000, |
| 262 | }, |
| 263 | ); |
| 264 | |
| 265 | // Changing the stream view type re-creates the Table's stream while the Table |
| 266 | // itself is updated in place, so the mapping must move to the new stream. |
| 267 | test.provider( |
| 268 | "moves the mapping when its table's stream is re-created", |
| 269 | (stack) => |
| 270 | Effect.gen(function* () { |
| 271 | yield* stack.destroy(); |
| 272 | |
| 273 | const v1 = yield* deployTableFunction(stack, { streamViewType: "NEW_AND_OLD_IMAGES" }); |
| 274 | const [before] = yield* tableMappingsOf(v1.functionName); |
| 275 | |
| 276 | const v2 = yield* deployTableFunction(stack, { streamViewType: "KEYS_ONLY" }); |
| 277 | const after = yield* tableMappingsOf(v2.functionName); |
| 278 | |
| 279 | expect(after.length).toBe(1); |
| 280 | expect(after[0]!.EventSourceArn).not.toEqual(before!.EventSourceArn); |
| 281 | expect(yield* streamStatusOf(after[0]!.EventSourceArn!)).toBe("ENABLED"); |
| 282 | |
| 283 | yield* stack.destroy(); |
| 284 | }).pipe(Effect.onError(() => stack.destroy().pipe(Effect.ignore))), |
| 285 | { |
| 286 | tags: ["provider:aws", "provider:aws:lambda", "provider:aws:dynamodb", "live"], |
| 287 | timeout: 240_000, |
| 288 | }, |
| 289 | ); |
| 290 | |
| 291 | // Changing the partition key replaces the Table, and with it the stream. |
| 292 | test.provider( |
| 293 | "moves the mapping when its table is replaced", |
| 294 | (stack) => |
| 295 | Effect.gen(function* () { |
| 296 | yield* stack.destroy(); |
| 297 | |
| 298 | const v1 = yield* deployTableFunction(stack, { partitionKey: "pk" }); |
| 299 | const [before] = yield* tableMappingsOf(v1.functionName); |
| 300 | |
| 301 | const v2 = yield* deployTableFunction(stack, { partitionKey: "id" }); |
| 302 | const after = yield* tableMappingsOf(v2.functionName); |
| 303 | |
| 304 | expect(after.length).toBe(1); |
| 305 | expect(after[0]!.EventSourceArn).not.toEqual(before!.EventSourceArn); |
| 306 | expect(yield* streamStatusOf(after[0]!.EventSourceArn!)).toBe("ENABLED"); |
| 307 | |
| 308 | yield* stack.destroy(); |
| 309 | }).pipe(Effect.onError(() => stack.destroy().pipe(Effect.ignore))), |
| 310 | { |
| 311 | tags: ["provider:aws", "provider:aws:lambda", "provider:aws:dynamodb", "live"], |
| 312 | timeout: 240_000, |
| 313 | }, |
| 314 | ); |
| 315 | |
| 316 | // #2688: EventSourceMapping and Permission had no `read`, so drift skipped |
| 317 | // them: a consumer disabled or reconfigured out of band, or a removed invoke |
| 318 | // permission, went unnoticed. |
| 319 | test.provider( |
| 320 | "drift detects and repairs a disabled mapping and a removed permission", |
| 321 | (stack) => |
| 322 | Effect.gen(function* () { |
| 323 | yield* stack.destroy(); |
| 324 | |
| 325 | const { fn, mapping } = yield* stack.deploy( |
| 326 | Effect.gen(function* () { |
| 327 | const queue = yield* AWS.SQS.Queue("DriftMappingQueue", {}); |
| 328 | const fn = yield* AWS.Lambda.Function("DriftMappingFn", { |
| 329 | main: timeoutHandlerPath, |
| 330 | handler: "handler", |
| 331 | isExternal: true, |
| 332 | functionUrl: false, |
| 333 | }); |
| 334 | yield* fn.bind("AllowQueuePoll", { |
| 335 | policyStatements: [ |
| 336 | { |
| 337 | Effect: "Allow", |
| 338 | Action: ["sqs:ReceiveMessage", "sqs:DeleteMessage", "sqs:GetQueueAttributes"], |
| 339 | Resource: [queue.queueArn], |
| 340 | }, |
| 341 | ], |
| 342 | }); |
| 343 | const mapping = yield* EventSourceMapping("DriftMapping", { |
| 344 | functionName: fn.functionName, |
| 345 | eventSourceArn: queue.queueArn, |
| 346 | batchSize: 5, |
| 347 | }); |
| 348 | yield* AWS.Lambda.Permission("DriftPermission", { |
| 349 | action: "lambda:InvokeFunction", |
| 350 | functionName: fn.functionName, |
| 351 | principal: "events.amazonaws.com", |
| 352 | }); |
| 353 | return { fn, mapping }; |
| 354 | }), |
| 355 | ); |
| 356 | const actions = yield* driftActions(stack); |
| 357 | expect(Object.values(actions).every((action) => action === "unchanged")).toBe(true); |
| 358 | |
| 359 | yield* Lambda.updateEventSourceMapping({ |
| 360 | UUID: mapping.uuid, |
| 361 | Enabled: false, |
| 362 | BatchSize: 9, |
| 363 | }).pipe( |
| 364 | Effect.retry({ |
| 365 | while: (e) => e._tag === "ResourceInUseException", |
| 366 | schedule: Schedule.spaced("2 seconds"), |
| 367 | times: 15, |
| 368 | }), |
| 369 | ); |
| 370 | const permission = yield* driftOf(stack, "DriftPermission"); |
| 371 | yield* Lambda.removePermission({ |
| 372 | FunctionName: fn.functionName, |
| 373 | StatementId: permission!.attr!.statementId, |
| 374 | }); |
| 375 | |
| 376 | const drifted = yield* waitForDrift(stack, "DriftMapping", "drifted"); |
| 377 | expect(drifted?.attr?.configuration).toMatchObject({ enabled: false, batchSize: 9 }); |
| 378 | expect((yield* waitForDrift(stack, "DriftPermission", "missing"))?.action).toBe("missing"); |
| 379 | |
| 380 | const repaired = yield* repairDrift(stack); |
| 381 | expect(repaired.resources.DriftMapping?.action).toBe("repaired"); |
| 382 | expect(repaired.resources.DriftPermission?.action).toBe("recreated"); |
| 383 | const after = yield* driftActions(stack); |
| 384 | expect(Object.values(after).every((action) => action === "unchanged")).toBe(true); |
| 385 | const restored = yield* Lambda.getEventSourceMapping({ UUID: mapping.uuid }); |
| 386 | expect(restored.State).toBe("Enabled"); |
| 387 | expect(restored.BatchSize).toBe(5); |
| 388 | |
| 389 | yield* stack.destroy(); |
| 390 | }).pipe(Effect.onError(() => stack.destroy().pipe(Effect.ignore))), |
| 391 | { |
| 392 | tags: ["provider:aws", "provider:aws:lambda", "provider:aws:sqs", "live"], |
| 393 | timeout: 300_000, |
| 394 | }, |
| 395 | ); |