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

main
ts 180 lines 5.31 KB
Raw
1 import * as Output from "alchemy/Output";
2 import * as Effect from "effect/Effect";
3 import { getAmi } from "./GetAmi.ts";
4
5 /**
6 * CPU architecture of the AMI to look up.
7 */
8 export type ImageArchitecture = "x86_64" | "arm64";
9
10 /**
11 * Filters for looking up the latest matching public AMI via
12 * `ec2:DescribeImages`.
13 */
14 export interface FindImageOptions {
15 /**
16 * AMI owners to search, e.g. `["amazon"]` or a vendor account ID like
17 * Canonical's `"099720109477"`.
18 */
19 owners: string[];
20 /**
21 * One or more AMI name patterns (glob-style). The newest available image
22 * matching any pattern wins.
23 */
24 name: [string, ...string[]];
25 /**
26 * CPU architecture to filter on.
27 * @default "x86_64"
28 */
29 architecture?: ImageArchitecture;
30 /**
31 * Human-readable label for the lookup (informational only).
32 */
33 description?: string;
34 /**
35 * Root device type to filter on.
36 * @default "ebs"
37 */
38 rootDeviceType?: "ebs" | "instance-store";
39 /**
40 * Virtualization type to filter on.
41 * @default "hvm"
42 */
43 virtualizationType?: "hvm" | "paravirtual";
44 }
45
46 const requireImageId = (options: FindImageOptions) =>
47 getAmi(options).pipe(
48 Output.mapEffect((image) =>
49 image?.ImageId
50 ? Effect.succeed(image.ImageId)
51 : Effect.die(
52 new Error(
53 `Could not resolve ${options.description ?? "an AMI"} matching ${options.name.join(", ")}`,
54 ),
55 ),
56 ),
57 );
58
59 /**
60 * Look up the latest available AMI ID matching the given filters.
61 *
62 * Returns an `Output<string>` resolved at plan/deploy time via the
63 * {@link getAmi} data source (and dies with a descriptive error when
64 * nothing matches), so it is safe to use both in resource props and in
65 * composition code that is re-executed inside a deployed runtime — the
66 * lookup never runs on the deployed machine. Use the preset helpers
67 * ({@link amazonLinux2023}, {@link ubuntu2404}, ...) for common distros.
68 *
69 * @example Find a custom AMI
70 * ```typescript
71 * const instance = yield* AWS.EC2.Instance("web", {
72 * imageId: AWS.EC2.image({
73 * owners: ["amazon"],
74 * name: ["al2023-ami-ecs-hvm-*"],
75 * architecture: "arm64",
76 * }),
77 * instanceType: "t4g.micro",
78 * subnetId: subnet.subnetId,
79 * });
80 * ```
81 */
82 export const image = (options: FindImageOptions) => requireImageId(options);
83
84 const amazonLinux2023Options = (options?: {
85 architecture?: ImageArchitecture;
86 }): FindImageOptions => ({
87 owners: ["amazon"],
88 // `al2023-ami-2023.*` selects the standard image. The broader
89 // `al2023-ami-*` also matches `al2023-ami-minimal-*`, which ships without
90 // the SSM agent and a stripped toolset and frequently sorts newest.
91 name: ["al2023-ami-2023.*"],
92 architecture: options?.architecture,
93 description: "Amazon Linux 2023",
94 });
95
96 const amazonLinux2Options = (options?: { architecture?: ImageArchitecture }): FindImageOptions => ({
97 owners: ["amazon"],
98 name: ["amzn2-ami-hvm-*-*-gp2"],
99 architecture: options?.architecture,
100 description: "Amazon Linux 2",
101 });
102
103 /**
104 * Resolve the latest Amazon Linux 2023 AMI ID for the current region as an
105 * `Output<string>`.
106 *
107 * @example Launch an Instance on Amazon Linux 2023
108 * ```typescript
109 * const instance = yield* AWS.EC2.Instance("web", {
110 * imageId: AWS.EC2.amazonLinux2023(),
111 * instanceType: "t3.micro",
112 * subnetId: subnet.subnetId,
113 * });
114 * ```
115 */
116 export const amazonLinux2023 = (options?: { architecture?: ImageArchitecture }) =>
117 image(amazonLinux2023Options(options));
118
119 /**
120 * Resolve the latest Amazon Linux 2 AMI ID for the current region as an
121 * `Output<string>`.
122 */
123 export const amazonLinux2 = (options?: { architecture?: ImageArchitecture }) =>
124 image(amazonLinux2Options(options));
125
126 /**
127 * Resolve the newest public Amazon Linux AMI as an `Output<string>`,
128 * preferring Amazon Linux 2023 and falling back to Amazon Linux 2. Dies if
129 * neither is available.
130 */
131 export const amazonLinux = (options?: { architecture?: ImageArchitecture }) =>
132 getAmi(amazonLinux2023Options(options)).pipe(
133 Output.flatMap((al2023) =>
134 al2023?.ImageId
135 ? Output.literal(al2023.ImageId)
136 : requireImageId({
137 ...amazonLinux2Options(options),
138 description: "a public Amazon Linux AMI",
139 }),
140 ),
141 );
142
143 /**
144 * Resolve the latest Canonical Ubuntu 24.04 LTS AMI ID for the current
145 * region as an `Output<string>`.
146 *
147 * @example Launch an Instance on Ubuntu 24.04
148 * ```typescript
149 * const instance = yield* AWS.EC2.Instance("web", {
150 * imageId: AWS.EC2.ubuntu2404(),
151 * instanceType: "t3.micro",
152 * subnetId: subnet.subnetId,
153 * });
154 * ```
155 */
156 export const ubuntu2404 = (options?: { architecture?: ImageArchitecture }) =>
157 image({
158 owners: ["099720109477"],
159 name: [
160 "ubuntu/images/hvm-ssd-gp3/ubuntu-noble-24.04-*-server-*",
161 "ubuntu/images/hvm-ssd/ubuntu-noble-24.04-*-server-*",
162 ],
163 architecture: options?.architecture,
164 description: "Ubuntu 24.04 LTS",
165 });
166
167 /**
168 * Resolve the latest Canonical Ubuntu 22.04 LTS AMI ID for the current
169 * region as an `Output<string>`.
170 */
171 export const ubuntu2204 = (options?: { architecture?: ImageArchitecture }) =>
172 image({
173 owners: ["099720109477"],
174 name: [
175 "ubuntu/images/hvm-ssd-gp3/ubuntu-jammy-22.04-*-server-*",
176 "ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-*-server-*",
177 ],
178 architecture: options?.architecture,
179 description: "Ubuntu 22.04 LTS",
180 });