The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as eventbridge from "@distilled.cloud/aws/eventbridge"; |
| 2 | import * as Output from "alchemy/Output"; |
| 3 | import { isResourceOfType } from "alchemy/Resource"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as Layer from "effect/Layer"; |
| 6 | import * as AWSBindings from "../AWSBindings.ts"; |
| 7 | import { AWSEnvironment } from "../Environment.ts"; |
| 8 | import type { Endpoint } from "./Endpoint.ts"; |
| 9 | import type { EventBus } from "./EventBus.ts"; |
| 10 | import { PutEvents, type PutEventsRequest } from "./PutEvents.ts"; |
| 11 | |
| 12 | /** |
| 13 | * HTTP implementation of {@link PutEvents}. At deploy time it grants |
| 14 | * `events:PutEvents` on the bound bus (or the default bus, or both buses of a |
| 15 | * global endpoint); at runtime it calls the EventBridge API with the host |
| 16 | * Function's credentials. Provide this layer on the Function using the |
| 17 | * binding. |
| 18 | */ |
| 19 | export const PutEventsHttp = Layer.effect( |
| 20 | PutEvents, |
| 21 | Effect.gen(function* () { |
| 22 | const bind = yield* AWSBindings.Bind; |
| 23 | const putEvents = yield* AWSBindings.withCredentials(eventbridge.putEvents); |
| 24 | |
| 25 | /** |
| 26 | * Publish through a global endpoint: requests go to the endpoint's URL |
| 27 | * (signed with SigV4A) and EventBridge routes them to the healthy |
| 28 | * region's bus. Both buses are granted, since either may receive them. |
| 29 | */ |
| 30 | const bindEndpoint = Effect.fn(function* (endpoint: Endpoint) { |
| 31 | const EndpointId = yield* endpoint.endpointId; |
| 32 | const EventBusArns = yield* endpoint.eventBusArns; |
| 33 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 34 | yield* bind`AWS.EventBridge.PutEvents(${endpoint})`({ |
| 35 | policyStatements: [ |
| 36 | { |
| 37 | Effect: "Allow", |
| 38 | Action: ["events:PutEvents"], |
| 39 | Resource: endpoint.eventBusArns, |
| 40 | }, |
| 41 | ], |
| 42 | }); |
| 43 | } |
| 44 | return Effect.fn(`AWS.EventBridge.PutEvents(${endpoint.LogicalId})`)(function* ( |
| 45 | request: PutEventsRequest, |
| 46 | ) { |
| 47 | const endpointId = yield* EndpointId; |
| 48 | // Both buses share a name; the endpoint resolves it in whichever |
| 49 | // region it routes to. |
| 50 | const [primary] = yield* EventBusArns; |
| 51 | const eventBusName = primary!.slice(primary!.indexOf(":event-bus/") + ":event-bus/".length); |
| 52 | return yield* putEvents({ |
| 53 | ...request, |
| 54 | EndpointId: endpointId, |
| 55 | Entries: request.Entries.map((entry) => ({ ...entry, EventBusName: eventBusName })), |
| 56 | }); |
| 57 | }); |
| 58 | }); |
| 59 | |
| 60 | return Effect.fn(function* (target?: EventBus | Endpoint) { |
| 61 | if (isResourceOfType(target, "AWS.EventBridge.Endpoint")) { |
| 62 | return yield* bindEndpoint(target as Endpoint); |
| 63 | } |
| 64 | const bus = target as EventBus | undefined; |
| 65 | const EventBusName = bus ? yield* bus.eventBusName : undefined; |
| 66 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 67 | const { accountId, region } = yield* AWSEnvironment.current as unknown as Effect.Effect<{ |
| 68 | accountId: string; |
| 69 | region: string; |
| 70 | }>; |
| 71 | // Pass the ARN as an unresolved Output — binding data is resolved |
| 72 | // by the engine before the host reconciles. Eagerly yielding here |
| 73 | // (during plan) produces a deferred object that serializes into an |
| 74 | // invalid IAM policy (MalformedPolicyDocumentException). |
| 75 | const resource = bus |
| 76 | ? Output.interpolate`${bus.eventBusArn}` |
| 77 | : (`arn:aws:events:${region}:${accountId}:event-bus/default` as const); |
| 78 | |
| 79 | yield* bind`AWS.EventBridge.PutEvents(${bus ?? "default"})`({ |
| 80 | policyStatements: [ |
| 81 | { |
| 82 | Effect: "Allow", |
| 83 | Action: ["events:PutEvents"], |
| 84 | Resource: [resource], |
| 85 | }, |
| 86 | ], |
| 87 | }); |
| 88 | } |
| 89 | return Effect.fn(`AWS.EventBridge.PutEvents(${bus?.LogicalId})`)(function* ( |
| 90 | request: PutEventsRequest, |
| 91 | ) { |
| 92 | const eventBusName = EventBusName ? yield* EventBusName : undefined; |
| 93 | return yield* putEvents({ |
| 94 | ...request, |
| 95 | Entries: request.Entries.map((entry) => ({ |
| 96 | ...entry, |
| 97 | EventBusName: eventBusName && eventBusName !== "default" ? eventBusName : undefined, |
| 98 | })), |
| 99 | }); |
| 100 | }); |
| 101 | }); |
| 102 | }), |
| 103 | ); |