The best way to control Claude Code, Codex and more from any computer or phone

main
ts 193 lines 6.96 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { AWSEnvironment } from "@alchemy.run/aws/Environment";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as logs from "@distilled.cloud/aws/cloudwatch-logs";
6 import * as mwaa from "@distilled.cloud/aws/mwaa";
7 import * as Output from "alchemy/Output";
8 import * as Effect from "effect/Effect";
9 import * as Schedule from "effect/Schedule";
10 import { MwaaNetwork } from "./network.ts";
11
12 const { test } = Test.make({ providers: AWS.providers() });
13
14 // Ungated typed-error probe: proves the distilled mwaa error union carries the
15 // ResourceNotFoundException tag this provider's read/delete paths depend on. A
16 // well-formed but nonexistent environment name returns ResourceNotFoundException.
17 test.provider(
18 "getEnvironment on a nonexistent environment fails with ResourceNotFoundException",
19 () =>
20 Effect.gen(function* () {
21 const error = yield* Effect.flip(
22 mwaa.getEnvironment({ Name: "alchemy-mwaa-nonexistent-probe" }),
23 );
24 expect(error._tag).toBe("ResourceNotFoundException");
25 }),
26 { tags: ["provider:aws", "provider:aws:mwaa", "live"] },
27 );
28
29 // MWAA execution role: trusts the Airflow environment + task principals and
30 // grants the standard access to the DAGs bucket, CloudWatch Logs, SQS, and KMS.
31 const executionRolePolicy = (
32 bucketArn: Output.Output<string>,
33 region: string,
34 account: string,
35 ) => ({
36 Version: "2012-10-17" as const,
37 Statement: [
38 {
39 Effect: "Allow" as const,
40 Action: ["airflow:PublishMetrics"],
41 Resource: [`arn:aws:airflow:${region}:${account}:environment/*`],
42 },
43 {
44 Effect: "Allow" as const,
45 Action: ["s3:GetObject*", "s3:GetBucket*", "s3:List*"],
46 Resource: [bucketArn, Output.interpolate`${bucketArn}/*`],
47 },
48 {
49 Effect: "Allow" as const,
50 Action: [
51 "logs:CreateLogStream",
52 "logs:CreateLogGroup",
53 "logs:PutLogEvents",
54 "logs:GetLogEvents",
55 "logs:GetLogRecord",
56 "logs:GetLogGroupFields",
57 "logs:GetQueryResults",
58 "logs:DescribeLogGroups",
59 ],
60 Resource: [`arn:aws:logs:${region}:${account}:log-group:airflow-*`],
61 },
62 {
63 Effect: "Allow" as const,
64 Action: ["cloudwatch:PutMetricData"],
65 Resource: ["*"],
66 },
67 {
68 Effect: "Allow" as const,
69 Action: [
70 "sqs:ChangeMessageVisibility",
71 "sqs:DeleteMessage",
72 "sqs:GetQueueAttributes",
73 "sqs:GetQueueUrl",
74 "sqs:ReceiveMessage",
75 "sqs:SendMessage",
76 ],
77 Resource: [`arn:aws:sqs:${region}:*:airflow-celery-*`],
78 },
79 ],
80 });
81
82 // MWAA environments take ~20-30 minutes to create and ~20-30 minutes to
83 // delete (the provider waits for deletion so the in-stack VPC can be torn
84 // down), and are billed hourly (~$0.49/h for mw1.small, plus ~$0.045/h for the
85 // test's single NAT gateway). The test builds its own private networking
86 // (VPC + 2 private subnets in 2 AZs + IGW + one NAT gateway, see network.ts),
87 // so the only reason for skipping it under `--fast` is time and cost.
88 test.provider.skipIf(!!process.env.FAST)(
89 "create environment, verify available, destroy, verify gone",
90 (stack) =>
91 Effect.gen(function* () {
92 yield* stack.destroy();
93
94 const { accountId, region } = yield* AWSEnvironment.current;
95
96 const { environment } = yield* stack.deploy(
97 Effect.gen(function* () {
98 const { subnetIds, securityGroupIds } = yield* MwaaNetwork("MwaaNetwork", "10.86.0.0/16");
99 const bucket = yield* AWS.S3.Bucket("Dags", {
100 versioning: "Enabled",
101 forceDestroy: true,
102 });
103 const role = yield* AWS.IAM.Role("ExecutionRole", {
104 assumeRolePolicyDocument: {
105 Version: "2012-10-17",
106 Statement: [
107 {
108 Effect: "Allow",
109 Principal: {
110 Service: ["airflow.amazonaws.com", "airflow-env.amazonaws.com"],
111 },
112 Action: ["sts:AssumeRole"],
113 },
114 ],
115 },
116 inlinePolicies: {
117 "mwaa-execution": executionRolePolicy(bucket.bucketArn, region, accountId),
118 },
119 });
120 const environment = yield* AWS.MWAA.Environment("Airflow", {
121 executionRoleArn: role.roleArn,
122 sourceBucketArn: bucket.bucketArn,
123 dagS3Path: "dags",
124 subnetIds,
125 securityGroupIds,
126 environmentClass: "mw1.small",
127 maxWorkers: 2,
128 minWorkers: 1,
129 webserverAccessMode: "PUBLIC_ONLY",
130 loggingConfiguration: {
131 schedulerLogs: { enabled: true, logLevel: "INFO" },
132 taskLogs: { enabled: true, logLevel: "INFO" },
133 },
134 tags: { fixture: "mwaa-environment" },
135 });
136 return { environment };
137 }),
138 );
139
140 expect(environment.environmentName).toBeDefined();
141 expect(environment.arn).toContain(":environment/");
142 expect(environment.status).toBe("AVAILABLE");
143 expect(environment.webserverUrl).toBeDefined();
144
145 // Out-of-band verification via distilled.
146 const described = yield* mwaa.getEnvironment({
147 Name: environment.environmentName,
148 });
149 expect(described.Environment?.Status).toBe("AVAILABLE");
150 expect(described.Environment?.EnvironmentClass).toBe("mw1.small");
151 expect(described.Environment?.Tags?.fixture).toBe("mwaa-environment");
152
153 yield* stack.destroy();
154 yield* assertEnvironmentDeleted(environment.environmentName);
155 // MWAA leaves its per-module log groups behind; the delete reaps them.
156 const { logGroups } = yield* logs.describeLogGroups({
157 logGroupNamePrefix: `airflow-${environment.environmentName}-`,
158 });
159 expect(logGroups ?? []).toEqual([]);
160 // Billable NAT gateway + environment: never leave them behind on failure.
161 }).pipe(Effect.onError(() => stack.destroy().pipe(Effect.ignore))),
162 // network + create (~20-30 min) + delete-until-gone (~20-30 min) + network teardown.
163 {
164 tags: [
165 "provider:aws",
166 "provider:aws:ec2",
167 "provider:aws:iam",
168 "provider:aws:mwaa",
169 "provider:aws:s3",
170 "live",
171 ],
172 timeout: 6_000_000,
173 },
174 );
175
176 // The provider's delete waits until the environment is gone; tolerate a
177 // lingering DELETING status for eventual consistency of GetEnvironment.
178 const assertEnvironmentDeleted = (name: string) =>
179 Effect.gen(function* () {
180 const status = yield* mwaa.getEnvironment({ Name: name }).pipe(
181 Effect.map((r) => r.Environment?.Status ?? "gone"),
182 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed("gone" as const)),
183 );
184 if (status !== "gone" && status !== "DELETING") {
185 return yield* Effect.fail(
186 new Error(`MWAA environment '${name}' still exists (status: ${status})`),
187 );
188 }
189 }).pipe(
190 Effect.retry({
191 schedule: Schedule.max([Schedule.fixed("15 seconds"), Schedule.recurs(20)]),
192 }),
193 );