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

main
ts 190 lines 8.21 KB
Raw
1 import * as Namespace from "alchemy/Namespace";
2 import * as Output from "alchemy/Output";
3 import type lambda from "aws-lambda";
4 import * as Effect from "effect/Effect";
5 import * as Layer from "effect/Layer";
6 import * as Stream from "effect/Stream";
7 import {
8 KafkaEventSource as MSKKafkaEventSource,
9 type KafkaEventSourceProps,
10 type KafkaEventSourceService,
11 type MSKRecord,
12 } from "../Kafka/ClusterEventSource.ts";
13 import type { ServerlessCluster } from "../Kafka/ServerlessCluster.ts";
14 import { EventSourceMapping } from "./EventSourceMapping.ts";
15 import * as Lambda from "./Function.ts";
16
17 export const isMSKEvent = (event: any): event is lambda.MSKEvent =>
18 event?.eventSource === "aws:kafka" &&
19 typeof event?.records === "object" &&
20 event.records !== null;
21
22 // The cluster ARN is `arn:...:cluster/name/uuid`. Topic and consumer-group
23 // resources live under sibling namespaces (`topic/` and `group/`) scoped to the
24 // same cluster path; MSK IAM auth grants data-plane access via these ARNs.
25 const topicArnGlob = (clusterArn: string) => `${clusterArn.replace(":cluster/", ":topic/")}/*`;
26 const groupArnGlob = (clusterArn: string) => `${clusterArn.replace(":cluster/", ":group/")}/*`;
27 // `arn:{partition}:kafka:{region}:{account}:cluster/...` →
28 // `arn:{partition}:ec2:{region}:{account}:`
29 const ec2ArnPrefix = (clusterArn: string) => {
30 const [arn, partition, , region, account] = clusterArn.split(":");
31 return `${arn}:${partition}:ec2:${region}:${account}:`;
32 };
33
34 /** @binding */
35 export const KafkaEventSource = Layer.effect(
36 MSKKafkaEventSource,
37 Effect.gen(function* () {
38 const host = yield* Lambda.eventSourceHost("AWS.Lambda.KafkaEventSource");
39 const Mapping = yield* EventSourceMapping;
40
41 return Effect.fn(function* <StreamReq = never, Req = never>(
42 cluster: ServerlessCluster,
43 props: KafkaEventSourceProps,
44 process: (
45 stream: Stream.Stream<MSKRecord, never, StreamReq>,
46 ) => Effect.Effect<void, never, Req>,
47 ) {
48 // Deploy-time: grant the IAM actions MSK IAM authentication requires and
49 // create the event-source mapping. Skipped once running inside the
50 // deployed Function (the global guard). Namespaced under the host so the
51 // mapping's logical identity is stable.
52 if (!globalThis.__ALCHEMY_RUNTIME__) {
53 yield* Namespace.push(
54 host.LogicalId,
55 Effect.gen(function* () {
56 yield* host.bind`Allow(${host}, AWS.Lambda.KafkaEventSource(${cluster}))`({
57 policyStatements: [
58 {
59 // Cluster-level: connect + describe (Lambda poller + IAM auth).
60 Effect: "Allow",
61 Action: [
62 "kafka-cluster:Connect",
63 "kafka-cluster:DescribeCluster",
64 "kafka-cluster:DescribeClusterDynamicConfiguration",
65 "kafka:DescribeClusterV2",
66 "kafka:GetBootstrapBrokers",
67 ],
68 Resource: [cluster.clusterArn],
69 },
70 {
71 // Topic-level: read records + describe topics.
72 Effect: "Allow",
73 Action: ["kafka-cluster:DescribeTopic", "kafka-cluster:ReadData"],
74 Resource: [cluster.clusterArn.pipe(Output.map(topicArnGlob))],
75 },
76 {
77 // Consumer-group-level: join/commit as a consumer group.
78 Effect: "Allow",
79 Action: ["kafka-cluster:DescribeGroup", "kafka-cluster:AlterGroup"],
80 Resource: [cluster.clusterArn.pipe(Output.map(groupArnGlob))],
81 },
82 {
83 // The poller reaches the cluster through ENIs in its VPC;
84 // Lambda validates these on the execution role when the
85 // mapping is created. EC2 Describe* actions do not support
86 // resource-level permissions, so scope them to the
87 // cluster's region instead.
88 Effect: "Allow",
89 Action: [
90 "ec2:DescribeNetworkInterfaces",
91 "ec2:DescribeSecurityGroups",
92 "ec2:DescribeSubnets",
93 "ec2:DescribeVpcs",
94 ],
95 Resource: ["*"],
96 Condition: {
97 StringEquals: {
98 "aws:RequestedRegion": cluster.clusterArn.pipe(
99 Output.map((arn) => arn.split(":")[3]!),
100 ),
101 },
102 },
103 },
104 {
105 // ENIs may only be created in the cluster's subnets with
106 // the cluster's security groups.
107 Effect: "Allow",
108 Action: ["ec2:CreateNetworkInterface"],
109 Resource: Output.all(
110 cluster.clusterArn,
111 cluster.subnetIds,
112 cluster.securityGroupIds,
113 ).pipe(
114 Output.map(([clusterArn, subnetIds, securityGroupIds]) => [
115 `${ec2ArnPrefix(clusterArn)}network-interface/*`,
116 ...subnetIds.map((id) => `${ec2ArnPrefix(clusterArn)}subnet/${id}`),
117 ...securityGroupIds.map(
118 (id) => `${ec2ArnPrefix(clusterArn)}security-group/${id}`,
119 ),
120 ]),
121 ),
122 },
123 {
124 // ENIs may only be deleted in the cluster's subnets. Creating
125 // the mapping makes Lambda check this permission with no ENI
126 // and no subnet in the request, which fails a
127 // `network-interface/*` resource or a plain `ec2:Subnet`
128 // condition ("Cannot delete ENIs on the subnets and security
129 // groups associated with the event source"). `IfExists`
130 // passes that check while every real delete, which always
131 // carries the ENI's subnet, stays in the cluster's subnets.
132 Effect: "Allow",
133 Action: ["ec2:DeleteNetworkInterface"],
134 Resource: ["*"],
135 Condition: {
136 StringEquals: {
137 "aws:RequestedRegion": cluster.clusterArn.pipe(
138 Output.map((arn) => arn.split(":")[3]!),
139 ),
140 },
141 ArnEqualsIfExists: {
142 "ec2:Subnet": Output.all(cluster.clusterArn, cluster.subnetIds).pipe(
143 Output.map(([clusterArn, subnetIds]) =>
144 subnetIds.map((id) => `${ec2ArnPrefix(clusterArn)}subnet/${id}`),
145 ),
146 ),
147 },
148 },
149 },
150 ],
151 });
152
153 yield* Mapping(
154 `AWS.Lambda.EventSourceMapping(${host.LogicalId}, ${cluster.LogicalId})`,
155 {
156 functionName: Lambda.qualifiedNameOf(host),
157 eventSourceArn: cluster.clusterArn,
158 topics: props.topics,
159 amazonManagedKafkaEventSourceConfig: props.consumerGroupId
160 ? { ConsumerGroupId: props.consumerGroupId }
161 : undefined,
162 batchSize: props.batchSize,
163 maximumBatchingWindow: props.maximumBatchingWindow,
164 startingPosition: props.startingPosition ?? "LATEST",
165 enabled: props.enabled ?? true,
166 filterCriteria: props.filterCriteria,
167 provisionedPollerConfig: props.provisionedPollerConfig,
168 },
169 );
170 }),
171 );
172 }
173
174 yield* host.listen(
175 Effect.gen(function* () {
176 return (event: any) => {
177 if (isMSKEvent(event)) {
178 // `event.records` maps `topic-partition` → MSKRecord[]; flatten
179 // to a single stream of records.
180 const records = Object.values(event.records).flat() as MSKRecord[];
181 if (records.length > 0) {
182 return process(Stream.fromArray(records)).pipe(Effect.orDie);
183 }
184 }
185 };
186 }),
187 );
188 }) as KafkaEventSourceService;
189 }),
190 );