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

main
ts 108 lines 4.38 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 /**
3 * An "orders" app on ECS Fargate that exercises the full container surface:
4 *
5 * - a stack-owned SHARED ALB + HTTP listener (`src/infra.ts`) that two
6 * services attach path rules to,
7 * - `Api` — an effectful `AWS.ECS.Service` (bundled `main:` image source)
8 * serving `/api/*` with DynamoDB + `RunTask` bindings (`src/Api.ts`),
9 * - `Web` — an EXTERNAL `AWS.ECS.Service` (registry `image:` source, no
10 * Effect runtime in the container) serving the catch-all `/*`,
11 * - `SeedTask` — an inline-effect one-shot `AWS.ECS.Task` (`{ run }`) that
12 * seeds the orders table, launched from `POST /api/seed` (`src/SeedTask.ts`),
13 * - `ReportTask` — an external one-shot task built from a local Dockerfile
14 * (`context:` image source, `src/report/Dockerfile`),
15 * - `HeartbeatTask` — an external one-shot task on an EventBridge cron
16 * schedule via `AWS.ECS.every`.
17 */
18 import * as Alchemy from "alchemy";
19 import * as Output from "alchemy/Output";
20 import * as Effect from "effect/Effect";
21 import ApiLive, { Api } from "./src/Api.ts";
22 import { OrdersCluster, OrdersIngress, OrdersNetwork, OrdersTable } from "./src/infra.ts";
23
24 export default Alchemy.Stack(
25 "AwsEcsExample",
26 {
27 providers: AWS.providers(),
28 state: Alchemy.localState(),
29 },
30 Effect.gen(function* () {
31 const network = yield* OrdersNetwork;
32 const cluster = yield* OrdersCluster;
33 const table = yield* OrdersTable;
34 const { alb, listener } = yield* OrdersIngress;
35
36 // ── Api — effectful service in the TAGGED form (bundled `main:`),
37 // /api/* on the shared ALB. `ApiLive` (the `Api.make(...)` Layer,
38 // provided below) carries the props + init program; its init also
39 // declares SeedTask (for the RunTask binding) and the DynamoDB
40 // bindings; see src/Api.ts.
41 const api = yield* Api;
42
43 // ── Web — EXTERNAL service: a pre-built registry image (mirrored into
44 // ECR), no Effect runtime in the container. Attaches the catch-all `/*`
45 // rule to the shared listener; the explicit priority orders it AFTER
46 // Api's `/api/*` rule (lower number evaluates first).
47 const web = yield* AWS.ECS.Service("Web", {
48 cluster,
49 image: "nginxdemos/hello:plain-text",
50 port: 80,
51 desiredCount: 1,
52 vpcId: network.vpcId,
53 subnets: network.publicSubnetIds,
54 assignPublicIp: true,
55 loadBalancer: {
56 listener,
57 rules: [{ path: "/*", priority: 20000 }],
58 },
59 });
60
61 // ── ReportTask — EXTERNAL one-shot task built from the example's own
62 // Dockerfile (`context:` image source; `dockerfile` defaults to
63 // `${context}/Dockerfile`). Deploy builds + pushes the image and
64 // registers the task definition; run it with `RunTask` or a schedule.
65 const reportTask = yield* AWS.ECS.Task("ReportTask", {
66 context: `${import.meta.dirname}/src/report`,
67 cpu: 256,
68 memory: 512,
69 // Build/run on ARM64 so an image built on an Apple Silicon host
70 // matches the Fargate runtime architecture (Graviton).
71 runtimePlatform: {
72 cpuArchitecture: "ARM64",
73 operatingSystemFamily: "LINUX",
74 },
75 });
76
77 // ── HeartbeatTask — EXTERNAL one-shot task (registry `image:` source)
78 // run nightly by an EventBridge Scheduler cron via `AWS.ECS.every`
79 // (which provisions the schedule plus the ecs:RunTask invoke role).
80 const heartbeatTask = yield* AWS.ECS.Task("HeartbeatTask", {
81 image: "busybox:stable",
82 command: ["sh", "-c", "echo orders heartbeat"],
83 cpu: 256,
84 memory: 512,
85 });
86 yield* AWS.ECS.every("HeartbeatSchedule", "cron(0 3 * * ? *)", {
87 cluster,
88 task: heartbeatTask,
89 subnets: network.publicSubnetIds,
90 assignPublicIp: true,
91 });
92
93 return {
94 // Service-derived URLs, not `http://${alb.dnsName}`: in `alchemy dev`
95 // the emulated ALB is host-routed on the emulator gateway port, and
96 // the service attribute is the only place that port is known.
97 url: web.url,
98 apiUrl: Output.interpolate`${api.url}/api/orders`,
99 seedUrl: Output.interpolate`${api.url}/api/seed`,
100 albDnsName: alb.dnsName,
101 tableName: table.tableName,
102 apiServiceName: api.serviceName,
103 webServiceName: web.serviceName,
104 reportTaskDefinitionArn: reportTask.taskDefinitionArn,
105 heartbeatTaskDefinitionArn: heartbeatTask.taskDefinitionArn,
106 };
107 }).pipe(Effect.provide(ApiLive)),
108 );