The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as ec2 from "@distilled.cloud/aws/ec2"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Layer from "effect/Layer"; |
| 4 | import * as AWSBindings from "../AWSBindings.ts"; |
| 5 | import { GetAmi } from "./GetAmi.ts"; |
| 6 | import type { FindImageOptions } from "./Image.ts"; |
| 7 | |
| 8 | // Bespoke (not the shared scaffold): the lookup filters and sorts the |
| 9 | // paginated image list to pick the newest available match, and the binding |
| 10 | // is account-scoped — there is no resource to bind, only options. |
| 11 | export const GetAmiHttp = Layer.effect( |
| 12 | GetAmi, |
| 13 | Effect.gen(function* () { |
| 14 | const bind = yield* AWSBindings.Bind; |
| 15 | const describeImages = yield* AWSBindings.withCredentials(ec2.describeImages); |
| 16 | |
| 17 | return Effect.fn(function* (options: FindImageOptions) { |
| 18 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 19 | yield* bind`AWS.EC2.GetAmi`({ |
| 20 | policyStatements: [ |
| 21 | { |
| 22 | Effect: "Allow", |
| 23 | Action: ["ec2:DescribeImages"], |
| 24 | Resource: ["*"], |
| 25 | }, |
| 26 | ], |
| 27 | }); |
| 28 | } |
| 29 | |
| 30 | const { |
| 31 | owners, |
| 32 | name, |
| 33 | architecture = "x86_64", |
| 34 | rootDeviceType = "ebs", |
| 35 | virtualizationType = "hvm", |
| 36 | } = options; |
| 37 | |
| 38 | return Effect.fn("AWS.EC2.GetAmi")(function* () { |
| 39 | const response = yield* describeImages({ |
| 40 | Owners: owners, |
| 41 | Filters: [ |
| 42 | { Name: "name", Values: [...name] }, |
| 43 | { Name: "architecture", Values: [architecture] }, |
| 44 | { Name: "state", Values: ["available"] }, |
| 45 | { Name: "root-device-type", Values: [rootDeviceType] }, |
| 46 | { Name: "virtualization-type", Values: [virtualizationType] }, |
| 47 | ], |
| 48 | }); |
| 49 | return (response.Images ?? []) |
| 50 | .slice() |
| 51 | .sort((a: ec2.Image, b: ec2.Image) => |
| 52 | String(b.CreationDate ?? "").localeCompare(String(a.CreationDate ?? "")), |
| 53 | )[0]; |
| 54 | }); |
| 55 | }); |
| 56 | }), |
| 57 | ); |