@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 1,275 lines 47.9 KB
Raw
1 import * as iam from "@distilled.cloud/aws/iam";
2 import * as analytics from "@distilled.cloud/aws/kinesis-analytics-v2";
3 import { Unowned } from "alchemy/AdoptPolicy";
4 import { isResolved } from "alchemy/Diff";
5 import { createPhysicalName } from "alchemy/PhysicalName";
6 import * as Provider from "alchemy/Provider";
7 import { Resource } from "alchemy/Resource";
8 import {
9 createInternalTags,
10 createTagsList,
11 diffTags,
12 hasAlchemyTags,
13 type Tags,
14 } from "alchemy/Tags";
15 import { toWireMillis } from "alchemy/Util/Duration";
16 import * as Data from "effect/Data";
17 import type * as Duration from "effect/Duration";
18 import * as Effect from "effect/Effect";
19 import * as Schedule from "effect/Schedule";
20 import * as Stream from "effect/Stream";
21 import { AWSEnvironment } from "../Environment.ts";
22 import type { PolicyStatement } from "../IAM/Policy.ts";
23 import type { Providers } from "../Providers.ts";
24
25 /**
26 * Runtime environment of a Managed Service for Apache Flink application,
27 * e.g. `"FLINK-1_20"`.
28 */
29 export type RuntimeEnvironment = analytics.RuntimeEnvironment;
30
31 /**
32 * Lifecycle status of the application.
33 */
34 export type ApplicationStatus = analytics.ApplicationStatus;
35
36 /**
37 * Mode of the application. `STREAMING` runs the packaged Flink job;
38 * `INTERACTIVE` backs a Studio (Zeppelin) notebook.
39 */
40 export type ApplicationMode = "STREAMING" | "INTERACTIVE";
41
42 export interface ApplicationCodeProps {
43 /**
44 * ARN of the S3 bucket that contains the application code package
45 * (a zip/jar built for the configured Flink runtime).
46 */
47 bucketArn: string;
48 /**
49 * Object key of the application code package within the bucket.
50 */
51 fileKey: string;
52 /**
53 * Version of the S3 object containing the application code.
54 * @default the latest object version
55 */
56 objectVersion?: string;
57 }
58
59 export interface CheckpointConfigurationProps {
60 /**
61 * Whether to use the `DEFAULT` Flink checkpointing behavior or the
62 * `CUSTOM` values supplied in this configuration.
63 */
64 configurationType: "DEFAULT" | "CUSTOM";
65 /**
66 * Whether checkpointing is enabled. Only used with `CUSTOM`.
67 */
68 checkpointingEnabled?: boolean;
69 /**
70 * Interval between checkpoints, e.g. `"1 minute"` or
71 * `Duration.seconds(30)`. The API stores whole milliseconds. Only used
72 * with `CUSTOM`.
73 */
74 checkpointInterval?: Duration.Input;
75 /**
76 * Minimum pause between checkpoint operations, e.g. `"5 seconds"`. The
77 * API stores whole milliseconds. Only used with `CUSTOM`.
78 */
79 minPauseBetweenCheckpoints?: Duration.Input;
80 }
81
82 export interface MonitoringConfigurationProps {
83 /**
84 * Whether to use the `DEFAULT` monitoring behavior or the `CUSTOM`
85 * values supplied in this configuration.
86 */
87 configurationType: "DEFAULT" | "CUSTOM";
88 /**
89 * Granularity of the CloudWatch metrics emitted by the application.
90 * Only used with `CUSTOM`.
91 */
92 metricsLevel?: "APPLICATION" | "TASK" | "OPERATOR" | "PARALLELISM";
93 /**
94 * Log verbosity of the application. Only used with `CUSTOM`.
95 */
96 logLevel?: "INFO" | "WARN" | "ERROR" | "DEBUG";
97 }
98
99 export interface ParallelismConfigurationProps {
100 /**
101 * Whether to use the `DEFAULT` parallelism behavior or the `CUSTOM`
102 * values supplied in this configuration.
103 */
104 configurationType: "DEFAULT" | "CUSTOM";
105 /**
106 * Initial number of parallel tasks the application can perform.
107 * Only used with `CUSTOM`.
108 */
109 parallelism?: number;
110 /**
111 * Number of parallel tasks per Kinesis Processing Unit.
112 * Only used with `CUSTOM`.
113 */
114 parallelismPerKPU?: number;
115 /**
116 * Whether the service automatically scales the application's parallelism.
117 * Only used with `CUSTOM`.
118 */
119 autoScalingEnabled?: boolean;
120 }
121
122 export interface FlinkConfigurationProps {
123 /**
124 * Checkpointing (fault tolerance) settings for the Flink job.
125 */
126 checkpointConfiguration?: CheckpointConfigurationProps;
127 /**
128 * CloudWatch metrics/logging verbosity settings for the Flink job.
129 */
130 monitoringConfiguration?: MonitoringConfigurationProps;
131 /**
132 * Parallelism (KPU scaling) settings for the Flink job.
133 */
134 parallelismConfiguration?: ParallelismConfigurationProps;
135 }
136
137 export interface PropertyGroupProps {
138 /**
139 * Identifier of the property group, e.g. `"kinesis.analytics.flink.run.options"`
140 * or an application-defined group name.
141 */
142 propertyGroupId: string;
143 /**
144 * Key-value runtime properties exposed to the application through this group.
145 */
146 propertyMap: Record<string, string>;
147 }
148
149 export interface ApplicationVpcProps {
150 /**
151 * IDs of the subnets the application's ENIs are placed in.
152 */
153 subnetIds: string[];
154 /**
155 * IDs of the security groups attached to the application's ENIs.
156 */
157 securityGroupIds: string[];
158 }
159
160 export interface ApplicationProps {
161 /**
162 * Name of the application. Changing the name replaces the application.
163 * @default ${app}-${id}-${stage}-${instanceId}
164 */
165 applicationName?: string;
166 /**
167 * Description of the application. The API offers no way to change the
168 * description in place, so changing it replaces the application.
169 */
170 description?: string;
171 /**
172 * Runtime environment of the application, e.g. `"FLINK-1_20"`.
173 * Updated in place (Flink version upgrade).
174 */
175 runtimeEnvironment: RuntimeEnvironment;
176 /**
177 * Mode of the application. Changing the mode replaces the application.
178 * @default "STREAMING"
179 */
180 applicationMode?: ApplicationMode;
181 /**
182 * ARN of the IAM role the service assumes to read the code object and
183 * access sources/sinks.
184 * @default a role is auto-created granting read access to the code
185 * bucket, CloudWatch Logs delivery, and (when `vpc` is set) the EC2
186 * permissions required to manage the application's network interfaces.
187 */
188 serviceExecutionRole?: string;
189 /**
190 * Location of the application code package (zip/jar) in S3.
191 */
192 code: ApplicationCodeProps;
193 /**
194 * Runtime properties exposed to the application as property groups.
195 */
196 environmentProperties?: PropertyGroupProps[];
197 /**
198 * Flink-specific settings (checkpointing, monitoring, parallelism).
199 */
200 flinkConfiguration?: FlinkConfigurationProps;
201 /**
202 * Whether snapshots (savepoints) are enabled for the application.
203 * @default false
204 */
205 snapshotsEnabled?: boolean;
206 /**
207 * VPC configuration. When set the application's ENIs are placed in the
208 * given subnets so the Flink job can reach VPC-private resources.
209 */
210 vpc?: ApplicationVpcProps;
211 /**
212 * Whether the application should be running. When `true` the reconciler
213 * starts the application and waits for `RUNNING`; when `false` (default)
214 * a running application is force-stopped back to `READY`.
215 *
216 * Starting requires the code object to be a real Flink application jar —
217 * a placeholder zip passes creation but fails to start.
218 * @default false
219 */
220 start?: boolean;
221 /**
222 * Start of the 8-hour daily window in which the service may apply
223 * maintenance (patching) to the application, as `"HH:mm"` UTC — e.g.
224 * `"02:00"`. When omitted the service-assigned window is left unchanged.
225 */
226 maintenanceWindowStartTime?: string;
227 /**
228 * Tags to apply to the application.
229 */
230 tags?: Record<string, string>;
231 }
232
233 export interface Application extends Resource<
234 "AWS.KinesisAnalyticsV2.Application",
235 ApplicationProps,
236 {
237 /**
238 * Physical name of the application.
239 */
240 applicationName: string;
241 /**
242 * ARN of the application.
243 */
244 applicationArn: string;
245 /**
246 * Current lifecycle status of the application.
247 */
248 applicationStatus: ApplicationStatus;
249 /**
250 * Current version of the application's configuration. Incremented by
251 * every configuration update.
252 */
253 applicationVersionId: number;
254 /**
255 * Runtime environment of the application.
256 */
257 runtimeEnvironment: RuntimeEnvironment;
258 /**
259 * Mode of the application.
260 */
261 applicationMode: ApplicationMode | undefined;
262 /**
263 * ARN of the IAM role the service assumes.
264 */
265 serviceExecutionRole: string | undefined;
266 /**
267 * Name of the auto-created IAM role, when one was synthesized for this
268 * application. `undefined` when the caller supplied `serviceExecutionRole`.
269 */
270 roleName: string | undefined;
271 /**
272 * ID of the application's VPC configuration, when one is attached.
273 */
274 vpcConfigurationId: string | undefined;
275 /**
276 * ARN of the S3 bucket currently holding the application code.
277 */
278 codeBucketArn: string | undefined;
279 /**
280 * Object key of the application code currently configured.
281 */
282 codeFileKey: string | undefined;
283 /**
284 * Object version of the application code currently configured.
285 */
286 codeObjectVersion: string | undefined;
287 /**
288 * Start of the daily maintenance window currently configured, as
289 * `"HH:mm"` UTC.
290 */
291 maintenanceWindowStartTime: string | undefined;
292 /**
293 * End of the daily maintenance window currently configured, as
294 * `"HH:mm"` UTC (always 8 hours after the start).
295 */
296 maintenanceWindowEndTime: string | undefined;
297 /**
298 * Current tags reported for the application.
299 */
300 tags: Record<string, string>;
301 },
302 never,
303 Providers
304 > {}
305
306 /**
307 * A Managed Service for Apache Flink (Kinesis Data Analytics v2)
308 * application.
309 *
310 * `Application` owns the application definition — runtime environment, code
311 * location in S3, runtime properties, Flink settings, optional VPC
312 * connectivity and tags — and converges each aspect in place via
313 * `UpdateApplication`. Unless you supply `serviceExecutionRole`, an IAM role
314 * is auto-created granting the service read access to the code bucket and
315 * CloudWatch Logs delivery.
316 *
317 * The application is created in `READY` and does not run (or bill KPUs)
318 * until started. Set `start: true` to have the reconciler start the job and
319 * wait for `RUNNING` — this requires the code object to be a real Flink
320 * application jar.
321 * ### Creating Applications
322 * **Example:** Flink application from S3 code
323 * ```typescript
324 * import * as AWS from "@alchemy.run/aws";
325 *
326 * const bucket = yield* AWS.S3.Bucket("FlinkCode");
327 * const app = yield* AWS.KinesisAnalyticsV2.Application("Enrichment", {
328 * runtimeEnvironment: "FLINK-1_20",
329 * code: {
330 * bucketArn: bucket.bucketArn,
331 * fileKey: "jobs/enrichment-1.0.jar",
332 * },
333 * });
334 * ```
335 *
336 * **Example:** Runtime properties and parallelism
337 * ```typescript
338 * const app = yield* AWS.KinesisAnalyticsV2.Application("Enrichment", {
339 * runtimeEnvironment: "FLINK-1_20",
340 * code: { bucketArn: bucket.bucketArn, fileKey: "jobs/enrichment-1.0.jar" },
341 * environmentProperties: [
342 * {
343 * propertyGroupId: "EnrichmentProperties",
344 * propertyMap: { "input.stream": "clickstream", "region": "us-west-2" },
345 * },
346 * ],
347 * flinkConfiguration: {
348 * parallelismConfiguration: {
349 * configurationType: "CUSTOM",
350 * parallelism: 2,
351 * parallelismPerKPU: 1,
352 * autoScalingEnabled: false,
353 * },
354 * },
355 * snapshotsEnabled: true,
356 * });
357 * ```
358 *
359 * ### VPC Connectivity
360 * **Example:** Place the application in a VPC
361 * ```typescript
362 * const app = yield* AWS.KinesisAnalyticsV2.Application("Enrichment", {
363 * runtimeEnvironment: "FLINK-1_20",
364 * code: { bucketArn: bucket.bucketArn, fileKey: "jobs/enrichment-1.0.jar" },
365 * vpc: {
366 * subnetIds: [subnetA.subnetId, subnetB.subnetId],
367 * securityGroupIds: [sg.securityGroupId],
368 * },
369 * });
370 * ```
371 *
372 * ### Maintenance Window
373 * **Example:** Pin the daily maintenance window
374 * ```typescript
375 * const app = yield* AWS.KinesisAnalyticsV2.Application("Enrichment", {
376 * runtimeEnvironment: "FLINK-1_20",
377 * code: { bucketArn: bucket.bucketArn, fileKey: "jobs/enrichment-1.0.jar" },
378 * maintenanceWindowStartTime: "02:00",
379 * });
380 * ```
381 *
382 * ### Running the Application
383 * **Example:** Start the Flink job and keep it running
384 * ```typescript
385 * const app = yield* AWS.KinesisAnalyticsV2.Application("Enrichment", {
386 * runtimeEnvironment: "FLINK-1_20",
387 * code: { bucketArn: bucket.bucketArn, fileKey: "jobs/enrichment-1.0.jar" },
388 * start: true,
389 * });
390 * ```
391 *
392 * @resource
393 */
394 export const Application = Resource<Application>("AWS.KinesisAnalyticsV2.Application");
395
396 /**
397 * Validation error raised before any AWS call when the props are invalid.
398 */
399 export class ApplicationValidationError extends Data.TaggedError("ApplicationValidationError")<{
400 readonly message: string;
401 }> {}
402
403 /**
404 * The application did not settle into a stable status (READY / RUNNING /
405 * ROLLED_BACK) within the bounded wait.
406 */
407 export class ApplicationNotStable extends Data.TaggedError("ApplicationNotStable")<{
408 readonly applicationName: string;
409 readonly status: string;
410 }> {}
411
412 /**
413 * `start: true` was requested but the application failed to reach `RUNNING`
414 * — either the start rolled back (bad jar, bad configuration) or the
415 * bounded wait elapsed.
416 */
417 export class ApplicationStartFailed extends Data.TaggedError("ApplicationStartFailed")<{
418 readonly applicationName: string;
419 readonly status: string;
420 }> {}
421
422 class ApplicationStatusPending extends Data.TaggedError("ApplicationStatusPending")<{
423 readonly status: string;
424 }> {}
425
426 class ApplicationStillExists extends Data.TaggedError("ApplicationStillExists") {}
427
428 const createApplicationName = (id: string, props: { applicationName?: string | undefined }) =>
429 Effect.gen(function* () {
430 if (props.applicationName) {
431 return props.applicationName;
432 }
433 return yield* createPhysicalName({ id, maxLength: 128 });
434 });
435
436 /**
437 * Data-FIRST `Effect.retry(self, options)` wrapped with an explicit return
438 * annotation — inlining `Effect.retry` with an options object in provider
439 * lifecycle code leaves `Retry.Return`'s conditional type unresolved in the
440 * provider's inferred layer type, which TypeScript's declaration emit widens
441 * to an `unknown` R — poisoning `AWS.providers()` for every downstream
442 * consumer (see `retryThroughDeletionWindow` in SecretsManager/Secret.ts).
443 *
444 * Retries `InvalidArgumentException`s that mention the IAM role — a freshly
445 * created role takes a few seconds to become assumable by the
446 * kinesisanalytics service principal, and `CreateApplication` surfaces that
447 * propagation window as a role-related `InvalidArgumentException`.
448 */
449 const retryThroughRolePropagation = <A, E extends { _tag: string }, R>(
450 self: Effect.Effect<A, E, R>,
451 ): Effect.Effect<A, E, R> =>
452 Effect.retry(self, {
453 while: (e) =>
454 e._tag === "InvalidArgumentException" &&
455 /role|assume|trust|principal/i.test((e as { message?: string }).message ?? ""),
456 schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(15)]),
457 });
458
459 /**
460 * Retries `ResourceInUseException` / `ConcurrentModificationException`
461 * (application transitioning between statuses, or a concurrent operation in
462 * flight) on a bounded schedule. Explicitly annotated for the same
463 * declaration-emit reason as {@link retryThroughRolePropagation}.
464 */
465 const retryWhileInUse = <A, E extends { _tag: string }, R>(
466 self: Effect.Effect<A, E, R>,
467 ): Effect.Effect<A, E, R> =>
468 Effect.retry(self, {
469 while: (e) =>
470 e._tag === "ResourceInUseException" || e._tag === "ConcurrentModificationException",
471 schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(30)]),
472 });
473
474 const toTagRecord = (tags: ReadonlyArray<{ Key: string; Value?: string }> | undefined) =>
475 Object.fromEntries(
476 (tags ?? [])
477 .filter((tag): tag is { Key: string; Value: string } => typeof tag.Value === "string")
478 .map((tag) => [tag.Key, tag.Value]),
479 );
480
481 const toAttrs = ({
482 detail,
483 tags,
484 roleName,
485 }: {
486 detail: analytics.ApplicationDetail;
487 tags: Record<string, string>;
488 roleName: string | undefined;
489 }): Application["Attributes"] => {
490 const config = detail.ApplicationConfigurationDescription;
491 const code =
492 config?.ApplicationCodeConfigurationDescription?.CodeContentDescription
493 ?.S3ApplicationCodeLocationDescription;
494 return {
495 applicationName: detail.ApplicationName,
496 applicationArn: detail.ApplicationARN,
497 applicationStatus: detail.ApplicationStatus,
498 applicationVersionId: detail.ApplicationVersionId,
499 runtimeEnvironment: detail.RuntimeEnvironment,
500 applicationMode: detail.ApplicationMode as ApplicationMode | undefined,
501 serviceExecutionRole: detail.ServiceExecutionRole,
502 roleName,
503 vpcConfigurationId: config?.VpcConfigurationDescriptions?.[0]?.VpcConfigurationId,
504 codeBucketArn: code?.BucketARN,
505 codeFileKey: code?.FileKey,
506 codeObjectVersion: code?.ObjectVersion,
507 maintenanceWindowStartTime:
508 detail.ApplicationMaintenanceConfigurationDescription?.ApplicationMaintenanceWindowStartTime,
509 maintenanceWindowEndTime:
510 detail.ApplicationMaintenanceConfigurationDescription?.ApplicationMaintenanceWindowEndTime,
511 tags,
512 };
513 };
514
515 const describeApplicationDetail = Effect.fn(function* (applicationName: string) {
516 const response = yield* analytics
517 .describeApplication({ ApplicationName: applicationName })
518 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined));
519 return response?.ApplicationDetail;
520 });
521
522 const readApplication = Effect.fn(function* ({
523 applicationName,
524 roleName,
525 }: {
526 applicationName: string;
527 roleName: string | undefined;
528 }) {
529 const detail = yield* describeApplicationDetail(applicationName);
530 if (!detail) {
531 return undefined;
532 }
533 // The application can vanish between the describe above and the tag read
534 // (e.g. a concurrent destroy) — a typed `ResourceNotFoundException` here
535 // just means it's gone.
536 const tagsResponse = yield* analytics
537 .listTagsForResource({ ResourceARN: detail.ApplicationARN })
538 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined));
539 if (!tagsResponse) {
540 return undefined;
541 }
542 return toAttrs({
543 detail,
544 tags: toTagRecord(tagsResponse.Tags),
545 roleName,
546 });
547 });
548
549 // Configuration updates settle fast (the app stays READY); 2s × 45 bounds
550 // the wait at 90s. RUNNING is also stable — a started app being updated
551 // passes through UPDATING and returns to RUNNING.
552 const waitForApplicationStable = (applicationName: string) =>
553 Effect.gen(function* () {
554 const detail = yield* describeApplicationDetail(applicationName);
555 if (!detail) {
556 return yield* new ApplicationNotStable({ applicationName, status: "MISSING" });
557 }
558 if (
559 detail.ApplicationStatus !== "READY" &&
560 detail.ApplicationStatus !== "RUNNING" &&
561 detail.ApplicationStatus !== "ROLLED_BACK"
562 ) {
563 return yield* new ApplicationStatusPending({ status: detail.ApplicationStatus });
564 }
565 return detail;
566 }).pipe(
567 Effect.retry({
568 while: (e: { _tag: string }) => e._tag === "ApplicationStatusPending",
569 schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(45)]),
570 }),
571 Effect.catchTag("ApplicationStatusPending", (e) =>
572 Effect.fail(new ApplicationNotStable({ applicationName, status: e.status })),
573 ),
574 );
575
576 // A Flink job start submits the jar to a fresh cluster — several minutes is
577 // normal. 10s × 57 bounds the wait at 570s. A start that rolls back
578 // (ROLLED_BACK / back to READY after STARTING) fails fast with a typed error.
579 const waitForApplicationRunning = (applicationName: string) =>
580 Effect.gen(function* () {
581 const detail = yield* describeApplicationDetail(applicationName);
582 const status = detail?.ApplicationStatus ?? "MISSING";
583 if (status === "RUNNING") {
584 return detail!;
585 }
586 if (status === "STARTING" || status === "UPDATING") {
587 return yield* new ApplicationStatusPending({ status });
588 }
589 return yield* new ApplicationStartFailed({ applicationName, status });
590 }).pipe(
591 Effect.retry({
592 while: (e: { _tag: string }) => e._tag === "ApplicationStatusPending",
593 schedule: Schedule.max([Schedule.fixed("10 seconds"), Schedule.recurs(57)]),
594 }),
595 Effect.catchTag("ApplicationStatusPending", (e) =>
596 Effect.fail(new ApplicationStartFailed({ applicationName, status: e.status })),
597 ),
598 );
599
600 // Force-stop transitions through FORCE_STOPPING/STOPPING back to READY;
601 // 10s × 30 bounds the wait at 300s.
602 const waitForApplicationStopped = (applicationName: string) =>
603 Effect.gen(function* () {
604 const detail = yield* describeApplicationDetail(applicationName);
605 const status = detail?.ApplicationStatus ?? "MISSING";
606 if (status === "READY" || status === "ROLLED_BACK" || status === "MISSING") {
607 return;
608 }
609 return yield* new ApplicationStatusPending({ status });
610 }).pipe(
611 Effect.retry({
612 while: (e: { _tag: string }) => e._tag === "ApplicationStatusPending",
613 schedule: Schedule.max([Schedule.fixed("10 seconds"), Schedule.recurs(30)]),
614 }),
615 Effect.catchTag("ApplicationStatusPending", (e) =>
616 Effect.fail(new ApplicationNotStable({ applicationName, status: e.status })),
617 ),
618 );
619
620 // Deletion is quick for READY apps but a previously-running app halts its
621 // job first; 3s × 50 bounds the wait at 150s.
622 const waitForApplicationDeleted = (applicationName: string) =>
623 Effect.gen(function* () {
624 const detail = yield* describeApplicationDetail(applicationName);
625 if (detail !== undefined) {
626 return yield* new ApplicationStillExists();
627 }
628 }).pipe(
629 Effect.retry({
630 while: (e: { _tag: string }) => e._tag === "ApplicationStillExists",
631 schedule: Schedule.max([Schedule.fixed("3 seconds"), Schedule.recurs(50)]),
632 }),
633 );
634
635 const deleteRoleIfExists = Effect.fn(function* (roleName: string) {
636 yield* iam
637 .deleteRolePolicy({ RoleName: roleName, PolicyName: roleName })
638 .pipe(Effect.catchTag("NoSuchEntityException", () => Effect.void));
639 yield* iam
640 .deleteRole({ RoleName: roleName })
641 .pipe(Effect.catchTag("NoSuchEntityException", () => Effect.void));
642 });
643
644 const toWirePropertyGroups = (
645 groups: ReadonlyArray<PropertyGroupProps>,
646 ): analytics.PropertyGroup[] =>
647 groups.map((group) => ({
648 PropertyGroupId: group.propertyGroupId,
649 PropertyMap: group.propertyMap,
650 }));
651
652 // Canonical string form for observed-vs-desired comparison — order of
653 // groups and of keys within a group is not significant.
654 const canonicalPropertyGroups = (groups: ReadonlyArray<analytics.PropertyGroup> | undefined) =>
655 JSON.stringify(
656 [...(groups ?? [])]
657 .map((group) => ({
658 id: group.PropertyGroupId,
659 map: Object.fromEntries(
660 Object.entries(group.PropertyMap ?? {})
661 .filter(([, value]) => value !== undefined)
662 .sort(([a], [b]) => a.localeCompare(b)),
663 ),
664 }))
665 .sort((a, b) => a.id.localeCompare(b.id)),
666 );
667
668 const sameStringSet = (a: ReadonlyArray<string>, b: ReadonlyArray<string>) =>
669 a.length === b.length && [...a].sort().join(",") === [...b].sort().join(",");
670
671 const checkpointDiffers = (
672 desired: CheckpointConfigurationProps,
673 observed: analytics.CheckpointConfigurationDescription | undefined,
674 ) =>
675 desired.configurationType !== observed?.ConfigurationType ||
676 (desired.checkpointingEnabled !== undefined &&
677 desired.checkpointingEnabled !== observed?.CheckpointingEnabled) ||
678 (desired.checkpointInterval !== undefined &&
679 toWireMillis(desired.checkpointInterval) !== observed?.CheckpointInterval) ||
680 (desired.minPauseBetweenCheckpoints !== undefined &&
681 toWireMillis(desired.minPauseBetweenCheckpoints) !== observed?.MinPauseBetweenCheckpoints);
682
683 const monitoringDiffers = (
684 desired: MonitoringConfigurationProps,
685 observed: analytics.MonitoringConfigurationDescription | undefined,
686 ) =>
687 desired.configurationType !== observed?.ConfigurationType ||
688 (desired.metricsLevel !== undefined && desired.metricsLevel !== observed?.MetricsLevel) ||
689 (desired.logLevel !== undefined && desired.logLevel !== observed?.LogLevel);
690
691 const parallelismDiffers = (
692 desired: ParallelismConfigurationProps,
693 observed: analytics.ParallelismConfigurationDescription | undefined,
694 ) =>
695 desired.configurationType !== observed?.ConfigurationType ||
696 (desired.parallelism !== undefined && desired.parallelism !== observed?.Parallelism) ||
697 (desired.parallelismPerKPU !== undefined &&
698 desired.parallelismPerKPU !== observed?.ParallelismPerKPU) ||
699 (desired.autoScalingEnabled !== undefined &&
700 desired.autoScalingEnabled !== observed?.AutoScalingEnabled);
701
702 const toFlinkConfiguration = (
703 flink: FlinkConfigurationProps | undefined,
704 ): analytics.FlinkApplicationConfiguration | undefined => {
705 if (!flink) return undefined;
706 const configuration: analytics.FlinkApplicationConfiguration = {};
707 if (flink.checkpointConfiguration) {
708 configuration.CheckpointConfiguration = {
709 ConfigurationType: flink.checkpointConfiguration.configurationType,
710 CheckpointingEnabled: flink.checkpointConfiguration.checkpointingEnabled,
711 CheckpointInterval: toWireMillis(flink.checkpointConfiguration.checkpointInterval),
712 MinPauseBetweenCheckpoints: toWireMillis(
713 flink.checkpointConfiguration.minPauseBetweenCheckpoints,
714 ),
715 };
716 }
717 if (flink.monitoringConfiguration) {
718 configuration.MonitoringConfiguration = {
719 ConfigurationType: flink.monitoringConfiguration.configurationType,
720 MetricsLevel: flink.monitoringConfiguration.metricsLevel,
721 LogLevel: flink.monitoringConfiguration.logLevel,
722 };
723 }
724 if (flink.parallelismConfiguration) {
725 configuration.ParallelismConfiguration = {
726 ConfigurationType: flink.parallelismConfiguration.configurationType,
727 Parallelism: flink.parallelismConfiguration.parallelism,
728 ParallelismPerKPU: flink.parallelismConfiguration.parallelismPerKPU,
729 AutoScalingEnabled: flink.parallelismConfiguration.autoScalingEnabled,
730 };
731 }
732 return Object.keys(configuration).length > 0 ? configuration : undefined;
733 };
734
735 const toFlinkConfigurationUpdate = (
736 flink: FlinkConfigurationProps,
737 observed: analytics.FlinkApplicationConfigurationDescription | undefined,
738 ): analytics.FlinkApplicationConfigurationUpdate | undefined => {
739 const update: analytics.FlinkApplicationConfigurationUpdate = {};
740 if (
741 flink.checkpointConfiguration &&
742 checkpointDiffers(flink.checkpointConfiguration, observed?.CheckpointConfigurationDescription)
743 ) {
744 update.CheckpointConfigurationUpdate = {
745 ConfigurationTypeUpdate: flink.checkpointConfiguration.configurationType,
746 CheckpointingEnabledUpdate: flink.checkpointConfiguration.checkpointingEnabled,
747 CheckpointIntervalUpdate: toWireMillis(flink.checkpointConfiguration.checkpointInterval),
748 MinPauseBetweenCheckpointsUpdate: toWireMillis(
749 flink.checkpointConfiguration.minPauseBetweenCheckpoints,
750 ),
751 };
752 }
753 if (
754 flink.monitoringConfiguration &&
755 monitoringDiffers(flink.monitoringConfiguration, observed?.MonitoringConfigurationDescription)
756 ) {
757 update.MonitoringConfigurationUpdate = {
758 ConfigurationTypeUpdate: flink.monitoringConfiguration.configurationType,
759 MetricsLevelUpdate: flink.monitoringConfiguration.metricsLevel,
760 LogLevelUpdate: flink.monitoringConfiguration.logLevel,
761 };
762 }
763 if (
764 flink.parallelismConfiguration &&
765 parallelismDiffers(
766 flink.parallelismConfiguration,
767 observed?.ParallelismConfigurationDescription,
768 )
769 ) {
770 update.ParallelismConfigurationUpdate = {
771 ConfigurationTypeUpdate: flink.parallelismConfiguration.configurationType,
772 ParallelismUpdate: flink.parallelismConfiguration.parallelism,
773 ParallelismPerKPUUpdate: flink.parallelismConfiguration.parallelismPerKPU,
774 AutoScalingEnabledUpdate: flink.parallelismConfiguration.autoScalingEnabled,
775 };
776 }
777 return Object.keys(update).length > 0 ? update : undefined;
778 };
779
780 export const ApplicationProvider = () =>
781 Provider.effect(
782 Application,
783 Effect.gen(function* () {
784 const createRoleName = (id: string) => createPhysicalName({ id, maxLength: 64 });
785
786 // Ensure the synthesized IAM role exists and its inline policy matches
787 // the desired access. `createRole` tolerates the already-exists race;
788 // `putRolePolicy` is an idempotent upsert, so the policy re-converges
789 // on every reconcile (e.g. after the code bucket changes or a VPC
790 // configuration is added).
791 const ensureRole = Effect.fn(function* ({
792 id,
793 roleName,
794 codeBucketArn,
795 vpc,
796 }: {
797 id: string;
798 roleName: string;
799 codeBucketArn: string;
800 vpc: ApplicationVpcProps | undefined;
801 }) {
802 const tags = yield* createInternalTags(id);
803 yield* iam
804 .createRole({
805 RoleName: roleName,
806 AssumeRolePolicyDocument: JSON.stringify({
807 Version: "2012-10-17",
808 Statement: [
809 {
810 Effect: "Allow",
811 Principal: { Service: "kinesisanalytics.amazonaws.com" },
812 Action: "sts:AssumeRole",
813 },
814 ],
815 }),
816 Tags: createTagsList(tags),
817 })
818 .pipe(
819 Effect.catchTag("EntityAlreadyExistsException", () =>
820 iam.getRole({ RoleName: roleName }),
821 ),
822 );
823
824 const statements: PolicyStatement[] = [
825 {
826 Effect: "Allow",
827 Action: ["s3:GetObject", "s3:GetObjectVersion"],
828 Resource: [`${codeBucketArn}/*`],
829 },
830 {
831 Effect: "Allow",
832 Action: ["s3:GetBucketLocation", "s3:ListBucket"],
833 Resource: [codeBucketArn],
834 },
835 {
836 // Log stream ARNs are attached after the application exists (via
837 // ApplicationCloudWatchLoggingOption), so log delivery is granted
838 // broadly rather than per-stream.
839 Effect: "Allow",
840 Action: ["logs:DescribeLogGroups", "logs:DescribeLogStreams", "logs:PutLogEvents"],
841 Resource: ["*"],
842 },
843 ];
844 if (vpc) {
845 statements.push({
846 // ENI management for VPC-attached applications has no
847 // resource-level IAM support.
848 Effect: "Allow",
849 Action: [
850 "ec2:CreateNetworkInterface",
851 "ec2:CreateNetworkInterfacePermission",
852 "ec2:DeleteNetworkInterface",
853 "ec2:DescribeNetworkInterfaces",
854 "ec2:DescribeVpcs",
855 "ec2:DescribeSubnets",
856 "ec2:DescribeSecurityGroups",
857 "ec2:DescribeDhcpOptions",
858 ],
859 Resource: ["*"],
860 });
861 }
862
863 yield* iam.putRolePolicy({
864 RoleName: roleName,
865 PolicyName: roleName,
866 PolicyDocument: JSON.stringify({
867 Version: "2012-10-17",
868 Statement: statements,
869 }),
870 });
871 });
872
873 return Application.Provider.of({
874 stables: ["applicationName", "applicationArn", "applicationMode", "roleName"],
875
876 // Enumerate every application in the ambient account/region and
877 // hydrate each summary into the `read` shape with bounded
878 // concurrency, dropping applications that vanish mid-flight.
879 list: () =>
880 Effect.gen(function* () {
881 const summaries = Array.from(
882 yield* analytics.listApplications.items({}).pipe(Stream.runCollect),
883 );
884 const hydrated = yield* Effect.forEach(
885 summaries,
886 (summary) =>
887 readApplication({
888 applicationName: summary.ApplicationName,
889 roleName: undefined,
890 }),
891 { concurrency: 10 },
892 );
893 return hydrated.filter(
894 (attrs): attrs is Application["Attributes"] => attrs !== undefined,
895 );
896 }),
897
898 read: Effect.fn(function* ({ id, olds, output }) {
899 const applicationName =
900 output?.applicationName ?? (yield* createApplicationName(id, olds ?? {}));
901 const state = yield* readApplication({
902 applicationName,
903 roleName: output?.roleName,
904 });
905 if (!state) return undefined;
906 return (yield* hasAlchemyTags(id, state.tags as Tags)) ? state : Unowned(state);
907 }),
908
909 diff: Effect.fn(function* ({ id, news, olds }) {
910 if (!isResolved(news)) return undefined;
911 const oldName = yield* createApplicationName(id, olds ?? {});
912 const newName = yield* createApplicationName(id, news ?? {});
913 if (oldName !== newName) {
914 return { action: "replace" } as const;
915 }
916 // The mode is immutable.
917 if ((olds?.applicationMode ?? "STREAMING") !== (news?.applicationMode ?? "STREAMING")) {
918 return { action: "replace" } as const;
919 }
920 // The API offers no UpdateApplication field for the description.
921 if ((olds?.description ?? "") !== (news?.description ?? "")) {
922 return { action: "replace" } as const;
923 }
924 }),
925
926 reconcile: Effect.fn(function* ({ id, news, output, session }) {
927 const { accountId } = yield* AWSEnvironment.current;
928
929 if (!news?.code?.bucketArn || !news.code.fileKey) {
930 return yield* new ApplicationValidationError({
931 message: `Application "${id}" requires code.bucketArn and code.fileKey`,
932 });
933 }
934
935 const applicationName =
936 output?.applicationName ?? (yield* createApplicationName(id, news));
937 const internalTags = yield* createInternalTags(id);
938 const desiredTags = { ...news.tags, ...internalTags };
939
940 // Synthesize the IAM role when the caller didn't supply one.
941 const roleName = news.serviceExecutionRole
942 ? undefined
943 : (output?.roleName ?? (yield* createRoleName(id)));
944 const serviceExecutionRole =
945 news.serviceExecutionRole ?? `arn:aws:iam::${accountId}:role/${roleName}`;
946 if (roleName) {
947 yield* ensureRole({
948 id,
949 roleName,
950 codeBucketArn: news.code.bucketArn,
951 vpc: news.vpc,
952 });
953 }
954
955 // 1. OBSERVE — cloud state is authoritative; `output` is only a
956 // cache for the physical name.
957 const observed = yield* describeApplicationDetail(applicationName);
958
959 // 2. ENSURE — create the application if it's missing. Tolerate the
960 // already-exists race (`ResourceInUseException`), retry through the
961 // fresh role's IAM propagation window, and retry
962 // `ConcurrentModificationException` — recreating an application
963 // under a recently-deleted name fails with "Tags are already
964 // registered for this resource ARN ... please retry later".
965 if (observed === undefined) {
966 const createRequest: analytics.CreateApplicationRequest = {
967 ApplicationName: applicationName,
968 ApplicationDescription: news.description,
969 RuntimeEnvironment: news.runtimeEnvironment,
970 ApplicationMode: news.applicationMode,
971 ServiceExecutionRole: serviceExecutionRole,
972 ApplicationConfiguration: {
973 ApplicationCodeConfiguration: {
974 CodeContentType: "ZIPFILE",
975 CodeContent: {
976 S3ContentLocation: {
977 BucketARN: news.code.bucketArn,
978 FileKey: news.code.fileKey,
979 ObjectVersion: news.code.objectVersion,
980 },
981 },
982 },
983 EnvironmentProperties: news.environmentProperties
984 ? {
985 PropertyGroups: toWirePropertyGroups(news.environmentProperties),
986 }
987 : undefined,
988 FlinkApplicationConfiguration: toFlinkConfiguration(news.flinkConfiguration),
989 ApplicationSnapshotConfiguration:
990 news.snapshotsEnabled !== undefined
991 ? { SnapshotsEnabled: news.snapshotsEnabled }
992 : undefined,
993 VpcConfigurations: news.vpc
994 ? [
995 {
996 SubnetIds: news.vpc.subnetIds,
997 SecurityGroupIds: news.vpc.securityGroupIds,
998 },
999 ]
1000 : undefined,
1001 },
1002 Tags: createTagsList(desiredTags),
1003 };
1004 const create = (request: analytics.CreateApplicationRequest) =>
1005 analytics.createApplication(request).pipe(
1006 Effect.catchTag("ResourceInUseException", () => Effect.void),
1007 retryThroughRolePropagation,
1008 retryWhileInUse,
1009 );
1010 yield* create(createRequest).pipe(
1011 // The lingering tag registration on a recently-deleted name can
1012 // outlive the bounded retry window. AWS's own guidance in the
1013 // error text: create without tags, then TagResource — the tag
1014 // sync below applies the desired tags right after.
1015 Effect.catchTag("ConcurrentModificationException", () =>
1016 create({ ...createRequest, Tags: undefined }),
1017 ),
1018 );
1019 yield* session.note(`Creating application ${applicationName}...`);
1020 }
1021
1022 // Both a fresh create and a crashed prior run land here — wait for
1023 // a stable status before syncing (bounded).
1024 yield* waitForApplicationStable(applicationName);
1025
1026 // 3. SYNC — read OBSERVED state fresh on every attempt (the
1027 // version id is a compare-and-set token), compute the delta from
1028 // `news`, and apply a single UpdateApplication call. Retried
1029 // through ConcurrentModification/ResourceInUse with a re-read.
1030 const syncConfiguration = Effect.gen(function* () {
1031 const detail = yield* describeApplicationDetail(applicationName);
1032 if (!detail) return;
1033 const config = detail.ApplicationConfigurationDescription;
1034
1035 const configurationUpdate: analytics.ApplicationConfigurationUpdate = {};
1036
1037 const observedCode =
1038 config?.ApplicationCodeConfigurationDescription?.CodeContentDescription
1039 ?.S3ApplicationCodeLocationDescription;
1040 if (
1041 observedCode?.BucketARN !== news.code.bucketArn ||
1042 observedCode?.FileKey !== news.code.fileKey ||
1043 (news.code.objectVersion !== undefined &&
1044 observedCode?.ObjectVersion !== news.code.objectVersion)
1045 ) {
1046 configurationUpdate.ApplicationCodeConfigurationUpdate = {
1047 CodeContentTypeUpdate: "ZIPFILE",
1048 CodeContentUpdate: {
1049 S3ContentLocationUpdate: {
1050 BucketARNUpdate: news.code.bucketArn,
1051 FileKeyUpdate: news.code.fileKey,
1052 ObjectVersionUpdate: news.code.objectVersion,
1053 },
1054 },
1055 };
1056 }
1057
1058 if (
1059 news.environmentProperties !== undefined &&
1060 canonicalPropertyGroups(toWirePropertyGroups(news.environmentProperties)) !==
1061 canonicalPropertyGroups(
1062 config?.EnvironmentPropertyDescriptions?.PropertyGroupDescriptions,
1063 )
1064 ) {
1065 configurationUpdate.EnvironmentPropertyUpdates = {
1066 PropertyGroups: toWirePropertyGroups(news.environmentProperties),
1067 };
1068 }
1069
1070 if (news.flinkConfiguration) {
1071 const flinkUpdate = toFlinkConfigurationUpdate(
1072 news.flinkConfiguration,
1073 config?.FlinkApplicationConfigurationDescription,
1074 );
1075 if (flinkUpdate) {
1076 configurationUpdate.FlinkApplicationConfigurationUpdate = flinkUpdate;
1077 }
1078 }
1079
1080 if (
1081 news.snapshotsEnabled !== undefined &&
1082 news.snapshotsEnabled !==
1083 config?.ApplicationSnapshotConfigurationDescription?.SnapshotsEnabled
1084 ) {
1085 configurationUpdate.ApplicationSnapshotConfigurationUpdate = {
1086 SnapshotsEnabledUpdate: news.snapshotsEnabled,
1087 };
1088 }
1089
1090 const observedVpc = config?.VpcConfigurationDescriptions?.[0];
1091 if (
1092 news.vpc &&
1093 observedVpc &&
1094 (!sameStringSet(news.vpc.subnetIds, observedVpc.SubnetIds) ||
1095 !sameStringSet(news.vpc.securityGroupIds, observedVpc.SecurityGroupIds))
1096 ) {
1097 configurationUpdate.VpcConfigurationUpdates = [
1098 {
1099 VpcConfigurationId: observedVpc.VpcConfigurationId,
1100 SubnetIdUpdates: news.vpc.subnetIds,
1101 SecurityGroupIdUpdates: news.vpc.securityGroupIds,
1102 },
1103 ];
1104 }
1105
1106 const update: analytics.UpdateApplicationRequest = {
1107 ApplicationName: applicationName,
1108 CurrentApplicationVersionId: detail.ApplicationVersionId,
1109 };
1110 let dirty = false;
1111 if (Object.keys(configurationUpdate).length > 0) {
1112 update.ApplicationConfigurationUpdate = configurationUpdate;
1113 dirty = true;
1114 }
1115 if (news.runtimeEnvironment !== detail.RuntimeEnvironment) {
1116 update.RuntimeEnvironmentUpdate = news.runtimeEnvironment;
1117 dirty = true;
1118 }
1119 if (serviceExecutionRole !== detail.ServiceExecutionRole) {
1120 update.ServiceExecutionRoleUpdate = serviceExecutionRole;
1121 dirty = true;
1122 }
1123 if (!dirty) return;
1124
1125 yield* retryThroughRolePropagation(analytics.updateApplication(update));
1126 yield* session.note(`Updated application configuration for ${applicationName}`);
1127 });
1128 yield* retryWhileInUse(syncConfiguration);
1129 yield* waitForApplicationStable(applicationName);
1130
1131 // 3b. SYNC maintenance window — separate API from
1132 // UpdateApplication; only applied when the caller pins a window
1133 // (otherwise the service-assigned window is left as-is).
1134 if (news.maintenanceWindowStartTime !== undefined) {
1135 const syncMaintenanceWindow = Effect.gen(function* () {
1136 const detail = yield* describeApplicationDetail(applicationName);
1137 if (!detail) return;
1138 const observedStart =
1139 detail.ApplicationMaintenanceConfigurationDescription
1140 ?.ApplicationMaintenanceWindowStartTime;
1141 if (observedStart === news.maintenanceWindowStartTime) return;
1142 yield* analytics.updateApplicationMaintenanceConfiguration({
1143 ApplicationName: applicationName,
1144 ApplicationMaintenanceConfigurationUpdate: {
1145 ApplicationMaintenanceWindowStartTimeUpdate: news.maintenanceWindowStartTime!,
1146 },
1147 });
1148 yield* session.note(`Updated maintenance window for ${applicationName}`);
1149 });
1150 yield* retryWhileInUse(syncMaintenanceWindow);
1151 }
1152
1153 // 3c. SYNC VPC attach/detach — separate APIs from
1154 // UpdateApplication; each re-reads the fresh version id.
1155 const syncVpcAttachment = Effect.gen(function* () {
1156 const detail = yield* describeApplicationDetail(applicationName);
1157 if (!detail) return;
1158 const observedVpcs =
1159 detail.ApplicationConfigurationDescription?.VpcConfigurationDescriptions ?? [];
1160 if (news.vpc && observedVpcs.length === 0) {
1161 yield* analytics.addApplicationVpcConfiguration({
1162 ApplicationName: applicationName,
1163 CurrentApplicationVersionId: detail.ApplicationVersionId,
1164 VpcConfiguration: {
1165 SubnetIds: news.vpc.subnetIds,
1166 SecurityGroupIds: news.vpc.securityGroupIds,
1167 },
1168 });
1169 yield* session.note(`Attached VPC configuration to ${applicationName}`);
1170 } else if (!news.vpc && observedVpcs[0]?.VpcConfigurationId) {
1171 yield* analytics.deleteApplicationVpcConfiguration({
1172 ApplicationName: applicationName,
1173 CurrentApplicationVersionId: detail.ApplicationVersionId,
1174 VpcConfigurationId: observedVpcs[0].VpcConfigurationId,
1175 });
1176 yield* session.note(`Detached VPC configuration from ${applicationName}`);
1177 }
1178 });
1179 yield* retryWhileInUse(syncVpcAttachment);
1180 const stable = yield* waitForApplicationStable(applicationName);
1181
1182 // 3d. SYNC run state — start when `start: true`, force-stop a
1183 // running application when `start` is absent/false.
1184 if (news.start === true && stable.ApplicationStatus !== "RUNNING") {
1185 yield* retryWhileInUse(
1186 analytics.startApplication({
1187 ApplicationName: applicationName,
1188 }),
1189 );
1190 yield* session.note(`Starting application ${applicationName}...`);
1191 yield* waitForApplicationRunning(applicationName);
1192 } else if (
1193 news.start !== true &&
1194 (stable.ApplicationStatus === "RUNNING" || stable.ApplicationStatus === "AUTOSCALING")
1195 ) {
1196 yield* retryWhileInUse(
1197 analytics.stopApplication({
1198 ApplicationName: applicationName,
1199 Force: true,
1200 }),
1201 );
1202 yield* session.note(`Stopping application ${applicationName}...`);
1203 yield* waitForApplicationStopped(applicationName);
1204 }
1205
1206 // 3e. SYNC TAGS — diff against OBSERVED cloud tags (adoption may
1207 // bring an application with foreign tags), never olds/output.
1208 const applicationArn = stable.ApplicationARN;
1209 const observedTagsResponse = yield* analytics
1210 .listTagsForResource({ ResourceARN: applicationArn })
1211 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined));
1212 const observedTags = toTagRecord(observedTagsResponse?.Tags);
1213 const { removed, upsert } = diffTags(observedTags, desiredTags);
1214 if (removed.length > 0) {
1215 yield* retryWhileInUse(
1216 analytics.untagResource({
1217 ResourceARN: applicationArn,
1218 TagKeys: removed,
1219 }),
1220 );
1221 }
1222 if (upsert.length > 0) {
1223 yield* retryWhileInUse(
1224 analytics.tagResource({
1225 ResourceARN: applicationArn,
1226 Tags: upsert,
1227 }),
1228 );
1229 }
1230
1231 yield* session.note(applicationArn);
1232
1233 // Clean up a previously-synthesized role once the application has
1234 // been switched to a user-supplied `serviceExecutionRole` (the
1235 // sync above already re-pointed the app at the new role).
1236 if (!roleName && output?.roleName) {
1237 yield* deleteRoleIfExists(output.roleName);
1238 }
1239
1240 // 4. RETURN fresh attributes reflecting post-sync cloud state.
1241 const final = yield* readApplication({ applicationName, roleName });
1242 if (!final) {
1243 return yield* new ApplicationValidationError({
1244 message: `failed to read reconciled application ${applicationName}`,
1245 });
1246 }
1247 return final;
1248 }),
1249
1250 delete: Effect.fn(function* ({ output }) {
1251 // DeleteApplication requires the application's CreateTimestamp —
1252 // observe it fresh rather than trusting persisted state. Missing
1253 // application means the delete already happened.
1254 const detail = yield* describeApplicationDetail(output.applicationName);
1255 if (detail?.CreateTimestamp) {
1256 yield* retryWhileInUse(
1257 analytics
1258 .deleteApplication({
1259 ApplicationName: output.applicationName,
1260 CreateTimestamp: detail.CreateTimestamp,
1261 })
1262 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void)),
1263 );
1264 yield* waitForApplicationDeleted(output.applicationName);
1265 }
1266
1267 // Remove the synthesized IAM role (idempotent — it may already be
1268 // gone from a previous partial delete).
1269 if (output.roleName) {
1270 yield* deleteRoleIfExists(output.roleName);
1271 }
1272 }),
1273 });
1274 }),
1275 );