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

main
ts 135 lines 3.93 KB
Raw
1 import { createHash } from "node:crypto";
2 import * as Effect from "effect/Effect";
3 import type { Cluster } from "../ECS/Cluster.ts";
4 import * as IAM from "../IAM/index.ts";
5 import type { EventBus } from "./EventBus.ts";
6 import { Rule, type RuleProps, type RuleTarget } from "./Rule.ts";
7
8 interface EventDescriptor {
9 id?: string;
10 bus?: EventBus;
11 pattern: Record<string, any>;
12 props?: Pick<RuleProps, "description" | "state">;
13 }
14
15 export interface EcsRouteTargetProps extends Pick<
16 RuleTarget,
17 "Input" | "InputPath" | "InputTransformer" | "RetryPolicy" | "DeadLetterConfig"
18 > {
19 task: {
20 taskDefinitionArn: string;
21 taskRoleArn: string;
22 executionRoleArn: string;
23 };
24 subnets: string[];
25 securityGroups?: string[];
26 assignPublicIp?: boolean;
27 taskCount?: number;
28 }
29
30 /**
31 * Routes matching events from an EventBridge bus to an ECS task run.
32 *
33 * Creates a {@link Rule} targeting the ECS cluster plus an IAM role that lets
34 * EventBridge call `ecs:RunTask` with the given task definition (Fargate
35 * launch type). Usually reached through the `events(...)` builder rather than
36 * called directly.
37 * **Example:** Run a Fargate Task for Matching Events
38 * ```typescript
39 * yield* AWS.EventBridge.events(bus, { source: ["my.app"] }).toEcsTask(cluster, {
40 * task: {
41 * taskDefinitionArn: yield* taskDefinition.taskDefinitionArn,
42 * taskRoleArn: yield* taskRole.roleArn,
43 * executionRoleArn: yield* executionRole.roleArn,
44 * },
45 * subnets: subnetIds,
46 * assignPublicIp: true,
47 * });
48 * ```
49 *
50 * @binding
51 */
52 export const toEcsTask = (
53 descriptor: EventDescriptor,
54 cluster: Cluster,
55 props: EcsRouteTargetProps,
56 ) =>
57 Effect.gen(function* () {
58 const routeId = descriptor.id ?? createRouteId(descriptor, `${cluster.LogicalId}Ecs`);
59
60 const role = yield* IAM.Role(`${routeId}${cluster.LogicalId}Role`, {
61 assumeRolePolicyDocument: {
62 Version: "2012-10-17",
63 Statement: [
64 {
65 Effect: "Allow",
66 Principal: {
67 Service: "events.amazonaws.com",
68 },
69 Action: ["sts:AssumeRole"],
70 Resource: ["*"],
71 },
72 ],
73 },
74 inlinePolicies: {
75 RunTask: {
76 Version: "2012-10-17",
77 Statement: [
78 {
79 Effect: "Allow",
80 Action: ["ecs:RunTask"],
81 Resource: [props.task.taskDefinitionArn],
82 },
83 {
84 Effect: "Allow",
85 Action: ["iam:PassRole"],
86 Resource: [props.task.taskRoleArn, props.task.executionRoleArn],
87 },
88 ],
89 },
90 },
91 });
92
93 return yield* Rule(routeId, {
94 description: descriptor.props?.description,
95 state: descriptor.props?.state,
96 eventBusName: descriptor.bus?.eventBusName,
97 eventPattern: descriptor.pattern,
98 targets: [
99 {
100 Id: `${cluster.LogicalId}Target`,
101 Arn: cluster.clusterArn as any,
102 RoleArn: role.roleArn as any,
103 Input: props.Input,
104 InputPath: props.InputPath,
105 InputTransformer: props.InputTransformer,
106 RetryPolicy: props.RetryPolicy,
107 DeadLetterConfig: props.DeadLetterConfig,
108 EcsParameters: {
109 TaskDefinitionArn: props.task.taskDefinitionArn,
110 TaskCount: props.taskCount ?? 1,
111 LaunchType: "FARGATE",
112 NetworkConfiguration: {
113 awsvpcConfiguration: {
114 Subnets: props.subnets,
115 SecurityGroups: props.securityGroups,
116 AssignPublicIp: props.assignPublicIp ? "ENABLED" : "DISABLED",
117 },
118 },
119 },
120 },
121 ],
122 });
123 });
124
125 const createRouteId = (descriptor: EventDescriptor, suffix: string) =>
126 `EventBridge${createHash("sha1")
127 .update(
128 JSON.stringify({
129 bus: descriptor.bus?.LogicalId ?? "default",
130 pattern: descriptor.pattern,
131 suffix,
132 }),
133 )
134 .digest("hex")
135 .slice(0, 10)}`;