The best way to control Claude Code, Codex and more from any computer or phone

main
ts 119 lines 4.25 KB
Raw
1 import type * as Effect from "effect/Effect";
2 import type * as Stream from "effect/Stream";
3 import {
4 consumeBusEvents,
5 type EventRecord,
6 type EventRouteProps,
7 } from "../EventBridge/EventSource.ts";
8
9 /**
10 * The documented AWS Control Tower lifecycle event names — one per
11 * completed Control Tower action (account provisioning, guardrail
12 * enablement, landing zone setup, OU registration).
13 */
14 export type ControlTowerLifecycleEventName =
15 | "CreateManagedAccount"
16 | "UpdateManagedAccount"
17 | "EnableGuardrail"
18 | "DisableGuardrail"
19 | "SetupLandingZone"
20 | "UpdateLandingZone"
21 | "RegisterOrganizationalUnit"
22 | "DeregisterOrganizationalUnit"
23 | "PrecheckOrganizationalUnit";
24
25 /**
26 * The `detail` payload AWS Control Tower delivers to EventBridge when a
27 * lifecycle action completes. Control Tower publishes them as AWS service
28 * events via CloudTrail, so the detail carries the CloudTrail record shape;
29 * the interesting outcome lives under `serviceEventDetails`.
30 */
31 export interface ControlTowerLifecycleEventDetail {
32 /** The lifecycle event name, e.g. `CreateManagedAccount`. */
33 eventName?: ControlTowerLifecycleEventName | (string & {});
34 /** Always `controltower.amazonaws.com` for lifecycle events. */
35 eventSource?: string;
36 /** The Control Tower home region that recorded the event. */
37 awsRegion?: string;
38 /** When the lifecycle action completed. */
39 eventTime?: string;
40 /**
41 * The lifecycle outcome, keyed by `{eventName}Status` (e.g.
42 * `createManagedAccountStatus.state` is `SUCCEEDED` or `FAILED`).
43 */
44 serviceEventDetails?: Record<string, unknown>;
45 /** Additional CloudTrail record fields (the schema grows over time). */
46 [key: string]: unknown;
47 }
48
49 /** An AWS Control Tower lifecycle EventBridge event delivered to the handler. */
50 export type ControlTowerLifecycleEvent = EventRecord<ControlTowerLifecycleEventDetail>;
51
52 export interface LifecycleEventSourceProps extends EventRouteProps {
53 /**
54 * Logical id for the backing EventBridge rule.
55 * @default "ControlTowerLifecycleEvents"
56 */
57 id?: string;
58 /**
59 * Which lifecycle event names to subscribe to.
60 * @default all lifecycle events
61 */
62 events?: readonly ControlTowerLifecycleEventName[];
63 }
64
65 /**
66 * Event source connecting AWS Control Tower lifecycle events to the hosting
67 * compute. Control Tower publishes every completed lifecycle action —
68 * account provisioning (`CreateManagedAccount`), guardrail changes
69 * (`EnableGuardrail`), landing zone setup/upgrade (`SetupLandingZone`,
70 * `UpdateLandingZone`), and OU registration — to the management account's
71 * default EventBridge bus in the home region (source `aws.controltower`);
72 * this subscribes the host Function to those events so it can trigger
73 * account-customization or notification automation.
74 *
75 * Control Tower publishes to EventBridge automatically — no additional
76 * resource is created besides the EventBridge rule targeting the host.
77 * Provide the host-specific implementation layer (e.g.
78 * `AWS.Lambda.EventSource`) on the Function effect.
79 *
80 * ### Consuming Lifecycle Events
81 * **Example:** Customize Newly Provisioned Accounts
82 * ```typescript
83 * import * as AWS from "@alchemy.run/aws";
84 *
85 * export default OnboardFunction.make(
86 * { main: import.meta.url },
87 * Effect.gen(function* () {
88 * yield* AWS.ControlTower.consumeLifecycleEvents(
89 * { events: ["CreateManagedAccount"] },
90 * (events) =>
91 * Stream.runForEach(events, (event) =>
92 * Effect.log(
93 * `account provisioned: ${JSON.stringify(
94 * event.detail.serviceEventDetails,
95 * )}`,
96 * ),
97 * ),
98 * );
99 * return {};
100 * }).pipe(Effect.provide(AWS.Lambda.EventSource)),
101 * );
102 * ```
103 */
104 export const consumeLifecycleEvents = <StreamReq = never, Req = never>(
105 props: LifecycleEventSourceProps,
106 process: (
107 events: Stream.Stream<ControlTowerLifecycleEvent, never, StreamReq>,
108 ) => Effect.Effect<void, never, Req>,
109 ) =>
110 consumeBusEvents(
111 props.id ?? "ControlTowerLifecycleEvents",
112 {
113 source: ["aws.controltower"],
114 "detail-type": ["AWS Service Event via CloudTrail"],
115 ...(props.events === undefined ? {} : { detail: { eventName: [...props.events] } }),
116 },
117 { description: props.description, state: props.state },
118 process,
119 );