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

main
ts 435 lines 14.8 KB
Raw
1 import * as logs from "@distilled.cloud/aws/cloudwatch-logs";
2 import * as ec2 from "@distilled.cloud/aws/ec2";
3 import * as iam from "@distilled.cloud/aws/iam";
4 import * as lambda from "@distilled.cloud/aws/lambda";
5 import * as rds from "@distilled.cloud/aws/rds";
6 import * as secretsmanager from "@distilled.cloud/aws/secrets-manager";
7 import * as Data from "effect/Data";
8 import * as Effect from "effect/Effect";
9 import * as Schedule from "effect/Schedule";
10 import * as Stream from "effect/Stream";
11
12 /**
13 * Out-of-band orphan reaper for the RDSData bindings fixture.
14 *
15 * The suite's scratch stack keeps its state IN MEMORY, so a run that dies
16 * between deploy and a completed destroy (Aurora SV2 provisioning/teardown
17 * runs 10+ minutes — an easy target for a killed process) orphans the whole
18 * fixture with no state left to destroy from. This reaper finds fixture
19 * resources by their deterministic physical-name prefix (`RDSDataBindings-`,
20 * from `createPhysicalName`'s `{stack}-{id}-{stage}-` scheme) and the
21 * `alchemy::stack` ownership tag / fixed `10.61.0.0/16` CIDR for EC2, and
22 * deletes them with typed, idempotent calls.
23 *
24 * It runs as a pre-clean in `beforeAll` (before deploy) and as an
25 * `Effect.ensuring` finalizer in `afterAll` (even when destroy fails), so a
26 * SLOW run converges to zero leftovers regardless of how the previous run
27 * ended. Every delete catches its not-found tag; bounded retries cover
28 * `DependencyViolation` (Lambda ENIs release asynchronously after function
29 * deletion) and in-flight RDS state transitions.
30 */
31
32 /** Stack name of the scratch stack in Bindings.test.ts. */
33 const STACK = "RDSDataBindings";
34 /** Physical names are `{stack}-{id}-{stage}-{suffix}`; RDS lowercases identifiers. */
35 const PREFIX = `${STACK.toLowerCase()}-`;
36 /** The fixture VPC's deterministic CIDR (see infra.ts). */
37 const VPC_CIDR = "10.61.0.0/16";
38
39 const matches = (name: string | undefined): name is string =>
40 !!name && name.toLowerCase().startsWith(PREFIX);
41
42 class OrphanStillPresent extends Data.TaggedError("OrphanStillPresent")<{
43 readonly kind: string;
44 }> {}
45
46 /** ~10 minute ceiling for slow asynchronous releases (RDS deletes, Lambda ENIs). */
47 const slowRelease = Schedule.max([Schedule.spaced("15 seconds"), Schedule.recurs(40)]);
48 /** ~2 minute ceiling for ordinary dependency-violation races. */
49 const dependencyRelease = Schedule.max([Schedule.spaced("10 seconds"), Schedule.recurs(12)]);
50
51 const reapFunctions = Effect.gen(function* () {
52 const pages = yield* lambda.listFunctions.pages({}).pipe(Stream.runCollect);
53 const names = Array.from(pages)
54 .flatMap((page) => page.Functions ?? [])
55 .map((fn) => fn.FunctionName)
56 .filter(matches);
57 yield* Effect.forEach(
58 names,
59 (FunctionName) =>
60 Effect.logInfo(`RDSData reap: deleting Lambda function ${FunctionName}`).pipe(
61 Effect.andThen(lambda.deleteFunction({ FunctionName })),
62 Effect.retry({
63 while: (e) =>
64 e._tag === "ResourceConflictException" || e._tag === "TooManyRequestsException",
65 schedule: dependencyRelease,
66 }),
67 Effect.catchTag("ResourceNotFoundException", () => Effect.void),
68 ),
69 { discard: true },
70 );
71 });
72
73 const reapLogGroups = Effect.gen(function* () {
74 const pages = yield* logs.describeLogGroups
75 .pages({ logGroupNamePrefix: `/aws/lambda/${STACK}-` })
76 .pipe(Stream.runCollect);
77 const names = Array.from(pages)
78 .flatMap((page) => page.logGroups ?? [])
79 .flatMap((group) => (group.logGroupName ? [group.logGroupName] : []));
80 yield* Effect.forEach(
81 names,
82 (logGroupName) =>
83 Effect.logInfo(`RDSData reap: deleting log group ${logGroupName}`).pipe(
84 Effect.andThen(logs.deleteLogGroup({ logGroupName })),
85 Effect.catchTag("ResourceNotFoundException", () => Effect.void),
86 ),
87 { discard: true },
88 );
89 });
90
91 const reapRoles = Effect.gen(function* () {
92 const pages = yield* iam.listRoles.pages({}).pipe(Stream.runCollect);
93 const names = Array.from(pages)
94 .flatMap((page) => page.Roles ?? [])
95 .map((role) => role.RoleName)
96 .filter(matches);
97 yield* Effect.forEach(
98 names,
99 (RoleName) =>
100 Effect.gen(function* () {
101 yield* Effect.logInfo(`RDSData reap: deleting IAM role ${RoleName}`);
102 const attached = yield* iam.listAttachedRolePolicies
103 .pages({ RoleName })
104 .pipe(Stream.runCollect);
105 yield* Effect.forEach(
106 Array.from(attached)
107 .flatMap((page) => page.AttachedPolicies ?? [])
108 .flatMap((policy) => (policy.PolicyArn ? [policy.PolicyArn] : [])),
109 (PolicyArn) =>
110 iam
111 .detachRolePolicy({ RoleName, PolicyArn })
112 .pipe(Effect.catchTag("NoSuchEntityException", () => Effect.void)),
113 { discard: true },
114 );
115 const inline = yield* iam.listRolePolicies.pages({ RoleName }).pipe(Stream.runCollect);
116 yield* Effect.forEach(
117 Array.from(inline).flatMap((page) => page.PolicyNames ?? []),
118 (PolicyName) =>
119 iam
120 .deleteRolePolicy({ RoleName, PolicyName })
121 .pipe(Effect.catchTag("NoSuchEntityException", () => Effect.void)),
122 { discard: true },
123 );
124 yield* iam.deleteRole({ RoleName }).pipe(
125 Effect.retry({
126 while: (e) => e._tag === "DeleteConflictException",
127 schedule: dependencyRelease,
128 }),
129 Effect.catchTag("NoSuchEntityException", () => Effect.void),
130 );
131 }).pipe(
132 // The role may vanish between the list and the detach walk.
133 Effect.catchTag("NoSuchEntityException", () => Effect.void),
134 ),
135 { discard: true },
136 );
137 });
138
139 const countDbInstances = rds.describeDBInstances.pages({}).pipe(
140 Stream.runCollect,
141 Effect.map(
142 (pages) =>
143 Array.from(pages)
144 .flatMap((page) => page.DBInstances ?? [])
145 .filter((instance) => matches(instance.DBInstanceIdentifier)).length,
146 ),
147 );
148
149 const reapDbInstances = Effect.gen(function* () {
150 const pages = yield* rds.describeDBInstances.pages({}).pipe(Stream.runCollect);
151 const ids = Array.from(pages)
152 .flatMap((page) => page.DBInstances ?? [])
153 .map((instance) => instance.DBInstanceIdentifier)
154 .filter(matches);
155 yield* Effect.forEach(
156 ids,
157 (id) =>
158 Effect.logInfo(`RDSData reap: deleting DB instance ${id}`).pipe(
159 Effect.andThen(
160 rds.deleteDBInstance({
161 DBInstanceIdentifier: id,
162 SkipFinalSnapshot: true,
163 DeleteAutomatedBackups: true,
164 }),
165 ),
166 // InvalidDBInstanceStateFault: already deleting — the wait below
167 // confirms it actually goes away.
168 Effect.catchTag(
169 ["DBInstanceNotFoundFault", "InvalidDBInstanceStateFault"],
170 () => Effect.void,
171 ),
172 ),
173 { discard: true },
174 );
175 if (ids.length > 0) {
176 yield* countDbInstances.pipe(
177 Effect.repeat({ schedule: slowRelease, until: (n) => n === 0 }),
178 Effect.flatMap((n) =>
179 n === 0 ? Effect.void : Effect.fail(new OrphanStillPresent({ kind: "RDS DB instances" })),
180 ),
181 );
182 }
183 });
184
185 const countDbClusters = rds.describeDBClusters.pages({}).pipe(
186 Stream.runCollect,
187 Effect.map(
188 (pages) =>
189 Array.from(pages)
190 .flatMap((page) => page.DBClusters ?? [])
191 .filter((cluster) => matches(cluster.DBClusterIdentifier)).length,
192 ),
193 );
194
195 const reapDbClusters = Effect.gen(function* () {
196 const pages = yield* rds.describeDBClusters.pages({}).pipe(Stream.runCollect);
197 const ids = Array.from(pages)
198 .flatMap((page) => page.DBClusters ?? [])
199 .map((cluster) => cluster.DBClusterIdentifier)
200 .filter(matches);
201 yield* Effect.forEach(
202 ids,
203 (id) =>
204 Effect.logInfo(`RDSData reap: deleting DB cluster ${id}`).pipe(
205 Effect.andThen(
206 rds.deleteDBCluster({
207 DBClusterIdentifier: id,
208 SkipFinalSnapshot: true,
209 }),
210 ),
211 // InvalidDBClusterStateFault: already deleting / member still
212 // detaching — the wait below confirms convergence either way.
213 Effect.catchTag(
214 ["DBClusterNotFoundFault", "InvalidDBClusterStateFault"],
215 () => Effect.void,
216 ),
217 ),
218 { discard: true },
219 );
220 if (ids.length > 0) {
221 yield* countDbClusters.pipe(
222 Effect.repeat({ schedule: slowRelease, until: (n) => n === 0 }),
223 Effect.flatMap((n) =>
224 n === 0 ? Effect.void : Effect.fail(new OrphanStillPresent({ kind: "RDS DB clusters" })),
225 ),
226 );
227 }
228 });
229
230 const reapDbSubnetGroups = Effect.gen(function* () {
231 const pages = yield* rds.describeDBSubnetGroups.pages({}).pipe(Stream.runCollect);
232 const names = Array.from(pages)
233 .flatMap((page) => page.DBSubnetGroups ?? [])
234 .map((group) => group.DBSubnetGroupName)
235 .filter(matches);
236 yield* Effect.forEach(
237 names,
238 (name) =>
239 Effect.logInfo(`RDSData reap: deleting DB subnet group ${name}`).pipe(
240 Effect.andThen(rds.deleteDBSubnetGroup({ DBSubnetGroupName: name })),
241 Effect.retry({
242 while: (e) => e._tag === "InvalidDBSubnetGroupStateFault",
243 schedule: dependencyRelease,
244 }),
245 Effect.catchTag("DBSubnetGroupNotFoundFault", () => Effect.void),
246 ),
247 { discard: true },
248 );
249 });
250
251 const reapSecrets = Effect.gen(function* () {
252 const pages = yield* secretsmanager.listSecrets
253 .pages({
254 IncludePlannedDeletion: true,
255 Filters: [{ Key: "name", Values: [STACK] }],
256 })
257 .pipe(Stream.runCollect);
258 const names = Array.from(pages)
259 .flatMap((page) => page.SecretList ?? [])
260 .map((secret) => secret.Name)
261 .filter(matches);
262 yield* Effect.forEach(
263 names,
264 (name) =>
265 Effect.logInfo(`RDSData reap: force-deleting secret ${name}`).pipe(
266 Effect.andThen(
267 secretsmanager.deleteSecret({
268 SecretId: name,
269 ForceDeleteWithoutRecovery: true,
270 }),
271 ),
272 // InvalidRequestException: deletion already in progress.
273 Effect.catchTag(
274 ["ResourceNotFoundException", "InvalidRequestException"],
275 () => Effect.void,
276 ),
277 ),
278 { discard: true },
279 );
280 });
281
282 /**
283 * Lambda-managed ENIs release asynchronously (minutes) after their function
284 * is deleted; they block SG/subnet/VPC deletion until then. Each pass deletes
285 * the ENIs that have reached `available`, then re-counts.
286 */
287 const sweepNetworkInterfaces = Effect.fn(function* (vpcId: string) {
288 const describe = ec2
289 .describeNetworkInterfaces({
290 Filters: [{ Name: "vpc-id", Values: [vpcId] }],
291 })
292 .pipe(Effect.map((response) => response.NetworkInterfaces ?? []));
293 yield* Effect.gen(function* () {
294 const enis = yield* describe;
295 yield* Effect.forEach(
296 enis.filter((eni) => eni.Status === "available" && eni.NetworkInterfaceId),
297 (eni) =>
298 Effect.logInfo(`RDSData reap: deleting ENI ${eni.NetworkInterfaceId}`).pipe(
299 Effect.andThen(
300 ec2.deleteNetworkInterface({
301 NetworkInterfaceId: eni.NetworkInterfaceId,
302 }),
303 ),
304 Effect.catchTag(
305 ["InvalidNetworkInterfaceID.NotFound", "InvalidNetworkInterface.InUse"],
306 () => Effect.void,
307 ),
308 ),
309 { discard: true },
310 );
311 return (yield* describe).length;
312 }).pipe(
313 Effect.repeat({ schedule: slowRelease, until: (n) => n === 0 }),
314 Effect.flatMap((n) =>
315 n === 0
316 ? Effect.void
317 : Effect.fail(new OrphanStillPresent({ kind: `network interfaces in ${vpcId}` })),
318 ),
319 );
320 });
321
322 const reapVpc = Effect.fn(function* (vpcId: string) {
323 yield* Effect.logInfo(`RDSData reap: tearing down VPC ${vpcId}`);
324 yield* sweepNetworkInterfaces(vpcId);
325
326 // Security groups: break the LambdaSG <- DbSG ingress cross-reference
327 // first, then delete (bounded DependencyViolation retry).
328 const groups = yield* ec2
329 .describeSecurityGroups({
330 Filters: [{ Name: "vpc-id", Values: [vpcId] }],
331 })
332 .pipe(
333 Effect.map((response) =>
334 (response.SecurityGroups ?? []).filter(
335 (group) => group.GroupName !== "default" && group.GroupId,
336 ),
337 ),
338 );
339 yield* Effect.forEach(
340 groups,
341 (group) =>
342 (group.IpPermissions ?? []).length > 0
343 ? ec2
344 .revokeSecurityGroupIngress({
345 GroupId: group.GroupId,
346 IpPermissions: group.IpPermissions,
347 })
348 .pipe(
349 Effect.catchTag(
350 ["InvalidGroup.NotFound", "InvalidPermission.NotFound"],
351 () => Effect.void,
352 ),
353 )
354 : Effect.void,
355 { discard: true },
356 );
357 yield* Effect.forEach(
358 groups,
359 (group) =>
360 Effect.logInfo(
361 `RDSData reap: deleting security group ${group.GroupId} (${group.GroupName})`,
362 ).pipe(
363 Effect.andThen(ec2.deleteSecurityGroup({ GroupId: group.GroupId })),
364 Effect.retry({
365 while: (e) => e._tag === "DependencyViolation",
366 schedule: dependencyRelease,
367 }),
368 Effect.catchTag("InvalidGroup.NotFound", () => Effect.void),
369 ),
370 { discard: true },
371 );
372
373 const subnets = yield* ec2
374 .describeSubnets({ Filters: [{ Name: "vpc-id", Values: [vpcId] }] })
375 .pipe(
376 Effect.map((response) =>
377 (response.Subnets ?? []).flatMap((subnet) => (subnet.SubnetId ? [subnet.SubnetId] : [])),
378 ),
379 );
380 yield* Effect.forEach(
381 subnets,
382 (SubnetId) =>
383 Effect.logInfo(`RDSData reap: deleting subnet ${SubnetId}`).pipe(
384 Effect.andThen(ec2.deleteSubnet({ SubnetId })),
385 Effect.retry({
386 while: (e) => e._tag === "DependencyViolation",
387 schedule: dependencyRelease,
388 }),
389 Effect.catchTag("InvalidSubnetID.NotFound", () => Effect.void),
390 ),
391 { discard: true },
392 );
393
394 yield* ec2.deleteVpc({ VpcId: vpcId }).pipe(
395 Effect.retry({
396 while: (e) => e._tag === "DependencyViolation",
397 schedule: dependencyRelease,
398 }),
399 Effect.catchTag("InvalidVpcID.NotFound", () => Effect.void),
400 );
401 });
402
403 const reapVpcs = Effect.gen(function* () {
404 const byTag = yield* ec2.describeVpcs({
405 Filters: [{ Name: "tag:alchemy::stack", Values: [STACK] }],
406 });
407 const byCidr = yield* ec2.describeVpcs({
408 Filters: [{ Name: "cidr", Values: [VPC_CIDR] }],
409 });
410 const vpcIds = [
411 ...new Set(
412 [...(byTag.Vpcs ?? []), ...(byCidr.Vpcs ?? [])].flatMap((vpc) =>
413 vpc.VpcId ? [vpc.VpcId] : [],
414 ),
415 ),
416 ];
417 yield* Effect.forEach(vpcIds, reapVpc, { discard: true });
418 });
419
420 /**
421 * Delete every leftover RDSData fixture resource, in dependency order.
422 * Idempotent: a run against a clean account is a handful of empty list calls.
423 *
424 * Requires the AWS provider environment (wrap with `Core.withProviders`).
425 */
426 export const reapRDSDataOrphans = Effect.gen(function* () {
427 yield* reapFunctions;
428 yield* reapLogGroups;
429 yield* reapRoles;
430 yield* reapDbInstances;
431 yield* reapDbClusters;
432 yield* reapDbSubnetGroups;
433 yield* reapSecrets;
434 yield* reapVpcs;
435 });