1import type { StateValue } from "@distilled.cloud/aws/cloudwatch";
2
3export interface AlarmStateChangesOptions {
4 /** Only match state changes for these alarm names. Matches all alarms when omitted. */
5 alarmNames?: string[];
6 /** Only match transitions INTO these states (e.g. `["ALARM"]`). */
7 states?: StateValue[];
8 /** Only match transitions FROM these states (e.g. `["OK"]`). */
9 previousStates?: StateValue[];
10}
11
12/**
13 * Builds an EventBridge event pattern for CloudWatch alarm state changes,
14 * for use with the EventBridge event source / routing helpers.
15 *
16 * @example Consume alarm state changes with a Lambda handler
17 * ```typescript
18 * yield* AWS.EventBridge.consumeBusEvents(
19 * bus,
20 * AWS.CloudWatch.alarmStateChanges({ states: ["ALARM"] }),
21 * (events) =>
22 * events.pipe(
23 * Stream.runForEach((event) => Effect.log(event.detail)),
24 * ),
25 * );
26 * ```
27 */
28export const alarmStateChanges = ({
29 alarmNames,
30 states,
31 previousStates,
32}: AlarmStateChangesOptions = {}) => ({
33 source: ["aws.cloudwatch"],
34 "detail-type": ["CloudWatch Alarm State Change"],
35 detail: {
36 ...(alarmNames ? { alarmName: alarmNames } : {}),
37 ...(states ? { state: { value: states } } : {}),
38 ...(previousStates ? { previousState: { value: previousStates } } : {}),
39 },
40});