The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as logs from "@distilled.cloud/aws/cloudwatch-logs"; |
| 2 | import { toSeconds } from "alchemy/Util/Duration"; |
| 3 | import type * as Duration from "effect/Duration"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | |
| 6 | /** Retention policy for a CloudWatch log group an Alchemy resource writes to. */ |
| 7 | export interface LogRetentionConfig { |
| 8 | /** |
| 9 | * How long to retain logs, e.g. `"2 weeks"`, or `"forever"` to clear the |
| 10 | * retention policy. Rounded up to the nearest CloudWatch-supported |
| 11 | * retention. When omitted the log group's existing retention is left |
| 12 | * untouched (new log groups default to never-expire). |
| 13 | */ |
| 14 | retention?: Duration.Input | "forever"; |
| 15 | } |
| 16 | |
| 17 | /** CloudWatch Logs' allowed retention values, in days, ascending. */ |
| 18 | const LOG_RETENTION_DAYS = [ |
| 19 | 1, 3, 5, 7, 14, 30, 60, 90, 120, 150, 180, 365, 400, 545, 731, 1096, 1827, 2192, 2557, 2922, 3288, |
| 20 | 3653, |
| 21 | ]; |
| 22 | |
| 23 | /** |
| 24 | * Apply a {@link LogRetentionConfig.retention} to an existing log group: |
| 25 | * round the duration UP to the nearest CloudWatch-supported retention, or |
| 26 | * clear the policy for `"forever"`. Leaves the group untouched when unset. |
| 27 | */ |
| 28 | export const syncLogGroupRetention = Effect.fn(function* ({ |
| 29 | logGroupName, |
| 30 | retention, |
| 31 | }: { |
| 32 | logGroupName: string; |
| 33 | retention: Duration.Input | "forever" | undefined; |
| 34 | }) { |
| 35 | if (retention === undefined) { |
| 36 | return; |
| 37 | } |
| 38 | if (retention === "forever") { |
| 39 | yield* logs |
| 40 | .deleteRetentionPolicy({ logGroupName }) |
| 41 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void)); |
| 42 | return; |
| 43 | } |
| 44 | const days = Math.max(1, Math.ceil((toSeconds(retention) ?? 0) / 86_400)); |
| 45 | yield* logs.putRetentionPolicy({ |
| 46 | logGroupName, |
| 47 | retentionInDays: |
| 48 | LOG_RETENTION_DAYS.find((allowed) => allowed >= days) ?? |
| 49 | LOG_RETENTION_DAYS[LOG_RETENTION_DAYS.length - 1]!, |
| 50 | }); |
| 51 | }); |