The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as memorydb from "@distilled.cloud/aws/memorydb"; |
| 2 | import { Unowned } from "alchemy/AdoptPolicy"; |
| 3 | import { isResolved } from "alchemy/Diff"; |
| 4 | import { createPhysicalName } from "alchemy/PhysicalName"; |
| 5 | import * as Provider from "alchemy/Provider"; |
| 6 | import { Resource } from "alchemy/Resource"; |
| 7 | import { createInternalTags, diffTags, hasAlchemyTags } from "alchemy/Tags"; |
| 8 | import * as Data from "effect/Data"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | import * as Stream from "effect/Stream"; |
| 12 | import type { Providers } from "../Providers.ts"; |
| 13 | import { readMemoryDbTags } from "./internal.ts"; |
| 14 | |
| 15 | export class MemoryDBParameterGroupUnexpectedResponse extends Data.TaggedError( |
| 16 | "MemoryDBParameterGroupUnexpectedResponse", |
| 17 | )<{ |
| 18 | readonly message: string; |
| 19 | readonly cause?: unknown; |
| 20 | }> {} |
| 21 | |
| 22 | export interface ParameterGroupProps { |
| 23 | /** |
| 24 | * Name of the parameter group. Must be 1-40 characters. If omitted, a |
| 25 | * deterministic physical name is generated. Changing the name replaces the |
| 26 | * parameter group. |
| 27 | */ |
| 28 | parameterGroupName?: string; |
| 29 | /** |
| 30 | * Parameter group family the group belongs to, e.g. `"memorydb_valkey7"`, |
| 31 | * `"memorydb_valkey8"`, `"memorydb_redis7"`. Changing the family replaces |
| 32 | * the parameter group. |
| 33 | */ |
| 34 | family: string; |
| 35 | /** |
| 36 | * Human-readable description of the parameter group. The MemoryDB API has |
| 37 | * no update for descriptions, so changing it replaces the parameter group. |
| 38 | */ |
| 39 | description?: string; |
| 40 | /** |
| 41 | * Engine parameter overrides (name → value), e.g. |
| 42 | * `{ "maxmemory-policy": "allkeys-lru" }`. Parameters removed from this map |
| 43 | * are reset to the engine default. |
| 44 | * @default {} (engine defaults) |
| 45 | */ |
| 46 | parameters?: Record<string, string>; |
| 47 | /** |
| 48 | * User-defined tags for the parameter group. |
| 49 | */ |
| 50 | tags?: Record<string, string>; |
| 51 | } |
| 52 | |
| 53 | export interface ParameterGroup extends Resource< |
| 54 | "AWS.MemoryDB.ParameterGroup", |
| 55 | ParameterGroupProps, |
| 56 | { |
| 57 | /** Name of the parameter group. */ |
| 58 | parameterGroupName: string; |
| 59 | /** ARN of the parameter group. */ |
| 60 | parameterGroupArn: string; |
| 61 | /** Parameter group family (e.g. `memorydb_valkey7`). */ |
| 62 | family: string | undefined; |
| 63 | /** Description of the parameter group. */ |
| 64 | description: string | undefined; |
| 65 | /** Tags on the parameter group (user + internal Alchemy tags). */ |
| 66 | tags: Record<string, string>; |
| 67 | }, |
| 68 | never, |
| 69 | Providers |
| 70 | > {} |
| 71 | |
| 72 | /** |
| 73 | * A MemoryDB parameter group — a named collection of engine parameter |
| 74 | * overrides applied to every node of any {@link Cluster} that references it |
| 75 | * via `parameterGroupName`. |
| 76 | * |
| 77 | * Parameter groups are free and provision instantly. Parameters not listed |
| 78 | * keep their engine defaults; removing a parameter from `parameters` resets |
| 79 | * it to the default. |
| 80 | * ### Creating a Parameter Group |
| 81 | * **Example:** Parameter Group with an Eviction Policy |
| 82 | * ```typescript |
| 83 | * const params = yield* ParameterGroup("CacheParams", { |
| 84 | * family: "memorydb_valkey7", |
| 85 | * description: "LRU eviction for the session cache", |
| 86 | * parameters: { "maxmemory-policy": "allkeys-lru" }, |
| 87 | * }); |
| 88 | * const cluster = yield* Cluster("Cache", { |
| 89 | * aclName: acl.aclName, |
| 90 | * parameterGroupName: params.parameterGroupName, |
| 91 | * }); |
| 92 | * ``` |
| 93 | * |
| 94 | * @resource |
| 95 | */ |
| 96 | export const ParameterGroup = Resource<ParameterGroup>("AWS.MemoryDB.ParameterGroup"); |
| 97 | |
| 98 | export const ParameterGroupProvider = () => |
| 99 | Provider.effect( |
| 100 | ParameterGroup, |
| 101 | Effect.gen(function* () { |
| 102 | const toName = (id: string, props: Partial<ParameterGroupProps>) => |
| 103 | props.parameterGroupName |
| 104 | ? Effect.succeed(props.parameterGroupName) |
| 105 | : createPhysicalName({ id, maxLength: 40, lowercase: true }); |
| 106 | |
| 107 | const readGroup = Effect.fn(function* (name: string) { |
| 108 | const response = yield* memorydb |
| 109 | .describeParameterGroups({ ParameterGroupName: name }) |
| 110 | .pipe(Effect.catchTag("ParameterGroupNotFoundFault", () => Effect.undefined)); |
| 111 | return response?.ParameterGroups?.[0]; |
| 112 | }); |
| 113 | |
| 114 | // Observed engine parameters (name → value), paginated. |
| 115 | const readParameters = Effect.fn(function* (name: string) { |
| 116 | const parameters = new Map<string, string>(); |
| 117 | yield* memorydb.describeParameters.pages({ ParameterGroupName: name }).pipe( |
| 118 | Stream.runForEach((page) => |
| 119 | Effect.sync(() => { |
| 120 | for (const parameter of page.Parameters ?? []) { |
| 121 | if (parameter.Name !== undefined) { |
| 122 | parameters.set(parameter.Name, parameter.Value ?? ""); |
| 123 | } |
| 124 | } |
| 125 | }), |
| 126 | ), |
| 127 | ); |
| 128 | return parameters; |
| 129 | }); |
| 130 | |
| 131 | const toAttrs = Effect.fn(function* (group: memorydb.ParameterGroup) { |
| 132 | if (!group.Name || !group.ARN) { |
| 133 | return yield* new MemoryDBParameterGroupUnexpectedResponse({ |
| 134 | message: `Parameter group '${group.Name}' is missing its ARN`, |
| 135 | }); |
| 136 | } |
| 137 | return { |
| 138 | parameterGroupName: group.Name, |
| 139 | parameterGroupArn: group.ARN, |
| 140 | family: group.Family, |
| 141 | description: group.Description, |
| 142 | tags: yield* readMemoryDbTags(group.ARN), |
| 143 | }; |
| 144 | }); |
| 145 | |
| 146 | return { |
| 147 | stables: ["parameterGroupName", "parameterGroupArn"], |
| 148 | |
| 149 | diff: Effect.fn(function* ({ id, olds, news }) { |
| 150 | if (!isResolved(news)) return undefined; |
| 151 | const n = news ?? { family: "" }; |
| 152 | const o = olds ?? { family: "" }; |
| 153 | if ((yield* toName(id, o)) !== (yield* toName(id, n))) { |
| 154 | return { action: "replace" } as const; |
| 155 | } |
| 156 | if (n.family !== o.family) { |
| 157 | return { action: "replace" } as const; |
| 158 | } |
| 159 | // Descriptions have no update API. |
| 160 | if ((n.description ?? undefined) !== (o.description ?? undefined)) { |
| 161 | return { action: "replace" } as const; |
| 162 | } |
| 163 | }), |
| 164 | |
| 165 | read: Effect.fn(function* ({ id, olds, output }) { |
| 166 | const name = output?.parameterGroupName ?? (yield* toName(id, olds ?? { family: "" })); |
| 167 | const group = yield* readGroup(name); |
| 168 | if (!group?.ARN) return undefined; |
| 169 | const attrs = yield* toAttrs(group); |
| 170 | return (yield* hasAlchemyTags(id, attrs.tags)) ? attrs : Unowned(attrs); |
| 171 | }), |
| 172 | |
| 173 | reconcile: Effect.fn(function* ({ id, news, olds, output, session }) { |
| 174 | const props = news!; |
| 175 | const name = output?.parameterGroupName ?? (yield* toName(id, props)); |
| 176 | const internalTags = yield* createInternalTags(id); |
| 177 | const desiredTags = { ...internalTags, ...props.tags }; |
| 178 | const desired = props.parameters ?? {}; |
| 179 | |
| 180 | // 1. Observe — cloud state is authoritative. |
| 181 | let observed = yield* readGroup(name); |
| 182 | |
| 183 | // 2. Ensure — create if missing; tolerate AlreadyExists as a race. |
| 184 | if (observed === undefined) { |
| 185 | yield* memorydb |
| 186 | .createParameterGroup({ |
| 187 | ParameterGroupName: name, |
| 188 | Family: props.family, |
| 189 | Description: props.description, |
| 190 | Tags: Object.entries(desiredTags).map(([Key, Value]) => ({ |
| 191 | Key, |
| 192 | Value, |
| 193 | })), |
| 194 | }) |
| 195 | .pipe(Effect.catchTag("ParameterGroupAlreadyExistsFault", () => Effect.void)); |
| 196 | observed = yield* readGroup(name); |
| 197 | } |
| 198 | if (observed === undefined) { |
| 199 | return yield* new MemoryDBParameterGroupUnexpectedResponse({ |
| 200 | message: `Parameter group '${name}' not found after create`, |
| 201 | }); |
| 202 | } |
| 203 | |
| 204 | // 3. Sync parameters — diff desired values against OBSERVED cloud |
| 205 | // values and apply only the delta. Parameters dropped from the |
| 206 | // desired map (present in olds) are reset to the engine default. |
| 207 | const observedParams = yield* readParameters(name); |
| 208 | const changed = Object.entries(desired).filter( |
| 209 | ([key, value]) => observedParams.get(key) !== value, |
| 210 | ); |
| 211 | if (changed.length > 0) { |
| 212 | yield* memorydb.updateParameterGroup({ |
| 213 | ParameterGroupName: name, |
| 214 | ParameterNameValues: changed.map(([ParameterName, ParameterValue]) => ({ |
| 215 | ParameterName, |
| 216 | ParameterValue, |
| 217 | })), |
| 218 | }); |
| 219 | } |
| 220 | const removed = Object.keys(olds?.parameters ?? {}).filter((key) => !(key in desired)); |
| 221 | if (removed.length > 0) { |
| 222 | yield* memorydb |
| 223 | .resetParameterGroup({ |
| 224 | ParameterGroupName: name, |
| 225 | ParameterNames: removed, |
| 226 | }) |
| 227 | .pipe( |
| 228 | // A parameter already at its default resets to a no-op; a |
| 229 | // group busy applying the previous update settles quickly. |
| 230 | Effect.retry({ |
| 231 | while: (e): boolean => e._tag === "InvalidParameterGroupStateFault", |
| 232 | schedule: Schedule.max([Schedule.fixed("5 seconds"), Schedule.recurs(8)]), |
| 233 | }), |
| 234 | ); |
| 235 | } |
| 236 | |
| 237 | // 3b. Sync tags — diff against OBSERVED cloud tags. |
| 238 | const arn = observed.ARN; |
| 239 | if (arn) { |
| 240 | const observedTags = yield* readMemoryDbTags(arn); |
| 241 | const { removed: removedTags, upsert } = diffTags(observedTags, desiredTags); |
| 242 | if (upsert.length > 0) { |
| 243 | yield* memorydb.tagResource({ ResourceArn: arn, Tags: upsert }); |
| 244 | } |
| 245 | if (removedTags.length > 0) { |
| 246 | yield* memorydb.untagResource({ |
| 247 | ResourceArn: arn, |
| 248 | TagKeys: removedTags, |
| 249 | }); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | yield* session.note(name); |
| 254 | return yield* toAttrs(observed); |
| 255 | }), |
| 256 | |
| 257 | delete: Effect.fn(function* ({ output }) { |
| 258 | // A parameter group still referenced by a cluster (or mid-update) |
| 259 | // rejects deletion with InvalidParameterGroupStateFault — retry |
| 260 | // bounded. NotFound is success (idempotent delete). |
| 261 | yield* memorydb |
| 262 | .deleteParameterGroup({ |
| 263 | ParameterGroupName: output.parameterGroupName, |
| 264 | }) |
| 265 | .pipe( |
| 266 | Effect.catchTag("ParameterGroupNotFoundFault", () => Effect.void), |
| 267 | Effect.retry({ |
| 268 | while: (e): boolean => e._tag === "InvalidParameterGroupStateFault", |
| 269 | schedule: Schedule.max([Schedule.fixed("5 seconds"), Schedule.recurs(12)]), |
| 270 | }), |
| 271 | ); |
| 272 | }), |
| 273 | |
| 274 | list: () => |
| 275 | memorydb.describeParameterGroups.pages({}).pipe( |
| 276 | Stream.runCollect, |
| 277 | Effect.map((chunk) => |
| 278 | Array.from(chunk).flatMap((page) => |
| 279 | (page.ParameterGroups ?? []).filter( |
| 280 | (group) => |
| 281 | group.Name !== undefined && |
| 282 | group.ARN !== undefined && |
| 283 | // The engine-default groups (default.memorydb-*) are |
| 284 | // AWS-owned and cannot be deleted. |
| 285 | !group.Name.startsWith("default."), |
| 286 | ), |
| 287 | ), |
| 288 | ), |
| 289 | Effect.flatMap(Effect.forEach((group) => toAttrs(group), { concurrency: 4 })), |
| 290 | ), |
| 291 | }; |
| 292 | }), |
| 293 | ); |