@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, 06:00 UTC

main
ts 748 lines 28.4 KB
Raw
1 import * as logs from "@distilled.cloud/aws/cloudwatch-logs";
2 import * as mwaa from "@distilled.cloud/aws/mwaa";
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 { createInternalTags, diffTags, hasAlchemyTags } from "alchemy/Tags";
9 import * as Data from "effect/Data";
10 import * as Effect from "effect/Effect";
11 import * as Redacted from "effect/Redacted";
12 import * as Schedule from "effect/Schedule";
13 import * as Stream from "effect/Stream";
14 import type { Providers } from "../Providers.ts";
15
16 export class MWAAEnvironmentNotReady extends Data.TaggedError("MWAAEnvironmentNotReady")<{
17 readonly message: string;
18 readonly cause?: unknown;
19 }> {}
20
21 export class MWAAEnvironmentOperationFailed extends Data.TaggedError(
22 "MWAAEnvironmentOperationFailed",
23 )<{
24 readonly message: string;
25 readonly cause?: unknown;
26 }> {}
27
28 export class MWAAEnvironmentUnexpectedResponse extends Data.TaggedError(
29 "MWAAEnvironmentUnexpectedResponse",
30 )<{
31 readonly message: string;
32 readonly cause?: unknown;
33 }> {}
34
35 /**
36 * Per-module CloudWatch logging configuration for an MWAA {@link Environment}.
37 */
38 export interface ModuleLoggingConfig {
39 /**
40 * Whether this Airflow log group is published to CloudWatch Logs.
41 */
42 enabled: boolean;
43 /**
44 * Airflow log level — one of `CRITICAL`, `ERROR`, `WARNING`, `INFO`, `DEBUG`.
45 */
46 logLevel: string;
47 }
48
49 /**
50 * CloudWatch logging configuration for the five Airflow log groups of an MWAA
51 * {@link Environment}. Any group left unset is disabled.
52 */
53 export interface EnvironmentLoggingConfig {
54 /** DAG processing logs. */
55 dagProcessingLogs?: ModuleLoggingConfig;
56 /** Scheduler logs. */
57 schedulerLogs?: ModuleLoggingConfig;
58 /** Webserver logs. */
59 webserverLogs?: ModuleLoggingConfig;
60 /** Worker logs. */
61 workerLogs?: ModuleLoggingConfig;
62 /** Task logs. */
63 taskLogs?: ModuleLoggingConfig;
64 }
65
66 export interface EnvironmentProps {
67 /**
68 * Name of the environment (1-80 chars, must start with a letter). If omitted,
69 * a deterministic physical name is generated. Changing the name replaces the
70 * environment.
71 */
72 environmentName?: string;
73 /**
74 * ARN of the IAM execution role Amazon MWAA and Apache Airflow assume to
75 * access AWS resources (the DAGs bucket, CloudWatch, SQS, KMS). Updateable
76 * in place.
77 */
78 executionRoleArn: string;
79 /**
80 * ARN of the S3 bucket that holds your DAG code, `requirements.txt`, plugins,
81 * and startup script. The bucket must block public access and have
82 * versioning enabled. Updateable in place.
83 */
84 sourceBucketArn: string;
85 /**
86 * Relative path (within the source bucket) to the folder containing your DAG
87 * files, e.g. `"dags"`. Updateable in place.
88 */
89 dagS3Path: string;
90 /**
91 * The private subnet IDs (exactly two, in distinct Availability Zones) the
92 * environment's webserver, scheduler, and workers run in. Changing the
93 * subnets replaces the environment.
94 */
95 subnetIds: string[];
96 /**
97 * VPC security group IDs applied to the environment's network interfaces.
98 * Updateable in place.
99 * @default a security group created for the environment
100 */
101 securityGroupIds?: string[];
102 /**
103 * Apache Airflow version, e.g. `"2.10.3"`. Downgrades are not permitted.
104 * @default the latest version supported by MWAA
105 */
106 airflowVersion?: string;
107 /**
108 * Environment class (sizing), e.g. `"mw1.small"`, `"mw1.medium"`,
109 * `"mw1.large"`. Updateable in place.
110 * @default "mw1.small"
111 */
112 environmentClass?: string;
113 /**
114 * Maximum number of Airflow workers to scale up to. Updateable in place.
115 * @default 10
116 */
117 maxWorkers?: number;
118 /**
119 * Minimum number of Airflow workers. Updateable in place.
120 * @default 1
121 */
122 minWorkers?: number;
123 /**
124 * Maximum number of Airflow web servers (Airflow 2.2.2+). Updateable in
125 * place.
126 */
127 maxWebservers?: number;
128 /**
129 * Minimum number of Airflow web servers (Airflow 2.2.2+). Updateable in
130 * place.
131 */
132 minWebservers?: number;
133 /**
134 * Number of Airflow schedulers. Updateable in place.
135 */
136 schedulers?: number;
137 /**
138 * Apache Airflow web server access mode — `PRIVATE_ONLY` (VPC-only endpoint)
139 * or `PUBLIC_ONLY` (internet-accessible). Updateable in place.
140 * @default "PRIVATE_ONLY"
141 */
142 webserverAccessMode?: string;
143 /**
144 * Day and time of the weekly 30-minute maintenance window in
145 * `DAY:HH:MM` (UTC), e.g. `"MON:03:30"`. Updateable in place.
146 */
147 weeklyMaintenanceWindowStart?: string;
148 /**
149 * ARN or key ID of the customer-managed KMS key used to encrypt data.
150 * Changing the key replaces the environment.
151 * @default an AWS-owned key
152 */
153 kmsKey?: string;
154 /**
155 * Whether the VPC endpoints for the environment are managed by MWAA
156 * (`SERVICE`) or by you (`CUSTOMER`). Changing this replaces the environment.
157 * @default "SERVICE"
158 */
159 endpointManagement?: string;
160 /**
161 * Apache Airflow configuration overrides, keyed by
162 * `section.option`, e.g. `{ "core.default_task_retries": "3" }`. Values
163 * carrying secrets (SMTP passwords, connection URIs, …) may be passed as
164 * `Redacted.Redacted<string>` so they never appear in logs. Updateable in
165 * place.
166 */
167 airflowConfigurationOptions?: Record<string, string | Redacted.Redacted<string>>;
168 /**
169 * Relative path to the plugins `.zip` in the source bucket. Updateable in
170 * place.
171 */
172 pluginsS3Path?: string;
173 /**
174 * S3 object version of the plugins `.zip`. Updateable in place.
175 */
176 pluginsS3ObjectVersion?: string;
177 /**
178 * Relative path to `requirements.txt` in the source bucket. Updateable in
179 * place.
180 */
181 requirementsS3Path?: string;
182 /**
183 * S3 object version of `requirements.txt`. Updateable in place.
184 */
185 requirementsS3ObjectVersion?: string;
186 /**
187 * Relative path to the startup shell script in the source bucket. Updateable
188 * in place.
189 */
190 startupScriptS3Path?: string;
191 /**
192 * S3 object version of the startup script. Updateable in place.
193 */
194 startupScriptS3ObjectVersion?: string;
195 /**
196 * CloudWatch logging configuration for the five Airflow log groups.
197 * Updateable in place.
198 */
199 loggingConfiguration?: EnvironmentLoggingConfig;
200 /**
201 * User-defined tags for the environment.
202 */
203 tags?: Record<string, string>;
204 }
205
206 export interface Environment extends Resource<
207 "AWS.MWAA.Environment",
208 EnvironmentProps,
209 {
210 /** Name of the environment. */
211 environmentName: string;
212 /** ARN of the environment. */
213 arn: string;
214 /** Current lifecycle status (e.g. `CREATING`, `AVAILABLE`). */
215 status: string;
216 /** Host name of the Airflow web server UI. */
217 webserverUrl: string | undefined;
218 /** ARN of the execution role Airflow tasks run as. */
219 executionRoleArn: string | undefined;
220 /** ARN of the service-linked role MWAA uses to manage the environment. */
221 serviceRoleArn: string | undefined;
222 /** Running Apache Airflow version. */
223 airflowVersion: string | undefined;
224 /** Environment size class (e.g. `mw1.small`). */
225 environmentClass: string | undefined;
226 /** ARN of the S3 bucket holding DAG code. */
227 sourceBucketArn: string | undefined;
228 /** Relative S3 path to the DAGs folder. */
229 dagS3Path: string | undefined;
230 /** Celery executor queue used by the environment's workers. */
231 celeryExecutorQueue: string | undefined;
232 /** VPC endpoint service name for the Airflow metadata database. */
233 databaseVpcEndpointService: string | undefined;
234 /** VPC endpoint service name for the Airflow web server. */
235 webserverVpcEndpointService: string | undefined;
236 /** Tags on the environment (user + internal Alchemy tags). */
237 tags: Record<string, string>;
238 },
239 never,
240 Providers
241 > {}
242
243 /**
244 * An Amazon Managed Workflows for Apache Airflow (MWAA) environment — a fully
245 * managed Apache Airflow deployment that runs your DAGs.
246 *
247 * Environments take roughly 20-30 minutes to create and are billed hourly for
248 * the environment, workers, and schedulers while they exist. Each environment
249 * needs an S3 bucket for DAG code (versioned, public access blocked), an IAM
250 * execution role, and two private subnets in distinct Availability Zones with
251 * outbound internet access (via NAT gateway or VPC endpoints). Destroy
252 * environments you are not using.
253 * ### Creating an Environment
254 * **Example:** Basic Environment
255 * ```typescript
256 * const environment = yield* Environment("Airflow", {
257 * executionRoleArn: role.roleArn,
258 * sourceBucketArn: bucket.bucketArn,
259 * dagS3Path: "dags",
260 * subnetIds: [privateSubnetA.subnetId, privateSubnetB.subnetId],
261 * airflowVersion: "2.10.3",
262 * environmentClass: "mw1.small",
263 * maxWorkers: 5,
264 * });
265 * ```
266 *
267 * **Example:** Public Webserver with Logging
268 * ```typescript
269 * const environment = yield* Environment("Airflow", {
270 * executionRoleArn: role.roleArn,
271 * sourceBucketArn: bucket.bucketArn,
272 * dagS3Path: "dags",
273 * subnetIds: [privateSubnetA.subnetId, privateSubnetB.subnetId],
274 * webserverAccessMode: "PUBLIC_ONLY",
275 * loggingConfiguration: {
276 * schedulerLogs: { enabled: true, logLevel: "INFO" },
277 * workerLogs: { enabled: true, logLevel: "INFO" },
278 * taskLogs: { enabled: true, logLevel: "INFO" },
279 * },
280 * airflowConfigurationOptions: {
281 * "core.default_task_retries": "3",
282 * },
283 * });
284 * ```
285 *
286 * @resource
287 */
288 export const Environment = Resource<Environment>("AWS.MWAA.Environment");
289
290 // Terminal states: environment stopped converging.
291 const FAILED_STATES = new Set(["CREATE_FAILED", "UPDATE_FAILED", "UNAVAILABLE", "ROLLBACK_FAILED"]);
292
293 const toModuleInput = (
294 config: ModuleLoggingConfig | undefined,
295 ): mwaa.ModuleLoggingConfigurationInput | undefined =>
296 config === undefined ? undefined : { Enabled: config.enabled, LogLevel: config.logLevel };
297
298 const toLoggingInput = (
299 config: EnvironmentLoggingConfig | undefined,
300 ): mwaa.LoggingConfigurationInput | undefined => {
301 if (config === undefined) return undefined;
302 return {
303 DagProcessingLogs: toModuleInput(config.dagProcessingLogs),
304 SchedulerLogs: toModuleInput(config.schedulerLogs),
305 WebserverLogs: toModuleInput(config.webserverLogs),
306 WorkerLogs: toModuleInput(config.workerLogs),
307 TaskLogs: toModuleInput(config.taskLogs),
308 };
309 };
310
311 // Compare desired logging against the OBSERVED configuration. A module left
312 // unset is disabled; a disabled module's log level is irrelevant.
313 const LOG_MODULES = [
314 ["dagProcessingLogs", "DagProcessingLogs"],
315 ["schedulerLogs", "SchedulerLogs"],
316 ["webserverLogs", "WebserverLogs"],
317 ["workerLogs", "WorkerLogs"],
318 ["taskLogs", "TaskLogs"],
319 ] as const;
320
321 const sameLogging = (
322 desired: EnvironmentLoggingConfig,
323 observed: mwaa.LoggingConfiguration | undefined,
324 ): boolean =>
325 LOG_MODULES.every(([prop, wire]) => {
326 const want = desired[prop];
327 const have = observed?.[wire];
328 const wantEnabled = want?.enabled ?? false;
329 if (wantEnabled !== (have?.Enabled ?? false)) return false;
330 return !wantEnabled || want?.logLevel === have?.LogLevel;
331 });
332
333 const sameStringSet = (a: string[], b: string[]): boolean => {
334 if (a.length !== b.length) return false;
335 const setB = new Set(b);
336 return a.every((v) => setB.has(v));
337 };
338
339 const configValue = (v: string | Redacted.Redacted<string> | undefined): string | undefined =>
340 Redacted.isRedacted(v) ? Redacted.value(v) : v;
341
342 // Compare desired Airflow configuration options against the observed ones.
343 // Both sides may carry Redacted values (the props by choice, the observed
344 // options because distilled decodes them as SensitiveString) — unwrap for the
345 // comparison only.
346 const sameConfigOptions = (
347 a: Record<string, string | Redacted.Redacted<string>> | undefined,
348 b: { [key: string]: string | Redacted.Redacted<string> | undefined } | undefined,
349 ): boolean => {
350 const ea = Object.entries(a ?? {});
351 const eb = Object.entries(b ?? {}).filter(([, v]) => v !== undefined);
352 if (ea.length !== eb.length) return false;
353 const bMap = b ?? {};
354 return ea.every(([k, v]) => configValue(bMap[k]) === configValue(v));
355 };
356
357 /** The modules MWAA creates `airflow-{environment}-{module}` log groups for. */
358 const MWAA_LOG_MODULES = ["DAGProcessing", "Scheduler", "Task", "WebServer", "Worker"] as const;
359
360 export const EnvironmentProvider = () =>
361 Provider.effect(
362 Environment,
363 Effect.gen(function* () {
364 const toName = (id: string, props: Partial<EnvironmentProps>) =>
365 props.environmentName
366 ? Effect.succeed(props.environmentName)
367 : createPhysicalName({ id, maxLength: 80 });
368
369 const readEnvironment = Effect.fn(function* (name: string) {
370 const response = yield* mwaa
371 .getEnvironment({ Name: name })
372 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined));
373 return response?.Environment;
374 });
375
376 const readTags = Effect.fn(function* (arn: string) {
377 const response = yield* mwaa
378 .listTagsForResource({ ResourceArn: arn })
379 .pipe(Effect.orElseSucceed(() => undefined));
380 const tags: Record<string, string> = {};
381 for (const [key, value] of Object.entries(response?.Tags ?? {})) {
382 if (value !== undefined) tags[key] = value;
383 }
384 return tags;
385 });
386
387 // Bounded readiness wait. MWAA environment provisioning/modification
388 // typically completes in 20-30 minutes; budget ~45 min (90 * 30s). A
389 // FAILED terminal state stops the wait immediately (non-retryable).
390 const waitForAvailable = Effect.fn(function* (name: string) {
391 const policy = Schedule.max([Schedule.fixed("30 seconds"), Schedule.recurs(90)]);
392 return yield* readEnvironment(name).pipe(
393 Effect.flatMap(
394 Effect.fnUntraced(function* (env) {
395 if (!env?.Arn) {
396 return yield* new MWAAEnvironmentNotReady({
397 message: `Environment '${name}' not found`,
398 });
399 }
400 const status = env.Status ?? "CREATING";
401 if (FAILED_STATES.has(status)) {
402 return yield* new MWAAEnvironmentOperationFailed({
403 message: `Environment '${name}' reached terminal state '${status}'`,
404 });
405 }
406 if (status !== "AVAILABLE") {
407 return yield* new MWAAEnvironmentNotReady({
408 message: `Environment '${name}' not available (status: ${status})`,
409 });
410 }
411 return env;
412 }),
413 ),
414 Effect.retry({
415 while: (e) => e instanceof Error && !e.message.includes("terminal state"),
416 schedule: policy,
417 }),
418 );
419 });
420
421 const toAttrs = Effect.fn(function* (env: mwaa.Environment) {
422 if (!env.Name || !env.Arn) {
423 return yield* new MWAAEnvironmentUnexpectedResponse({
424 message: `Environment '${env.Name}' is missing its ARN`,
425 });
426 }
427 return {
428 environmentName: env.Name,
429 arn: env.Arn,
430 status: env.Status ?? "CREATING",
431 webserverUrl: env.WebserverUrl,
432 executionRoleArn: env.ExecutionRoleArn,
433 serviceRoleArn: env.ServiceRoleArn,
434 airflowVersion: env.AirflowVersion,
435 environmentClass: env.EnvironmentClass,
436 sourceBucketArn: env.SourceBucketArn,
437 dagS3Path: env.DagS3Path,
438 celeryExecutorQueue: env.CeleryExecutorQueue,
439 databaseVpcEndpointService: env.DatabaseVpcEndpointService,
440 webserverVpcEndpointService: env.WebserverVpcEndpointService,
441 tags: yield* readTags(env.Arn),
442 };
443 });
444
445 return {
446 stables: ["environmentName", "arn"],
447
448 diff: Effect.fn(function* ({ id, olds, news }) {
449 if (!isResolved(news)) return undefined;
450 const n = news;
451 const o = olds;
452 if (n === undefined || o === undefined) return undefined;
453 if ((yield* toName(id, o)) !== (yield* toName(id, n))) {
454 return { action: "replace" } as const;
455 }
456 // Create-only properties force a replacement.
457 if (!sameStringSet(n.subnetIds ?? [], o.subnetIds ?? [])) {
458 return { action: "replace" } as const;
459 }
460 if ((n.kmsKey ?? undefined) !== (o.kmsKey ?? undefined)) {
461 return { action: "replace" } as const;
462 }
463 if ((n.endpointManagement ?? undefined) !== (o.endpointManagement ?? undefined)) {
464 return { action: "replace" } as const;
465 }
466 }),
467
468 read: Effect.fn(function* ({ id, olds, output }) {
469 const name = output?.environmentName ?? (yield* toName(id, olds ?? {}));
470 const env = yield* readEnvironment(name);
471 if (!env?.Arn) return undefined;
472 const attrs = yield* toAttrs(env);
473 return (yield* hasAlchemyTags(id, attrs.tags)) ? attrs : Unowned(attrs);
474 }),
475
476 reconcile: Effect.fn(function* ({ id, news, output, session }) {
477 const props = news!;
478 const name = output?.environmentName ?? (yield* toName(id, props));
479 const internalTags = yield* createInternalTags(id);
480 const desiredTags = { ...internalTags, ...props.tags };
481
482 // 1. Observe — cloud state is authoritative.
483 let observed = yield* readEnvironment(name);
484
485 // 2. Ensure — create if missing. Create errors (invalid subnets,
486 // role, etc.) propagate so misconfiguration surfaces directly
487 // rather than as a confusing "not found" after the wait below.
488 if (observed === undefined) {
489 yield* mwaa
490 .createEnvironment({
491 Name: name,
492 ExecutionRoleArn: props.executionRoleArn,
493 SourceBucketArn: props.sourceBucketArn,
494 DagS3Path: props.dagS3Path,
495 NetworkConfiguration: {
496 SubnetIds: props.subnetIds,
497 SecurityGroupIds: props.securityGroupIds,
498 },
499 AirflowVersion: props.airflowVersion,
500 EnvironmentClass: props.environmentClass,
501 MaxWorkers: props.maxWorkers,
502 MinWorkers: props.minWorkers,
503 MaxWebservers: props.maxWebservers,
504 MinWebservers: props.minWebservers,
505 Schedulers: props.schedulers,
506 WebserverAccessMode: props.webserverAccessMode,
507 WeeklyMaintenanceWindowStart: props.weeklyMaintenanceWindowStart,
508 KmsKey: props.kmsKey,
509 EndpointManagement: props.endpointManagement,
510 AirflowConfigurationOptions: props.airflowConfigurationOptions,
511 PluginsS3Path: props.pluginsS3Path,
512 PluginsS3ObjectVersion: props.pluginsS3ObjectVersion,
513 RequirementsS3Path: props.requirementsS3Path,
514 RequirementsS3ObjectVersion: props.requirementsS3ObjectVersion,
515 StartupScriptS3Path: props.startupScriptS3Path,
516 StartupScriptS3ObjectVersion: props.startupScriptS3ObjectVersion,
517 LoggingConfiguration: toLoggingInput(props.loggingConfiguration),
518 Tags: desiredTags,
519 })
520 .pipe(
521 // A freshly created execution role is not yet assumable by
522 // airflow-env.amazonaws.com (IAM eventual consistency) —
523 // CreateEnvironment rejects it with "Failed to assume role" for
524 // several seconds. Retry bounded.
525 Effect.retry({
526 while: (e) => e._tag === "MwaaExecutionRoleNotAssumable",
527 schedule: Schedule.spaced("5 seconds"),
528 times: 10,
529 }),
530 );
531 }
532
533 // Provisioning and in-flight modifications both surface as a
534 // non-AVAILABLE status; wait for availability (bounded) so update
535 // calls do not hit a conflict.
536 observed = yield* waitForAvailable(name);
537
538 // 3. Sync — compute an update delta from OBSERVED state.
539 const update: mwaa.UpdateEnvironmentInput = { Name: name };
540 let mutated = false;
541 if (
542 props.executionRoleArn !== undefined &&
543 props.executionRoleArn !== observed.ExecutionRoleArn
544 ) {
545 update.ExecutionRoleArn = props.executionRoleArn;
546 mutated = true;
547 }
548 if (
549 props.sourceBucketArn !== undefined &&
550 props.sourceBucketArn !== observed.SourceBucketArn
551 ) {
552 update.SourceBucketArn = props.sourceBucketArn;
553 mutated = true;
554 }
555 if (props.dagS3Path !== undefined && props.dagS3Path !== observed.DagS3Path) {
556 update.DagS3Path = props.dagS3Path;
557 mutated = true;
558 }
559 if (
560 props.airflowVersion !== undefined &&
561 props.airflowVersion !== observed.AirflowVersion
562 ) {
563 update.AirflowVersion = props.airflowVersion;
564 mutated = true;
565 }
566 if (
567 props.environmentClass !== undefined &&
568 props.environmentClass !== observed.EnvironmentClass
569 ) {
570 update.EnvironmentClass = props.environmentClass;
571 mutated = true;
572 }
573 if (props.maxWorkers !== undefined && props.maxWorkers !== observed.MaxWorkers) {
574 update.MaxWorkers = props.maxWorkers;
575 mutated = true;
576 }
577 if (props.minWorkers !== undefined && props.minWorkers !== observed.MinWorkers) {
578 update.MinWorkers = props.minWorkers;
579 mutated = true;
580 }
581 if (props.maxWebservers !== undefined && props.maxWebservers !== observed.MaxWebservers) {
582 update.MaxWebservers = props.maxWebservers;
583 mutated = true;
584 }
585 if (props.minWebservers !== undefined && props.minWebservers !== observed.MinWebservers) {
586 update.MinWebservers = props.minWebservers;
587 mutated = true;
588 }
589 if (props.schedulers !== undefined && props.schedulers !== observed.Schedulers) {
590 update.Schedulers = props.schedulers;
591 mutated = true;
592 }
593 if (
594 props.webserverAccessMode !== undefined &&
595 props.webserverAccessMode !== observed.WebserverAccessMode
596 ) {
597 update.WebserverAccessMode = props.webserverAccessMode;
598 mutated = true;
599 }
600 if (
601 props.weeklyMaintenanceWindowStart !== undefined &&
602 props.weeklyMaintenanceWindowStart !== observed.WeeklyMaintenanceWindowStart
603 ) {
604 update.WeeklyMaintenanceWindowStart = props.weeklyMaintenanceWindowStart;
605 mutated = true;
606 }
607 const observedSg = observed.NetworkConfiguration?.SecurityGroupIds;
608 if (
609 props.securityGroupIds !== undefined &&
610 !sameStringSet(props.securityGroupIds, observedSg ?? [])
611 ) {
612 update.NetworkConfiguration = {
613 SecurityGroupIds: props.securityGroupIds,
614 };
615 mutated = true;
616 }
617 if (props.pluginsS3Path !== undefined && props.pluginsS3Path !== observed.PluginsS3Path) {
618 update.PluginsS3Path = props.pluginsS3Path;
619 update.PluginsS3ObjectVersion = props.pluginsS3ObjectVersion;
620 mutated = true;
621 }
622 if (
623 props.requirementsS3Path !== undefined &&
624 props.requirementsS3Path !== observed.RequirementsS3Path
625 ) {
626 update.RequirementsS3Path = props.requirementsS3Path;
627 update.RequirementsS3ObjectVersion = props.requirementsS3ObjectVersion;
628 mutated = true;
629 }
630 if (
631 props.startupScriptS3Path !== undefined &&
632 props.startupScriptS3Path !== observed.StartupScriptS3Path
633 ) {
634 update.StartupScriptS3Path = props.startupScriptS3Path;
635 update.StartupScriptS3ObjectVersion = props.startupScriptS3ObjectVersion;
636 mutated = true;
637 }
638 if (
639 props.airflowConfigurationOptions !== undefined &&
640 !sameConfigOptions(
641 props.airflowConfigurationOptions,
642 observed.AirflowConfigurationOptions,
643 )
644 ) {
645 update.AirflowConfigurationOptions = props.airflowConfigurationOptions;
646 mutated = true;
647 }
648 if (
649 props.loggingConfiguration !== undefined &&
650 !sameLogging(props.loggingConfiguration, observed.LoggingConfiguration)
651 ) {
652 update.LoggingConfiguration = toLoggingInput(props.loggingConfiguration);
653 mutated = true;
654 }
655
656 if (mutated) {
657 yield* mwaa.updateEnvironment(update);
658 observed = yield* waitForAvailable(name);
659 }
660
661 // 3b. Sync tags — diff against OBSERVED cloud tags.
662 const arn = observed.Arn;
663 if (arn) {
664 const observedTags = yield* readTags(arn);
665 const { removed, upsert } = diffTags(observedTags, desiredTags);
666 if (upsert.length > 0) {
667 yield* mwaa.tagResource({
668 ResourceArn: arn,
669 Tags: Object.fromEntries(upsert.map(({ Key, Value }) => [Key, Value])),
670 });
671 }
672 if (removed.length > 0) {
673 yield* mwaa.untagResource({ ResourceArn: arn, tagKeys: removed });
674 }
675 }
676
677 yield* session.note(name);
678 return yield* toAttrs(observed);
679 }),
680
681 delete: Effect.fn(function* ({ output }) {
682 const name = output.environmentName;
683 const existing = yield* readEnvironment(name);
684 if (existing === undefined) return;
685 // A second DeleteEnvironment on an environment that is already
686 // DELETING is rejected; only initiate when it is not in flight.
687 if (existing.Status !== "DELETING") {
688 yield* mwaa
689 .deleteEnvironment({ Name: name })
690 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void));
691 }
692 // Wait until the environment is gone. MWAA keeps ENIs in the
693 // customer's subnets (under the environment's security groups)
694 // until deletion completes, so returning early makes every
695 // downstream Subnet/SecurityGroup/VPC delete fail with
696 // DependencyViolation. Deletion typically takes 20-30 minutes;
697 // budget ~45 min (90 * 30s). DELETE_FAILED stops immediately.
698 yield* readEnvironment(name).pipe(
699 Effect.flatMap(
700 Effect.fnUntraced(function* (env) {
701 if (env === undefined || env.Status === "DELETED") return;
702 if (env.Status === "DELETE_FAILED") {
703 return yield* new MWAAEnvironmentOperationFailed({
704 message: `Environment '${name}' reached terminal state 'DELETE_FAILED'`,
705 });
706 }
707 return yield* new MWAAEnvironmentNotReady({
708 message: `Environment '${name}' still deleting (status: ${env.Status})`,
709 });
710 }),
711 ),
712 Effect.retry({
713 while: (e) => e instanceof Error && !e.message.includes("terminal state"),
714 schedule: Schedule.max([Schedule.fixed("30 seconds"), Schedule.recurs(90)]),
715 }),
716 );
717 // MWAA creates a log group per enabled module and leaves them when
718 // the environment is deleted, so every environment would leak them.
719 yield* Effect.forEach(
720 MWAA_LOG_MODULES,
721 (module) =>
722 logs
723 .deleteLogGroup({ logGroupName: `airflow-${name}-${module}` })
724 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void)),
725 { discard: true },
726 );
727 }),
728
729 list: () =>
730 mwaa.listEnvironments.pages({}).pipe(
731 Stream.runCollect,
732 Effect.map((chunk) => Array.from(chunk).flatMap((page) => page.Environments ?? [])),
733 Effect.flatMap(
734 Effect.forEach((name) => readEnvironment(name), {
735 concurrency: 4,
736 }),
737 ),
738 Effect.map((envs) =>
739 envs.filter(
740 (env): env is mwaa.Environment =>
741 env !== undefined && env.Name !== undefined && !!env.Arn,
742 ),
743 ),
744 Effect.flatMap(Effect.forEach((env) => toAttrs(env), { concurrency: 4 })),
745 ),
746 };
747 }),
748 );