@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 65 lines 2.37 KB
Raw
1 import * as ec2 from "@distilled.cloud/aws/ec2";
2 import * as Data from "effect/Data";
3 import * as Effect from "effect/Effect";
4 import * as Schedule from "effect/Schedule";
5 import { getDefaultVpc } from "../DefaultVpc.ts";
6
7 class DefaultSubnetNotVisible extends Data.TaggedError("DefaultSubnetNotVisible")<{}> {}
8
9 /**
10 * Resolve the newest Amazon Linux 2023 AMI id out-of-band via distilled
11 * `ec2.describeImages`, for launch templates created directly with the raw
12 * SDK (outside `stack.deploy`, where the `amazonLinux2023()` Output would be
13 * resolved by the engine). Launch templates do not validate the AMI at
14 * creation time, so fall back to a syntactically valid id when the lookup
15 * returns nothing.
16 */
17 export const getTestAmiId: Effect.Effect<string, any, any> = ec2
18 .describeImages({
19 Owners: ["amazon"],
20 Filters: [
21 { Name: "name", Values: ["al2023-ami-2023.*"] },
22 { Name: "architecture", Values: ["x86_64"] },
23 { Name: "state", Values: ["available"] },
24 { Name: "root-device-type", Values: ["ebs"] },
25 { Name: "virtualization-type", Values: ["hvm"] },
26 ],
27 })
28 .pipe(
29 Effect.map(
30 (response) =>
31 (response.Images ?? [])
32 .slice()
33 .sort((a, b) =>
34 String(b.CreationDate ?? "").localeCompare(String(a.CreationDate ?? "")),
35 )[0]?.ImageId ?? "ami-00000000000000000",
36 ),
37 );
38
39 /**
40 * Resolve one deterministic subnet from the standing default VPC. The account
41 * nuke may remove that VPC, and CreateDefaultVpc can return before its default
42 * subnets are queryable, so restore it and wait for subnet readiness boundedly.
43 */
44 export const getAutoScalingTestSubnetId: Effect.Effect<`subnet-${string}`, any, any> = Effect.gen(
45 function* () {
46 const { vpcId } = yield* getDefaultVpc;
47 const response = yield* ec2.describeSubnets({
48 Filters: [
49 { Name: "vpc-id", Values: [vpcId] },
50 { Name: "default-for-az", Values: ["true"] },
51 ],
52 } as any);
53 const subnetId = (response.Subnets ?? [])
54 .flatMap((subnet) => (subnet.SubnetId ? [subnet.SubnetId] : []))
55 .sort()[0];
56 return subnetId
57 ? (subnetId as `subnet-${string}`)
58 : yield* Effect.fail(new DefaultSubnetNotVisible());
59 },
60 ).pipe(
61 Effect.retry({
62 while: (error) => error._tag === "DefaultSubnetNotVisible",
63 schedule: Schedule.max([Schedule.fixed("1 second"), Schedule.recurs(30)]),
64 }),
65 );