The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as cloudwatch from "@distilled.cloud/aws/cloudwatch"; |
| 2 | import { isResolved } from "alchemy/Diff"; |
| 3 | import * as Provider from "alchemy/Provider"; |
| 4 | import { Resource } from "alchemy/Resource"; |
| 5 | import * as Data from "effect/Data"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as Stream from "effect/Stream"; |
| 8 | import { AWSEnvironment, type AccountID } from "../Environment.ts"; |
| 9 | import type { Providers } from "../Providers.ts"; |
| 10 | import type { RegionID } from "../Region.ts"; |
| 11 | import { createManagedTags, createName, retryConcurrent } from "./common.ts"; |
| 12 | |
| 13 | export class AlarmMuteRuleUnexpectedResponse extends Data.TaggedError( |
| 14 | "AlarmMuteRuleUnexpectedResponse", |
| 15 | )<{ |
| 16 | readonly message: string; |
| 17 | readonly cause?: unknown; |
| 18 | }> {} |
| 19 | |
| 20 | export type AlarmMuteRuleName = string; |
| 21 | export type AlarmMuteRuleArn = |
| 22 | `arn:aws:cloudwatch:${RegionID}:${AccountID}:alarm-mute-rule:${string}`; |
| 23 | |
| 24 | export interface AlarmMuteRuleProps extends Omit< |
| 25 | cloudwatch.PutAlarmMuteRuleInput, |
| 26 | "Name" | "Tags" |
| 27 | > { |
| 28 | /** |
| 29 | * Name of the mute rule. If omitted, a unique name is generated. |
| 30 | */ |
| 31 | name?: AlarmMuteRuleName; |
| 32 | /** |
| 33 | * Optional tags to apply to the mute rule. |
| 34 | */ |
| 35 | tags?: Record<string, string>; |
| 36 | } |
| 37 | |
| 38 | export interface AlarmMuteRule extends Resource< |
| 39 | "AWS.CloudWatch.AlarmMuteRule", |
| 40 | AlarmMuteRuleProps, |
| 41 | { |
| 42 | /** Physical name of the mute rule. */ |
| 43 | alarmMuteRuleName: AlarmMuteRuleName; |
| 44 | /** ARN of the mute rule. */ |
| 45 | alarmMuteRuleArn: AlarmMuteRuleArn; |
| 46 | /** Current status of the mute rule. */ |
| 47 | status: string | undefined; |
| 48 | /** The mute type of the rule. */ |
| 49 | muteType: string | undefined; |
| 50 | /** The full mute rule description as last read from CloudWatch. */ |
| 51 | alarmMuteRule: cloudwatch.GetAlarmMuteRuleOutput; |
| 52 | /** Tags on the mute rule, including the internal Alchemy ownership tags. */ |
| 53 | tags: Record<string, string>; |
| 54 | }, |
| 55 | never, |
| 56 | Providers |
| 57 | > {} |
| 58 | |
| 59 | /** |
| 60 | * A CloudWatch alarm mute rule — suppresses alarm actions on a recurring |
| 61 | * schedule (e.g. maintenance windows) instead of manually disabling and |
| 62 | * re-enabling alarm actions. |
| 63 | * ### Creating Mute Rules |
| 64 | * **Example:** Scheduled Mute |
| 65 | * ```typescript |
| 66 | * const rule = yield* AlarmMuteRule("NightlyMute", { |
| 67 | * Rule: { |
| 68 | * Schedule: { |
| 69 | * Expression: "0 2 * * SUN", |
| 70 | * Duration: "PT1H", |
| 71 | * }, |
| 72 | * }, |
| 73 | * }); |
| 74 | * ``` |
| 75 | * |
| 76 | * ### Reading Mute Rules at Runtime |
| 77 | * **Example:** Read the Mute Rule from a Function |
| 78 | * ```typescript |
| 79 | * // init — bind the rule to the function (see GetAlarmMuteRule) |
| 80 | * const getAlarmMuteRule = yield* AWS.CloudWatch.GetAlarmMuteRule(rule); |
| 81 | * |
| 82 | * // runtime |
| 83 | * const result = yield* getAlarmMuteRule(); |
| 84 | * const schedule = result.Rule?.Schedule; |
| 85 | * ``` |
| 86 | * |
| 87 | * @resource |
| 88 | */ |
| 89 | export const AlarmMuteRule = Resource<AlarmMuteRule>("AWS.CloudWatch.AlarmMuteRule"); |
| 90 | |
| 91 | export const AlarmMuteRuleProvider = () => |
| 92 | Provider.effect( |
| 93 | AlarmMuteRule, |
| 94 | Effect.gen(function* () { |
| 95 | const createMuteRuleName = (id: string, props: { name?: string } = {}) => |
| 96 | createName(id, props.name, 255); |
| 97 | |
| 98 | const alarmMuteRuleArn = (name: string) => |
| 99 | AWSEnvironment.current.pipe( |
| 100 | Effect.map( |
| 101 | (env) => |
| 102 | `arn:aws:cloudwatch:${env.region}:${env.accountId}:alarm-mute-rule:${name}` as AlarmMuteRuleArn, |
| 103 | ), |
| 104 | ); |
| 105 | |
| 106 | const readAlarmMuteRule = Effect.fn(function* (name: string) { |
| 107 | const output = yield* cloudwatch |
| 108 | .getAlarmMuteRule({ |
| 109 | AlarmMuteRuleName: name, |
| 110 | }) |
| 111 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined)); |
| 112 | |
| 113 | if (!output?.Name || !output.AlarmMuteRuleArn) { |
| 114 | return undefined; |
| 115 | } |
| 116 | |
| 117 | return { |
| 118 | alarmMuteRuleName: output.Name, |
| 119 | alarmMuteRuleArn: output.AlarmMuteRuleArn as AlarmMuteRuleArn, |
| 120 | status: output.Status, |
| 121 | muteType: output.MuteType, |
| 122 | alarmMuteRule: output, |
| 123 | tags: {}, |
| 124 | }; |
| 125 | }); |
| 126 | |
| 127 | return { |
| 128 | stables: ["alarmMuteRuleName", "alarmMuteRuleArn"], |
| 129 | diff: Effect.fn(function* ({ id, olds = {}, news = {} }) { |
| 130 | if (!isResolved(news)) return undefined; |
| 131 | const oldName = yield* createMuteRuleName(id, olds); |
| 132 | const newName = yield* createMuteRuleName(id, news); |
| 133 | |
| 134 | if (oldName !== newName) { |
| 135 | return { action: "replace" } as const; |
| 136 | } |
| 137 | }), |
| 138 | read: Effect.fn(function* ({ id, olds, output }) { |
| 139 | const name = output?.alarmMuteRuleName ?? (yield* createMuteRuleName(id, olds ?? {})); |
| 140 | return yield* readAlarmMuteRule(name); |
| 141 | }), |
| 142 | // AWS account/region collection: `listAlarmMuteRules` paginates every |
| 143 | // mute rule in the region. The summary only carries the ARN, so we |
| 144 | // derive the name from it and re-read each rule to return the full |
| 145 | // `Attributes` shape (identical to `read`). |
| 146 | list: () => |
| 147 | Effect.gen(function* () { |
| 148 | const summaries = yield* cloudwatch.listAlarmMuteRules.pages({}).pipe( |
| 149 | Stream.runCollect, |
| 150 | Effect.map((chunk) => |
| 151 | Array.from(chunk).flatMap((page) => page.AlarmMuteRuleSummaries ?? []), |
| 152 | ), |
| 153 | ); |
| 154 | |
| 155 | const rows = yield* Effect.forEach( |
| 156 | summaries, |
| 157 | (summary) => { |
| 158 | const name = summary.AlarmMuteRuleArn?.split(":alarm-mute-rule:")[1]; |
| 159 | if (!name) { |
| 160 | return Effect.undefined; |
| 161 | } |
| 162 | return readAlarmMuteRule(name); |
| 163 | }, |
| 164 | { concurrency: 10 }, |
| 165 | ); |
| 166 | |
| 167 | return rows.filter((row): row is NonNullable<typeof row> => row !== undefined); |
| 168 | }), |
| 169 | reconcile: Effect.fn(function* ({ id, news, output, session }) { |
| 170 | // Observe — pin the physical name from `output` if we already |
| 171 | // have one; otherwise derive it from desired props. |
| 172 | const name = output?.alarmMuteRuleName ?? (yield* createMuteRuleName(id, news)); |
| 173 | |
| 174 | // Ensure — `putAlarmMuteRule` is an upsert. The CloudWatch API |
| 175 | // accepts `Tags` on every put, so we send the full managed tag |
| 176 | // set every reconcile and let the API converge it. |
| 177 | const tags = yield* createManagedTags(id, news.tags); |
| 178 | |
| 179 | yield* retryConcurrent( |
| 180 | cloudwatch.putAlarmMuteRule({ |
| 181 | ...news, |
| 182 | Name: name, |
| 183 | Tags: Object.entries(tags).map(([Key, Value]) => ({ |
| 184 | Key, |
| 185 | Value, |
| 186 | })), |
| 187 | }), |
| 188 | ); |
| 189 | |
| 190 | yield* session.note(yield* alarmMuteRuleArn(name)); |
| 191 | |
| 192 | const state = yield* readAlarmMuteRule(name); |
| 193 | if (!state) { |
| 194 | return yield* new AlarmMuteRuleUnexpectedResponse({ |
| 195 | message: `failed to read reconciled alarm mute rule '${name}'`, |
| 196 | }); |
| 197 | } |
| 198 | |
| 199 | return { |
| 200 | ...state, |
| 201 | tags, |
| 202 | }; |
| 203 | }), |
| 204 | delete: Effect.fn(function* ({ output }) { |
| 205 | yield* retryConcurrent( |
| 206 | cloudwatch.deleteAlarmMuteRule({ |
| 207 | AlarmMuteRuleName: output.alarmMuteRuleName, |
| 208 | }), |
| 209 | ); |
| 210 | }), |
| 211 | }; |
| 212 | }), |
| 213 | ); |