The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import * as logs from "@distilled.cloud/aws/cloudwatch-logs"; |
| 2 | import { isResolved } from "alchemy/Diff"; |
| 3 | import { createPhysicalName } from "alchemy/PhysicalName"; |
| 4 | import * as Provider from "alchemy/Provider"; |
| 5 | import { Resource } from "alchemy/Resource"; |
| 6 | import { createInternalTags, diffTags, tagRecord } from "alchemy/Tags"; |
| 7 | import { toWireDays } from "alchemy/Util/Duration"; |
| 8 | import type * as Duration from "effect/Duration"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import * as Option from "effect/Option"; |
| 11 | import * as Schedule from "effect/Schedule"; |
| 12 | import * as Stream from "effect/Stream"; |
| 13 | import type { AccountID } from "../Environment.ts"; |
| 14 | import { AWSEnvironment } from "../Environment.ts"; |
| 15 | import type { Providers } from "../Providers.ts"; |
| 16 | import type { RegionID } from "../Region.ts"; |
| 17 | |
| 18 | export type LogGroupName = string; |
| 19 | export type LogGroupArn = `arn:aws:logs:${RegionID}:${AccountID}:log-group:${LogGroupName}`; |
| 20 | export type LogGroupClass = logs.LogGroupClass; |
| 21 | |
| 22 | export interface LogGroupProps { |
| 23 | /** |
| 24 | * Name of the log group. If omitted, a unique name is generated. |
| 25 | */ |
| 26 | logGroupName?: string; |
| 27 | /** |
| 28 | * How long CloudWatch retains log events. If omitted, logs are kept |
| 29 | * indefinitely. Accepts any `Duration.Input` (e.g. `"7 days"`, |
| 30 | * `Duration.days(7)`; a bare number is milliseconds); the wire unit is |
| 31 | * whole days (`retentionInDays`) and must resolve to one of the retention |
| 32 | * values CloudWatch accepts (1, 3, 5, 7, 14, 30, ...). |
| 33 | */ |
| 34 | retention?: Duration.Input; |
| 35 | /** |
| 36 | * Optional KMS key identifier used to encrypt the log group. |
| 37 | */ |
| 38 | kmsKeyId?: string; |
| 39 | /** |
| 40 | * Log class for the log group. Changing this value replaces the log group. |
| 41 | * @default "STANDARD" |
| 42 | */ |
| 43 | logGroupClass?: LogGroupClass; |
| 44 | /** |
| 45 | * Whether deletion protection is enabled for the log group. |
| 46 | * @default false |
| 47 | */ |
| 48 | deletionProtectionEnabled?: boolean; |
| 49 | /** |
| 50 | * User-defined tags to apply to the log group. |
| 51 | */ |
| 52 | tags?: Record<string, string>; |
| 53 | } |
| 54 | |
| 55 | export interface LogGroup extends Resource< |
| 56 | "AWS.Logs.LogGroup", |
| 57 | LogGroupProps, |
| 58 | { |
| 59 | logGroupName: LogGroupName; |
| 60 | logGroupArn: LogGroupArn; |
| 61 | retentionInDays?: number; |
| 62 | kmsKeyId?: string; |
| 63 | logGroupClass: LogGroupClass; |
| 64 | deletionProtectionEnabled: boolean; |
| 65 | tags: Record<string, string>; |
| 66 | }, |
| 67 | never, |
| 68 | Providers |
| 69 | > {} |
| 70 | |
| 71 | /** |
| 72 | * A CloudWatch Logs log group — the container for log streams and the unit |
| 73 | * that retention, encryption, metric filters, and subscriptions attach to. |
| 74 | * ### Creating Log Groups |
| 75 | * **Example:** ECS Task Log Group |
| 76 | * ```typescript |
| 77 | * const logs = yield* LogGroup("TaskLogs", { |
| 78 | * retention: "7 days", |
| 79 | * }); |
| 80 | * ``` |
| 81 | * |
| 82 | * **Example:** Encrypted Log Group with Deletion Protection |
| 83 | * ```typescript |
| 84 | * const key = yield* AWS.KMS.Key("LogsKey"); |
| 85 | * const logs = yield* LogGroup("AuditLogs", { |
| 86 | * retention: "30 days", |
| 87 | * kmsKeyId: key.keyArn, |
| 88 | * deletionProtectionEnabled: true, |
| 89 | * }); |
| 90 | * ``` |
| 91 | * |
| 92 | * ### Writing Custom Log Events |
| 93 | * Declare a `LogStream` and use the `PutLogEvents` binding inside a Lambda |
| 94 | * function (or the batching `LogEventSink` for high-volume streams). |
| 95 | * |
| 96 | * **Example:** Custom Audit Trail from a Lambda Function |
| 97 | * ```typescript |
| 98 | * // init |
| 99 | * const logGroup = yield* AWS.Logs.LogGroup("AuditLogs", { |
| 100 | * retention: "30 days", |
| 101 | * }); |
| 102 | * const stream = yield* AWS.Logs.LogStream("AuditStream", { |
| 103 | * logGroupName: logGroup.logGroupName, |
| 104 | * logStreamName: "audit", |
| 105 | * }); |
| 106 | * const putLogEvents = yield* AWS.Logs.PutLogEvents(logGroup); |
| 107 | * |
| 108 | * // runtime |
| 109 | * yield* putLogEvents({ |
| 110 | * logStreamName: "audit", |
| 111 | * logEvents: [{ timestamp, message: "user.login id=123" }], |
| 112 | * }); |
| 113 | * ``` |
| 114 | * |
| 115 | * ### Consuming Log Events |
| 116 | * **Example:** React to Error Logs |
| 117 | * ```typescript |
| 118 | * // Subscribe a Lambda handler to matching events (creates the |
| 119 | * // subscription filter + invoke permission automatically). |
| 120 | * yield* AWS.Logs.consumeLogEvents( |
| 121 | * logGroup, |
| 122 | * { filterPattern: "?ERROR ?Error" }, |
| 123 | * (events) => |
| 124 | * Stream.runForEach(events, (event) => |
| 125 | * Effect.log(`${event.logStream}: ${event.message}`), |
| 126 | * ), |
| 127 | * ); |
| 128 | * ``` |
| 129 | * |
| 130 | * ### Metrics |
| 131 | * **Example:** Count Errors with a Metric Filter |
| 132 | * ```typescript |
| 133 | * yield* AWS.Logs.MetricFilter("ErrorCount", { |
| 134 | * logGroupName: logGroup.logGroupName, |
| 135 | * filterPattern: '"ERROR"', |
| 136 | * metricTransformations: [{ |
| 137 | * metricName: "ErrorCount", |
| 138 | * metricNamespace: "MyApp", |
| 139 | * metricValue: "1", |
| 140 | * }], |
| 141 | * }); |
| 142 | * ``` |
| 143 | * |
| 144 | * @resource |
| 145 | */ |
| 146 | export const LogGroup = Resource<LogGroup>("AWS.Logs.LogGroup"); |
| 147 | |
| 148 | export const LogGroupProvider = () => |
| 149 | Provider.effect( |
| 150 | LogGroup, |
| 151 | Effect.gen(function* () { |
| 152 | const toLogGroupName = (id: string, props: { logGroupName?: string } = {}) => |
| 153 | props.logGroupName |
| 154 | ? Effect.succeed(props.logGroupName) |
| 155 | : createPhysicalName({ id, maxLength: 512 }); |
| 156 | const toLogGroupClass = (props: { logGroupClass?: LogGroupClass } = {}): LogGroupClass => |
| 157 | props.logGroupClass ?? "STANDARD"; |
| 158 | // describeLogGroups returns ARNs with a trailing `:*` while reconcile |
| 159 | // constructs them without — normalize so refresh/adoption never reports |
| 160 | // phantom drift and interpolated IAM ARNs (`${arn}:*`) stay correct. |
| 161 | const normalizeLogGroupArn = (arn: string): LogGroupArn => |
| 162 | (arn.endsWith(":*") ? arn.slice(0, -2) : arn) as LogGroupArn; |
| 163 | const observe = Effect.fn(function* (logGroupName: string) { |
| 164 | return yield* logs.describeLogGroups.items({ logGroupNamePrefix: logGroupName }).pipe( |
| 165 | Stream.filter((group) => group.logGroupName === logGroupName), |
| 166 | Stream.runHead, |
| 167 | Effect.map(Option.getOrUndefined), |
| 168 | ); |
| 169 | }); |
| 170 | |
| 171 | return { |
| 172 | stables: ["logGroupArn", "logGroupName"], |
| 173 | // AWS account/region collection: paginate `describeLogGroups` |
| 174 | // exhaustively, then hydrate each group's tags via |
| 175 | // `listTagsForResource` (bounded concurrency) into the exact `read` |
| 176 | // Attributes shape. |
| 177 | list: () => |
| 178 | Effect.gen(function* () { |
| 179 | const { accountId, region } = yield* AWSEnvironment.current; |
| 180 | const groups = yield* logs.describeLogGroups.pages({}).pipe( |
| 181 | Stream.runCollect, |
| 182 | Effect.map((chunk) => Array.from(chunk).flatMap((page) => page.logGroups ?? [])), |
| 183 | ); |
| 184 | |
| 185 | return yield* Effect.forEach( |
| 186 | groups.filter( |
| 187 | ( |
| 188 | group, |
| 189 | ): group is logs.LogGroup & { |
| 190 | logGroupName: string; |
| 191 | arn: string; |
| 192 | } => group.logGroupName != null && group.arn != null, |
| 193 | ), |
| 194 | (group) => |
| 195 | Effect.gen(function* () { |
| 196 | const tagArn = |
| 197 | `arn:aws:logs:${region}:${accountId}:log-group:${group.logGroupName}` as LogGroupArn; |
| 198 | const tags = yield* logs.listTagsForResource({ resourceArn: tagArn }).pipe( |
| 199 | Effect.map((r): Record<string, string> => |
| 200 | Object.fromEntries( |
| 201 | Object.entries(r.tags ?? {}).filter( |
| 202 | (entry): entry is [string, string] => typeof entry[1] === "string", |
| 203 | ), |
| 204 | ), |
| 205 | ), |
| 206 | Effect.catchTag("ResourceNotFoundException", () => |
| 207 | Effect.succeed({} as Record<string, string>), |
| 208 | ), |
| 209 | ); |
| 210 | return { |
| 211 | logGroupName: group.logGroupName, |
| 212 | logGroupArn: normalizeLogGroupArn(group.arn), |
| 213 | retentionInDays: group.retentionInDays, |
| 214 | kmsKeyId: group.kmsKeyId, |
| 215 | logGroupClass: group.logGroupClass ?? "STANDARD", |
| 216 | deletionProtectionEnabled: group.deletionProtectionEnabled ?? false, |
| 217 | tags, |
| 218 | }; |
| 219 | }), |
| 220 | { concurrency: 10 }, |
| 221 | ); |
| 222 | }), |
| 223 | diff: Effect.fn(function* ({ id, olds, news }) { |
| 224 | if (!isResolved(news)) return; |
| 225 | if ((yield* toLogGroupName(id, olds ?? {})) !== (yield* toLogGroupName(id, news ?? {}))) { |
| 226 | return { action: "replace" } as const; |
| 227 | } |
| 228 | if (toLogGroupClass(olds ?? {}) !== toLogGroupClass(news ?? {})) { |
| 229 | return { action: "replace" } as const; |
| 230 | } |
| 231 | }), |
| 232 | read: Effect.fn(function* ({ id, olds, output }) { |
| 233 | const logGroupName = output?.logGroupName ?? (yield* toLogGroupName(id, olds ?? {})); |
| 234 | const match = yield* observe(logGroupName); |
| 235 | if (!match?.arn) { |
| 236 | return undefined; |
| 237 | } |
| 238 | return { |
| 239 | logGroupName, |
| 240 | logGroupArn: normalizeLogGroupArn(match.arn), |
| 241 | retentionInDays: match.retentionInDays, |
| 242 | kmsKeyId: match.kmsKeyId, |
| 243 | logGroupClass: match.logGroupClass ?? "STANDARD", |
| 244 | deletionProtectionEnabled: match.deletionProtectionEnabled ?? false, |
| 245 | tags: yield* logs |
| 246 | .listTagsForResource({ resourceArn: normalizeLogGroupArn(match.arn) }) |
| 247 | .pipe( |
| 248 | Effect.map((r) => tagRecord(r.tags)), |
| 249 | Effect.catchTag("ResourceNotFoundException", () => |
| 250 | Effect.succeed({} as Record<string, string>), |
| 251 | ), |
| 252 | ), |
| 253 | }; |
| 254 | }), |
| 255 | reconcile: Effect.fn(function* ({ id, news, output, session }) { |
| 256 | const { accountId, region } = yield* AWSEnvironment.current; |
| 257 | const logGroupName = output?.logGroupName ?? (yield* toLogGroupName(id, news)); |
| 258 | // `output.logGroupArn` may be state written by an older provider |
| 259 | // version (or anything else) that persisted `describeLogGroups`' |
| 260 | // trailing `:*` form — normalize before it reaches the tagging |
| 261 | // APIs, which reject that suffix with "Invalid resourceArn". |
| 262 | const arn = normalizeLogGroupArn( |
| 263 | (output?.logGroupArn ?? |
| 264 | `arn:aws:logs:${region}:${accountId}:log-group:${logGroupName}`) as LogGroupArn, |
| 265 | ); |
| 266 | const internalTags = yield* createInternalTags(id); |
| 267 | const desiredTags = { ...internalTags, ...news.tags }; |
| 268 | // Wire unit is whole days (retentionInDays). |
| 269 | const desiredRetention = toWireDays(news.retention); |
| 270 | |
| 271 | // Observe - fetch live state. `describeLogGroups` returns |
| 272 | // retention/kms info so we can diff against desired without |
| 273 | // trusting `olds` or `output`. |
| 274 | let observed = yield* observe(logGroupName); |
| 275 | |
| 276 | // Ensure - create if missing. `createLogGroup` accepts tags and |
| 277 | // kmsKeyId on first create; tolerate `ResourceAlreadyExistsException` |
| 278 | // (race with peer reconciler) and re-read. |
| 279 | if (!observed?.arn) { |
| 280 | yield* logs |
| 281 | .createLogGroup({ |
| 282 | logGroupName, |
| 283 | kmsKeyId: news.kmsKeyId, |
| 284 | tags: desiredTags, |
| 285 | logGroupClass: news.logGroupClass, |
| 286 | deletionProtectionEnabled: news.deletionProtectionEnabled, |
| 287 | }) |
| 288 | .pipe(Effect.catchTag("ResourceAlreadyExistsException", () => Effect.void)); |
| 289 | observed = yield* observe(logGroupName); |
| 290 | } |
| 291 | |
| 292 | // Sync KMS key - create accepts kmsKeyId, but updates require the |
| 293 | // association API. Disassociating only affects newly ingested events. |
| 294 | const observedKmsKeyId = observed?.kmsKeyId; |
| 295 | if (news.kmsKeyId !== observedKmsKeyId) { |
| 296 | if (news.kmsKeyId === undefined) { |
| 297 | if (observedKmsKeyId !== undefined) { |
| 298 | yield* logs |
| 299 | .disassociateKmsKey({ logGroupName }) |
| 300 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void)); |
| 301 | } |
| 302 | } else { |
| 303 | yield* logs.associateKmsKey({ |
| 304 | logGroupName, |
| 305 | kmsKeyId: news.kmsKeyId, |
| 306 | }); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | // Sync deletion protection - destroy disables it before deletion, but |
| 311 | // normal deploys should still converge the live flag to desired state. |
| 312 | const desiredDeletionProtection = news.deletionProtectionEnabled ?? false; |
| 313 | const observedDeletionProtection = observed?.deletionProtectionEnabled ?? false; |
| 314 | if (desiredDeletionProtection !== observedDeletionProtection) { |
| 315 | yield* logs.putLogGroupDeletionProtection({ |
| 316 | logGroupIdentifier: logGroupName, |
| 317 | deletionProtectionEnabled: desiredDeletionProtection, |
| 318 | }); |
| 319 | } |
| 320 | |
| 321 | // Sync retention - observed to desired. |
| 322 | const observedRetention = observed?.retentionInDays; |
| 323 | if (desiredRetention !== observedRetention) { |
| 324 | if (desiredRetention === undefined) { |
| 325 | yield* logs |
| 326 | .deleteRetentionPolicy({ |
| 327 | logGroupName, |
| 328 | }) |
| 329 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void)); |
| 330 | } else { |
| 331 | yield* logs.putRetentionPolicy({ |
| 332 | logGroupName, |
| 333 | retentionInDays: desiredRetention, |
| 334 | }); |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | // Sync tags - list observed tags then diff against desired so |
| 339 | // adoption rewrites ownership tags correctly. |
| 340 | const observedTags = yield* logs.listTagsForResource({ resourceArn: arn }).pipe( |
| 341 | Effect.map((r): Record<string, string> => |
| 342 | Object.fromEntries( |
| 343 | Object.entries(r.tags ?? {}).filter( |
| 344 | (entry): entry is [string, string] => typeof entry[1] === "string", |
| 345 | ), |
| 346 | ), |
| 347 | ), |
| 348 | Effect.orElseSucceed(() => ({}) as Record<string, string>), |
| 349 | ); |
| 350 | const { removed, upsert } = diffTags(observedTags, desiredTags); |
| 351 | if (upsert.length > 0) { |
| 352 | yield* logs.tagResource({ |
| 353 | resourceArn: arn, |
| 354 | tags: Object.fromEntries(upsert.map((tag) => [tag.Key, tag.Value])), |
| 355 | }); |
| 356 | } |
| 357 | if (removed.length > 0) { |
| 358 | yield* logs.untagResource({ |
| 359 | resourceArn: arn, |
| 360 | tagKeys: removed, |
| 361 | }); |
| 362 | } |
| 363 | |
| 364 | yield* session.note(arn); |
| 365 | |
| 366 | return { |
| 367 | logGroupName, |
| 368 | logGroupArn: arn, |
| 369 | retentionInDays: desiredRetention, |
| 370 | kmsKeyId: news.kmsKeyId, |
| 371 | logGroupClass: observed?.logGroupClass ?? toLogGroupClass(news), |
| 372 | deletionProtectionEnabled: desiredDeletionProtection, |
| 373 | tags: desiredTags, |
| 374 | }; |
| 375 | }), |
| 376 | delete: Effect.fn(function* ({ output }) { |
| 377 | yield* logs |
| 378 | .putLogGroupDeletionProtection({ |
| 379 | logGroupIdentifier: output.logGroupName, |
| 380 | deletionProtectionEnabled: false, |
| 381 | }) |
| 382 | .pipe( |
| 383 | Effect.retry({ |
| 384 | while: (error) => |
| 385 | error._tag === "OperationAbortedException" || |
| 386 | error._tag === "ServiceUnavailableException", |
| 387 | schedule: Schedule.exponential(100), |
| 388 | times: 8, |
| 389 | }), |
| 390 | Effect.catchTag("ResourceNotFoundException", () => Effect.void), |
| 391 | ); |
| 392 | yield* logs |
| 393 | .deleteLogGroup({ |
| 394 | logGroupName: output.logGroupName, |
| 395 | }) |
| 396 | .pipe( |
| 397 | Effect.retry({ |
| 398 | while: (error) => |
| 399 | error._tag === "OperationAbortedException" || |
| 400 | error._tag === "ServiceUnavailableException", |
| 401 | schedule: Schedule.exponential(100), |
| 402 | times: 8, |
| 403 | }), |
| 404 | Effect.catchTag("ResourceNotFoundException", () => Effect.void), |
| 405 | ); |
| 406 | |
| 407 | const remaining = yield* Effect.repeat( |
| 408 | logs.describeLogGroups.items({ logGroupNamePrefix: output.logGroupName }).pipe( |
| 409 | Stream.filter((group) => group.logGroupName === output.logGroupName), |
| 410 | Stream.runHead, |
| 411 | Effect.map(Option.isSome), |
| 412 | ), |
| 413 | { |
| 414 | schedule: Schedule.fixed("250 millis"), |
| 415 | until: (present) => !present, |
| 416 | times: 20, |
| 417 | }, |
| 418 | ); |
| 419 | if (remaining) { |
| 420 | return yield* Effect.die( |
| 421 | new Error( |
| 422 | `CloudWatch log group ${output.logGroupName} remained observable after delete`, |
| 423 | ), |
| 424 | ); |
| 425 | } |
| 426 | }), |
| 427 | }; |
| 428 | }), |
| 429 | ); |