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

main
ts 93 lines 2.84 KB
Raw
1 import type * as Effect from "effect/Effect";
2 import type * as Stream from "effect/Stream";
3 import { consumeBusEvents, type EventRecord } from "../EventBridge/EventSource.ts";
4 import type { Instance } from "./Instance.ts";
5
6 /**
7 * Lifecycle states EC2 reports through the
8 * `EC2 Instance State-change Notification` EventBridge event.
9 */
10 export type InstanceState =
11 | "pending"
12 | "running"
13 | "shutting-down"
14 | "stopping"
15 | "stopped"
16 | "terminated";
17
18 /**
19 * The `detail` payload EC2 delivers to EventBridge when an instance changes
20 * state.
21 */
22 export interface InstanceStateChangeDetail {
23 /**
24 * ID of the instance that changed state.
25 */
26 "instance-id": string;
27 /**
28 * The state the instance transitioned into.
29 */
30 state: InstanceState;
31 }
32
33 /** An instance state-change EventBridge event delivered to the handler. */
34 export type InstanceStateChangeEvent = EventRecord<InstanceStateChangeDetail>;
35
36 export interface InstanceStateEventSourceProps {
37 /**
38 * Logical id prefix for the EventBridge rule. Defaults to the instance's
39 * logical id.
40 */
41 id?: string;
42 /**
43 * Which instance states to deliver.
44 * @default all six lifecycle states
45 */
46 states?: InstanceState[];
47 }
48
49 /**
50 * Deliver EC2 instance state-change events to the host Function via
51 * EventBridge — e.g. to deregister a host from an external system when it
52 * stops, or alert when a critical box is terminated.
53 *
54 * The EventBridge pattern matches every instance state-change in the account
55 * (narrowed to `states` when given); inspect `event.detail["instance-id"]` in
56 * the handler if the Function should only react to specific instances — the
57 * bound instance's id is an Output and cannot appear in the deploy-time rule
58 * pattern.
59 *
60 * ### Observing Instance State
61 * **Example:** React to an instance stopping or terminating
62 * ```typescript
63 * yield* consumeInstanceStateEvents(
64 * instance,
65 * { states: ["stopped", "terminated"] },
66 * (events) =>
67 * Stream.runForEach(events, (event) =>
68 * Effect.log(
69 * `${event.detail["instance-id"]} is now ${event.detail.state}`,
70 * ),
71 * ),
72 * );
73 * ```
74 */
75 export const consumeInstanceStateEvents = <StreamReq = never, Req = never>(
76 instance: Instance,
77 props: InstanceStateEventSourceProps,
78 process: (
79 events: Stream.Stream<InstanceStateChangeEvent, never, StreamReq>,
80 ) => Effect.Effect<void, never, Req>,
81 ) =>
82 // The pattern uses only literal strings so it round-trips through both the
83 // deploy-time rule and the runtime matcher (Output values cannot appear in
84 // the pattern — they don't resolve inside the deployed bundle).
85 consumeBusEvents(
86 `${props.id ?? instance.LogicalId}-InstanceState`,
87 {
88 source: ["aws.ec2"],
89 "detail-type": ["EC2 Instance State-change Notification"],
90 ...(props.states ? { detail: { state: props.states } } : {}),
91 },
92 process,
93 );