The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as apprunner from "@distilled.cloud/aws/apprunner"; |
| 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, 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 { isActiveStatus, readAppRunnerTags, syncAppRunnerTags, toWireTags } from "./internal.ts"; |
| 14 | |
| 15 | export class AutoScalingConfigurationUnexpectedResponse extends Data.TaggedError( |
| 16 | "AutoScalingConfigurationUnexpectedResponse", |
| 17 | )<{ |
| 18 | readonly message: string; |
| 19 | readonly cause?: unknown; |
| 20 | }> {} |
| 21 | |
| 22 | /** |
| 23 | * Derive the revision-less "name partial" ARN |
| 24 | * (`...:autoscalingconfiguration/{name}`) from any configuration ARN. |
| 25 | * `DeleteAllRevisions` rejects revision-qualified ARNs (observed live: |
| 26 | * "You cannot specify full auto scaling configuration ARN and |
| 27 | * DeleteAllRevisions as true at same time"). |
| 28 | */ |
| 29 | const toNamePartialArn = (arn: string): string => |
| 30 | // "arn:...:autoscalingconfiguration/{name}/{revision}/{uuid}" -> keep |
| 31 | // everything up to and including {name}. |
| 32 | arn.split("/").slice(0, 2).join("/"); |
| 33 | |
| 34 | export interface AutoScalingConfigurationProps { |
| 35 | /** |
| 36 | * Name of the auto scaling configuration. Must be 4-32 characters |
| 37 | * (letters, digits, hyphens, underscores). If omitted, a deterministic |
| 38 | * physical name is generated. Changing the name replaces the |
| 39 | * configuration (all revisions of the old name are deleted). |
| 40 | */ |
| 41 | autoScalingConfigurationName?: string; |
| 42 | /** |
| 43 | * Maximum number of concurrent requests an instance processes before |
| 44 | * App Runner scales up. |
| 45 | * @default 100 |
| 46 | */ |
| 47 | maxConcurrency?: number; |
| 48 | /** |
| 49 | * Minimum number of provisioned (warm) instances. Higher values spread |
| 50 | * the service over more Availability Zones at a higher minimal cost. |
| 51 | * @default 1 |
| 52 | */ |
| 53 | minSize?: number; |
| 54 | /** |
| 55 | * Maximum number of instances the service scales up to. |
| 56 | * @default 25 |
| 57 | */ |
| 58 | maxSize?: number; |
| 59 | /** |
| 60 | * User-defined tags for the configuration. |
| 61 | */ |
| 62 | tags?: Record<string, string>; |
| 63 | } |
| 64 | |
| 65 | export interface AutoScalingConfiguration extends Resource< |
| 66 | "AWS.AppRunner.AutoScalingConfiguration", |
| 67 | AutoScalingConfigurationProps, |
| 68 | { |
| 69 | /** |
| 70 | * Name of the auto scaling configuration. |
| 71 | */ |
| 72 | autoScalingConfigurationName: string; |
| 73 | /** |
| 74 | * ARN of this auto scaling configuration revision. |
| 75 | */ |
| 76 | autoScalingConfigurationArn: string; |
| 77 | /** |
| 78 | * Revision number of the configuration (revisions are immutable). |
| 79 | */ |
| 80 | autoScalingConfigurationRevision: number; |
| 81 | /** |
| 82 | * Maximum concurrent requests per instance before scaling out. |
| 83 | */ |
| 84 | maxConcurrency: number | undefined; |
| 85 | /** |
| 86 | * Minimum number of provisioned instances. |
| 87 | */ |
| 88 | minSize: number | undefined; |
| 89 | /** |
| 90 | * Maximum number of instances the service may scale out to. |
| 91 | */ |
| 92 | maxSize: number | undefined; |
| 93 | }, |
| 94 | never, |
| 95 | Providers |
| 96 | > {} |
| 97 | |
| 98 | /** |
| 99 | * An AWS App Runner auto scaling configuration. |
| 100 | * |
| 101 | * Auto scaling configurations are immutable revisions: changing |
| 102 | * `maxConcurrency`, `minSize`, or `maxSize` creates a new revision under |
| 103 | * the same name (the ARN and revision attributes change). A configuration |
| 104 | * can be shared across multiple App Runner services. |
| 105 | * ### Creating an Auto Scaling Configuration |
| 106 | * **Example:** Basic Configuration |
| 107 | * ```typescript |
| 108 | * const scaling = yield* AppRunner.AutoScalingConfiguration("Scaling", { |
| 109 | * maxConcurrency: 50, |
| 110 | * minSize: 1, |
| 111 | * maxSize: 3, |
| 112 | * }); |
| 113 | * ``` |
| 114 | * |
| 115 | * ### Using with an App Runner Service |
| 116 | * **Example:** Attach to a Service |
| 117 | * ```typescript |
| 118 | * const service = yield* AppRunner.Service("Api", { |
| 119 | * imageRepository: { |
| 120 | * imageIdentifier: "public.ecr.aws/aws-containers/hello-app-runner:latest", |
| 121 | * imageRepositoryType: "ECR_PUBLIC", |
| 122 | * port: "8000", |
| 123 | * }, |
| 124 | * autoScalingConfigurationArn: scaling.autoScalingConfigurationArn, |
| 125 | * }); |
| 126 | * ``` |
| 127 | * |
| 128 | * @resource |
| 129 | */ |
| 130 | export const AutoScalingConfiguration = Resource<AutoScalingConfiguration>( |
| 131 | "AWS.AppRunner.AutoScalingConfiguration", |
| 132 | ); |
| 133 | |
| 134 | export const AutoScalingConfigurationProvider = () => |
| 135 | Provider.effect( |
| 136 | AutoScalingConfiguration, |
| 137 | Effect.gen(function* () { |
| 138 | const toName = (id: string, props: AutoScalingConfigurationProps) => |
| 139 | props.autoScalingConfigurationName |
| 140 | ? Effect.succeed(props.autoScalingConfigurationName) |
| 141 | : createPhysicalName({ id, maxLength: 32 }); |
| 142 | |
| 143 | /** Find the latest ACTIVE revision of a configuration by name. */ |
| 144 | const findLatest = Effect.fn(function* (name: string) { |
| 145 | const page = yield* apprunner.listAutoScalingConfigurations({ |
| 146 | AutoScalingConfigurationName: name, |
| 147 | LatestOnly: true, |
| 148 | }); |
| 149 | const summary = (page.AutoScalingConfigurationSummaryList ?? []).find( |
| 150 | (s) => |
| 151 | s.AutoScalingConfigurationName === name && |
| 152 | isActiveStatus(s.Status) && |
| 153 | s.AutoScalingConfigurationArn !== undefined, |
| 154 | ); |
| 155 | if (!summary?.AutoScalingConfigurationArn) return undefined; |
| 156 | const described = yield* apprunner |
| 157 | .describeAutoScalingConfiguration({ |
| 158 | AutoScalingConfigurationArn: summary.AutoScalingConfigurationArn, |
| 159 | }) |
| 160 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined)); |
| 161 | const config = described?.AutoScalingConfiguration; |
| 162 | return config !== undefined && isActiveStatus(config.Status) ? config : undefined; |
| 163 | }); |
| 164 | |
| 165 | const toAttrs = Effect.fn(function* (config: apprunner.AutoScalingConfiguration) { |
| 166 | if ( |
| 167 | !config.AutoScalingConfigurationName || |
| 168 | !config.AutoScalingConfigurationArn || |
| 169 | config.AutoScalingConfigurationRevision === undefined |
| 170 | ) { |
| 171 | return yield* new AutoScalingConfigurationUnexpectedResponse({ |
| 172 | message: "App Runner auto scaling configuration is missing its name, ARN, or revision", |
| 173 | }); |
| 174 | } |
| 175 | return { |
| 176 | autoScalingConfigurationName: config.AutoScalingConfigurationName, |
| 177 | autoScalingConfigurationArn: config.AutoScalingConfigurationArn, |
| 178 | autoScalingConfigurationRevision: config.AutoScalingConfigurationRevision, |
| 179 | maxConcurrency: config.MaxConcurrency, |
| 180 | minSize: config.MinSize, |
| 181 | maxSize: config.MaxSize, |
| 182 | }; |
| 183 | }); |
| 184 | |
| 185 | return { |
| 186 | stables: ["autoScalingConfigurationName"], |
| 187 | |
| 188 | diff: Effect.fn(function* ({ id, olds, news }) { |
| 189 | if (!isResolved(news)) return undefined; |
| 190 | if ((yield* toName(id, olds ?? {})) !== (yield* toName(id, news ?? {}))) { |
| 191 | return { action: "replace" } as const; |
| 192 | } |
| 193 | }), |
| 194 | |
| 195 | read: Effect.fn(function* ({ id, olds, output }) { |
| 196 | const name = output?.autoScalingConfigurationName ?? (yield* toName(id, olds ?? {})); |
| 197 | const config = yield* findLatest(name); |
| 198 | if (config === undefined) return undefined; |
| 199 | const attrs = yield* toAttrs(config); |
| 200 | const tags = yield* readAppRunnerTags(attrs.autoScalingConfigurationArn); |
| 201 | return (yield* hasAlchemyTags(id, tags)) ? attrs : Unowned(attrs); |
| 202 | }), |
| 203 | |
| 204 | reconcile: Effect.fn(function* ({ id, news = {}, output, session }) { |
| 205 | const name = output?.autoScalingConfigurationName ?? (yield* toName(id, news)); |
| 206 | const internalTags = yield* createInternalTags(id); |
| 207 | const desiredTags = { ...internalTags, ...news.tags }; |
| 208 | |
| 209 | // 1. Observe — the latest ACTIVE revision is authoritative. |
| 210 | let observed = yield* findLatest(name); |
| 211 | |
| 212 | // 2/3. Ensure + sync — scaling settings live on immutable |
| 213 | // revisions, so a drift on any user-specified field is converged |
| 214 | // by creating a new revision under the same name. Only compare |
| 215 | // fields the user actually specified: the service fills in |
| 216 | // defaults that must not trigger spurious revisions. |
| 217 | const drifted = |
| 218 | observed !== undefined && |
| 219 | ((news.maxConcurrency !== undefined && |
| 220 | news.maxConcurrency !== observed.MaxConcurrency) || |
| 221 | (news.minSize !== undefined && news.minSize !== observed.MinSize) || |
| 222 | (news.maxSize !== undefined && news.maxSize !== observed.MaxSize)); |
| 223 | |
| 224 | if (observed === undefined || drifted) { |
| 225 | const created = yield* apprunner.createAutoScalingConfiguration({ |
| 226 | AutoScalingConfigurationName: name, |
| 227 | MaxConcurrency: news.maxConcurrency, |
| 228 | MinSize: news.minSize, |
| 229 | MaxSize: news.maxSize, |
| 230 | Tags: toWireTags(desiredTags), |
| 231 | }); |
| 232 | observed = created.AutoScalingConfiguration; |
| 233 | } |
| 234 | |
| 235 | // 3b. Sync tags on the latest revision ARN — diff against |
| 236 | // OBSERVED cloud tags. |
| 237 | if (observed.AutoScalingConfigurationArn) { |
| 238 | yield* syncAppRunnerTags(observed.AutoScalingConfigurationArn, desiredTags); |
| 239 | } |
| 240 | |
| 241 | // 4. Return fresh attributes. |
| 242 | yield* session.note(name); |
| 243 | return yield* toAttrs(observed); |
| 244 | }), |
| 245 | |
| 246 | delete: Effect.fn(function* ({ output }) { |
| 247 | // DeleteAllRevisions removes every revision that shares the |
| 248 | // configuration name; it requires the revision-less name partial |
| 249 | // ARN. A configuration still referenced by a service (App Runner |
| 250 | // releases the association asynchronously after the service |
| 251 | // finishes deleting) rejects with InvalidRequestException — |
| 252 | // retry through that window (bounded). |
| 253 | yield* apprunner |
| 254 | .deleteAutoScalingConfiguration({ |
| 255 | AutoScalingConfigurationArn: toNamePartialArn(output.autoScalingConfigurationArn), |
| 256 | DeleteAllRevisions: true, |
| 257 | }) |
| 258 | .pipe( |
| 259 | Effect.catchTag("ResourceNotFoundException", () => Effect.void), |
| 260 | Effect.retry({ |
| 261 | while: (e) => e._tag === "InvalidRequestException", |
| 262 | schedule: Schedule.max([Schedule.fixed("5 seconds"), Schedule.recurs(24)]), |
| 263 | }), |
| 264 | ); |
| 265 | }), |
| 266 | |
| 267 | list: () => |
| 268 | apprunner.listAutoScalingConfigurations.pages({ LatestOnly: true }).pipe( |
| 269 | Stream.runCollect, |
| 270 | Effect.map((chunk) => |
| 271 | Array.from(chunk).flatMap((page) => |
| 272 | (page.AutoScalingConfigurationSummaryList ?? []).filter( |
| 273 | ( |
| 274 | s, |
| 275 | ): s is apprunner.AutoScalingConfigurationSummary & { |
| 276 | AutoScalingConfigurationArn: string; |
| 277 | } => |
| 278 | isActiveStatus(s.Status) && |
| 279 | s.AutoScalingConfigurationArn !== undefined && |
| 280 | // App Runner ships an AWS-managed `DefaultConfiguration` |
| 281 | // revision that always exists and can never be deleted |
| 282 | // — keep it out of enumeration for account-wide |
| 283 | // teardown (nuke). |
| 284 | s.AutoScalingConfigurationName !== "DefaultConfiguration", |
| 285 | ), |
| 286 | ), |
| 287 | ), |
| 288 | Effect.flatMap( |
| 289 | Effect.forEach( |
| 290 | (summary) => |
| 291 | apprunner |
| 292 | .describeAutoScalingConfiguration({ |
| 293 | AutoScalingConfigurationArn: summary.AutoScalingConfigurationArn, |
| 294 | }) |
| 295 | .pipe( |
| 296 | Effect.flatMap((r) => toAttrs(r.AutoScalingConfiguration)), |
| 297 | // Tolerate a delete race — drop the item. |
| 298 | Effect.catchTag("ResourceNotFoundException", () => Effect.undefined), |
| 299 | ), |
| 300 | { concurrency: 4 }, |
| 301 | ), |
| 302 | ), |
| 303 | Effect.map((items) => items.filter((item) => item !== undefined)), |
| 304 | ), |
| 305 | }; |
| 306 | }), |
| 307 | ); |