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

main
ts 151 lines 4.46 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 { AutoScalingGroup } from "./AutoScalingGroup.ts";
5
6 /**
7 * EventBridge `detail-type`s EC2 Auto Scaling emits when a scaling activity
8 * finishes launching or terminating an instance.
9 */
10 export type InstanceEventType =
11 | "EC2 Instance Launch Successful"
12 | "EC2 Instance Launch Unsuccessful"
13 | "EC2 Instance Terminate Successful"
14 | "EC2 Instance Terminate Unsuccessful";
15
16 /**
17 * The `detail` payload EC2 Auto Scaling delivers to EventBridge when a
18 * scaling activity launches or terminates an instance.
19 */
20 export interface InstanceEventDetail {
21 /**
22 * ID of the scaling activity that produced the event.
23 */
24 ActivityId: string;
25 /**
26 * Name of the Auto Scaling Group the instance belongs to.
27 */
28 AutoScalingGroupName: string;
29 /**
30 * ID of the launched/terminated EC2 instance.
31 */
32 EC2InstanceId: string;
33 /**
34 * Final status of the activity (e.g. `InProgress`, `Successful`, `Failed`).
35 */
36 StatusCode: string;
37 /**
38 * Human-readable status message for unsuccessful activities.
39 */
40 StatusMessage?: string;
41 /**
42 * What initiated the activity (e.g. a policy execution or capacity change).
43 */
44 Cause?: string;
45 /**
46 * ISO-8601 time the activity started.
47 */
48 StartTime?: string;
49 /**
50 * ISO-8601 time the activity ended.
51 */
52 EndTime?: string;
53 /**
54 * Additional context, e.g. `{ "Availability Zone": "...", "Subnet ID": "..." }`.
55 */
56 Details?: Record<string, string>;
57 /**
58 * Where the instance is coming from (e.g. `EC2` or `WarmPool`).
59 */
60 Origin?: string;
61 /**
62 * Where the instance is headed (e.g. `AutoScalingGroup` or `WarmPool`).
63 */
64 Destination?: string;
65 }
66
67 /** An instance launch/terminate EventBridge event delivered to the handler. */
68 export type InstanceEvent = EventRecord<InstanceEventDetail>;
69
70 export interface InstanceEventSourceProps {
71 /**
72 * Logical id prefix for the EventBridge rule. Defaults to the Auto Scaling
73 * Group's logical id.
74 */
75 id?: string;
76 /**
77 * Which instance events to deliver.
78 * @default all four launch/terminate success/failure events
79 */
80 events?: InstanceEventType[];
81 }
82
83 const allInstanceEvents: InstanceEventType[] = [
84 "EC2 Instance Launch Successful",
85 "EC2 Instance Launch Unsuccessful",
86 "EC2 Instance Terminate Successful",
87 "EC2 Instance Terminate Unsuccessful",
88 ];
89
90 /**
91 * Deliver EC2 Auto Scaling instance launch/terminate events to the host
92 * Function via EventBridge. Unlike {@link consumeLifecycleActions} this does
93 * not pause the instance transition — it observes completed scaling
94 * activities, e.g. to deregister an instance from an external system after
95 * termination or alert on launch failures.
96 *
97 * The EventBridge pattern matches every event of the chosen detail-types in
98 * the account; inspect `event.detail.AutoScalingGroupName` in the handler if
99 * multiple groups share the Function.
100 *
101 * ### Observing the Fleet
102 * **Example:** React to instance launches and terminations
103 * ```typescript
104 * yield* consumeInstanceEvents(
105 * group,
106 * {
107 * events: [
108 * "EC2 Instance Launch Successful",
109 * "EC2 Instance Terminate Successful",
110 * ],
111 * },
112 * (events) =>
113 * Stream.runForEach(events, (event) =>
114 * Effect.log(
115 * `${event["detail-type"]}: ${event.detail.EC2InstanceId} in ${event.detail.AutoScalingGroupName}`,
116 * ),
117 * ),
118 * );
119 * ```
120 *
121 * **Example:** Alert on launch failures
122 * ```typescript
123 * yield* consumeInstanceEvents(
124 * group,
125 * { events: ["EC2 Instance Launch Unsuccessful"] },
126 * (events) =>
127 * Stream.runForEach(events, (event) =>
128 * notify(`Launch failed: ${event.detail.StatusMessage}`),
129 * ),
130 * );
131 * ```
132 */
133 export const consumeInstanceEvents = <StreamReq = never, Req = never>(
134 group: AutoScalingGroup,
135 props: InstanceEventSourceProps,
136 process: (
137 events: Stream.Stream<InstanceEvent, never, StreamReq>,
138 ) => Effect.Effect<void, never, Req>,
139 ) =>
140 // The pattern uses only literal `source` + `detail-type` so it round-trips
141 // through both the deploy-time rule and the runtime matcher (Output values
142 // cannot appear in the pattern — they don't resolve inside the deployed
143 // bundle).
144 consumeBusEvents(
145 `${props.id ?? group.LogicalId}-InstanceEvents`,
146 {
147 source: ["aws.autoscaling"],
148 "detail-type": props.events ?? allInstanceEvents,
149 },
150 process,
151 );